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