Elgg  Version 1.10
MockSessionStorage.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Http;
3 
37 
39  protected $started = false;
40 
42  protected $closed = false;
43 
45  protected $id = '';
46 
48  protected $name;
49 
51  protected $data = array();
52 
58  public function __construct($name = 'MOCKSESSID') {
59  $this->name = $name;
60  }
61 
65  public function start() {
66  if ($this->started && !$this->closed) {
67  return true;
68  }
69 
70  if (empty($this->id)) {
71  $this->id = $this->generateId();
72  }
73 
74  $this->started = true;
75  $this->closed = false;
76 
77  return true;
78  }
79 
83  public function regenerate($destroy = false, $lifetime = null) {
84  if (!$this->started) {
85  $this->start();
86  }
87 
88  $this->id = $this->generateId();
89 
90  return true;
91  }
92 
96  public function save() {
97  if (!$this->started || $this->closed) {
98  throw new \RuntimeException("Trying to save a session that was not started yet or was already closed");
99  }
100  $this->closed = false;
101  }
102 
106  public function isStarted() {
107  return $this->started;
108  }
109 
113  public function getId() {
114  return $this->id;
115  }
116 
120  public function setId($id) {
121  if ($this->started) {
122  throw new \RuntimeException('Cannot change the ID of an active session');
123  }
124 
125  $this->id = $id;
126  }
127 
131  public function getName() {
132  return $this->name;
133  }
134 
138  public function setName($name) {
139  $this->name = $name;
140  }
141 
150  protected function generateId() {
151  return sha1(uniqid(mt_rand()));
152  }
153 
157  public function has($name) {
158  if (!$this->started) {
159  $this->start();
160  }
161  return array_key_exists($name, $this->data);
162  }
163 
167  public function get($name, $default = null) {
168  if (!$this->started) {
169  $this->start();
170  }
171  return array_key_exists($name, $this->data) ? $this->data[$name] : $default;
172  }
173 
177  public function set($name, $value) {
178  if (!$this->started) {
179  $this->start();
180  }
181  $this->data[$name] = $value;
182  }
183 
187  public function all() {
188  if (!$this->started) {
189  $this->start();
190  }
191  return $this->data;
192  }
193 
197  public function replace(array $attributes) {
198  if (!$this->started) {
199  $this->start();
200  }
201  $this->data = array();
202  foreach ($attributes as $key => $value) {
203  $this->set($key, $value);
204  }
205  }
206 
210  public function remove($name) {
211  if (!$this->started) {
212  $this->start();
213  }
214  $retval = null;
215  if (array_key_exists($name, $this->data)) {
216  $retval = $this->data[$name];
217  unset($this->data[$name]);
218  }
219 
220  return $retval;
221  }
222 
226  public function clear() {
227  if (!$this->started) {
228  $this->start();
229  }
230  $this->data = array();
231  }
232 
233 }
234 
__construct($name= 'MOCKSESSID')
Constructor.
$value
Definition: longtext.php:29
$default
Definition: checkbox.php:36
generateId()
Generates a session ID.
has($name)
{Checks if an attribute is defined.The attribute nameboolean}
$key
Definition: summary.php:34
setId($id)
{Sets the session ID.Session string void}
getName()
{Returns the session name.string The session name.}
save()
{Force the session to be saved and closed.This method must invoke session_write_close() unless this i...
clear()
{Clears all attributes.void}
regenerate($destroy=false, $lifetime=null)
{Regenerates id that represents this storage.This method must invoke session_regenerate_id($destroy) ...
$site name
replace(array $attributes)
{Replaces all attributes.Attributes void}
start()
{Starts the session.boolean True if started. }
isStarted()
{Checks if the session is started.boolean True if started, false otherwise.}
all()
{Returns all attributes.array Attributes}
$attributes
Definition: ajax_loader.php:13
getId()
{Returns the session ID.string The session ID or empty.}
setName($name)
{Sets the session name.Session name. void}