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