[ Index ] |
PHP Cross Reference of phpBB-3.3.14-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 $data = $this->data; 95 96 foreach ($this->bags as $bag) { 97 if (empty($data[$key = $bag->getStorageKey()])) { 98 unset($data[$key]); 99 } 100 } 101 if ([$key = $this->metadataBag->getStorageKey()] === array_keys($data)) { 102 unset($data[$key]); 103 } 104 105 try { 106 if ($data) { 107 file_put_contents($this->getFilePath(), serialize($data)); 108 } else { 109 $this->destroy(); 110 } 111 } finally { 112 $this->data = $data; 113 } 114 115 // this is needed for Silex, where the session object is re-used across requests 116 // in functional tests. In Symfony, the container is rebooted, so we don't have 117 // this issue 118 $this->started = false; 119 } 120 121 /** 122 * Deletes a session from persistent storage. 123 * Deliberately leaves session data in memory intact. 124 */ 125 private function destroy() 126 { 127 if (is_file($this->getFilePath())) { 128 unlink($this->getFilePath()); 129 } 130 } 131 132 /** 133 * Calculate path to file. 134 * 135 * @return string File path 136 */ 137 private function getFilePath() 138 { 139 return $this->savePath.'/'.$this->id.'.mocksess'; 140 } 141 142 /** 143 * Reads session from storage and loads session. 144 */ 145 private function read() 146 { 147 $filePath = $this->getFilePath(); 148 $this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : []; 149 150 $this->loadSession(); 151 } 152 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |