| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfSessionTestStorage extends sfStorage |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$sessionId = null, |
|---|
| 23 |
$sessionData = array(), |
|---|
| 24 |
$sessionPath = null; |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
* Initializes this Storage instance. |
|---|
| 28 |
* |
|---|
| 29 |
* @param sfContext A sfContext instance |
|---|
| 30 |
* @param array An associative array of initialization parameters |
|---|
| 31 |
* |
|---|
| 32 |
* @return boolean true, if initialization completes successfully, otherwise false |
|---|
| 33 |
* |
|---|
| 34 |
* @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage |
|---|
| 35 |
*/ |
|---|
| 36 |
public function initialize($context, $parameters = null) |
|---|
| 37 |
{ |
|---|
| 38 |
|
|---|
| 39 |
parent::initialize($context, $parameters); |
|---|
| 40 |
|
|---|
| 41 |
$this->sessionPath = sfConfig::get('sf_test_cache_dir').DIRECTORY_SEPARATOR.'sessions'; |
|---|
| 42 |
|
|---|
| 43 |
if (array_key_exists('session_id', $_SERVER)) |
|---|
| 44 |
{ |
|---|
| 45 |
$this->sessionId = $_SERVER['session_id']; |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
$file = $this->sessionPath.DIRECTORY_SEPARATOR.$this->sessionId.'.session'; |
|---|
| 49 |
$this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array(); |
|---|
| 50 |
} |
|---|
| 51 |
else |
|---|
| 52 |
{ |
|---|
| 53 |
$this->sessionId = md5(uniqid(rand(), true)); |
|---|
| 54 |
$this->sessionData = array(); |
|---|
| 55 |
} |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
* Gets session id for the current session storage instance. |
|---|
| 60 |
* |
|---|
| 61 |
* @return string Session id |
|---|
| 62 |
*/ |
|---|
| 63 |
public function getSessionId() |
|---|
| 64 |
{ |
|---|
| 65 |
return $this->sessionId; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
* Reads data from this storage. |
|---|
| 70 |
* |
|---|
| 71 |
* The preferred format for a key is directory style so naming conflicts can be avoided. |
|---|
| 72 |
* |
|---|
| 73 |
* @param string A unique key identifying your data |
|---|
| 74 |
* |
|---|
| 75 |
* @return mixed Data associated with the key |
|---|
| 76 |
*/ |
|---|
| 77 |
public function & read($key) |
|---|
| 78 |
{ |
|---|
| 79 |
$retval = null; |
|---|
| 80 |
|
|---|
| 81 |
if (isset($this->sessionData[$key])) |
|---|
| 82 |
{ |
|---|
| 83 |
$retval =& $this->sessionData[$key]; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
return $retval; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
* Removes data from this storage. |
|---|
| 91 |
* |
|---|
| 92 |
* The preferred format for a key is directory style so naming conflicts can be avoided. |
|---|
| 93 |
* |
|---|
| 94 |
* @param string A unique key identifying your data |
|---|
| 95 |
* |
|---|
| 96 |
* @return mixed Data associated with the key |
|---|
| 97 |
*/ |
|---|
| 98 |
public function & remove($key) |
|---|
| 99 |
{ |
|---|
| 100 |
$retval = null; |
|---|
| 101 |
|
|---|
| 102 |
if (isset($this->sessionData[$key])) |
|---|
| 103 |
{ |
|---|
| 104 |
$retval =& $this->sessionData[$key]; |
|---|
| 105 |
unset($this->sessionData[$key]); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
return $retval; |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
* Writes data to this storage. |
|---|
| 113 |
* |
|---|
| 114 |
* The preferred format for a key is directory style so naming conflicts can be avoided |
|---|
| 115 |
* |
|---|
| 116 |
* @param string A unique key identifying your data |
|---|
| 117 |
* @param mixed Data associated with your key |
|---|
| 118 |
* |
|---|
| 119 |
*/ |
|---|
| 120 |
public function write($key, &$data) |
|---|
| 121 |
{ |
|---|
| 122 |
$this->sessionData[$key] =& $data; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
* Clears all test sessions. |
|---|
| 127 |
*/ |
|---|
| 128 |
public function clear() |
|---|
| 129 |
{ |
|---|
| 130 |
sfToolkit::clearDirectory($this->sessionPath); |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
* Executes the shutdown procedure. |
|---|
| 135 |
* |
|---|
| 136 |
*/ |
|---|
| 137 |
public function shutdown() |
|---|
| 138 |
{ |
|---|
| 139 |
if ($this->sessionId) |
|---|
| 140 |
{ |
|---|
| 141 |
$current_umask = umask(0000); |
|---|
| 142 |
if (!is_dir($this->sessionPath)) |
|---|
| 143 |
{ |
|---|
| 144 |
mkdir($this->sessionPath, 0777, true); |
|---|
| 145 |
} |
|---|
| 146 |
umask($current_umask); |
|---|
| 147 |
file_put_contents($this->sessionPath.DIRECTORY_SEPARATOR.$this->sessionId.'.session', serialize($this->sessionData)); |
|---|
| 148 |
$this->sessionId = ''; |
|---|
| 149 |
$this->sessionData = array(); |
|---|
| 150 |
} |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|