[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpFoundation\Session\Storage; 13 14 /** 15 * MockFileSessionStorage is used to mock sessions for 16 * functional testing when done in a single PHP process. 17 * 18 * No PHP session is actually started since a session can be initialized 19 * and shutdown only once per PHP execution cycle and this class does 20 * not pollute any session related globals, including session_*() functions 21 * or session.* PHP ini directives. 22 * 23 * @author Drak <drak@zikula.org> 24 */ 25 class MockFileSessionStorage extends MockArraySessionStorage 26 { 27 private $savePath; 28 29 /** 30 * @param string $savePath Path of directory to save session files 31 * @param string $name Session name 32 * @param MetadataBag $metaBag MetadataBag instance 33 */ 34 public function __construct($savePath = null, $name = 'MOCKSESSID', MetadataBag $metaBag = null) 35 { 36 if (null === $savePath) { 37 $savePath = sys_get_temp_dir(); 38 } 39 40 if (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath)) { 41 throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s"', $savePath)); 42 } 43 44 $this->savePath = $savePath; 45 46 parent::__construct($name, $metaBag); 47 } 48 49 /** 50 * {@inheritdoc} 51 */ 52 public function start() 53 { 54 if ($this->started) { 55 return true; 56 } 57 58 if (!$this->id) { 59 $this->id = $this->generateId(); 60 } 61 62 $this->read(); 63 64 $this->started = true; 65 66 return true; 67 } 68 69 /** 70 * {@inheritdoc} 71 */ 72 public function regenerate($destroy = false, $lifetime = null) 73 { 74 if (!$this->started) { 75 $this->start(); 76 } 77 78 if ($destroy) { 79 $this->destroy(); 80 } 81 82 return parent::regenerate($destroy, $lifetime); 83 } 84 85 /** 86 * {@inheritdoc} 87 */ 88 public function save() 89 { 90 if (!$this->started) { 91 throw new \RuntimeException('Trying to save a session that was not started yet or was already closed'); 92 } 93 94 file_put_contents($this->getFilePath(), serialize($this->data)); 95 96 // this is needed for Silex, where the session object is re-used across requests 97 // in functional tests. In Symfony, the container is rebooted, so we don't have 98 // this issue 99 $this->started = false; 100 } 101 102 /** 103 * Deletes a session from persistent storage. 104 * Deliberately leaves session data in memory intact. 105 */ 106 private function destroy() 107 { 108 if (is_file($this->getFilePath())) { 109 unlink($this->getFilePath()); 110 } 111 } 112 113 /** 114 * Calculate path to file. 115 * 116 * @return string File path 117 */ 118 private function getFilePath() 119 { 120 return $this->savePath.'/'.$this->id.'.mocksess'; 121 } 122 123 /** 124 * Reads session from storage and loads session. 125 */ 126 private function read() 127 { 128 $filePath = $this->getFilePath(); 129 $this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array(); 130 131 $this->loadSession(); 132 } 133 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |