Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Namespace.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Session.php';
28 
29 
33 #require_once 'Zend/Session/Abstract.php';
34 
35 
44 class Zend_Session_Namespace extends Zend_Session_Abstract implements IteratorAggregate
45 {
46 
50  const SINGLE_INSTANCE = true;
51 
57  protected $_namespace = "Default";
58 
64  protected static $_namespaceLocks = array();
65 
71  protected static $_singleInstances = array();
72 
79  public static function resetSingleInstance($namespaceName = null)
80  {
81  if ($namespaceName != null) {
82  if (array_key_exists($namespaceName, self::$_singleInstances)) {
83  unset(self::$_singleInstances[$namespaceName]);
84  }
85  return;
86  }
87 
88  self::$_singleInstances = array();
89  return;
90  }
91 
102  public function __construct($namespace = 'Default', $singleInstance = false)
103  {
104  if ($namespace === '') {
108  #require_once 'Zend/Session/Exception.php';
109  throw new Zend_Session_Exception('Session namespace must be a non-empty string.');
110  }
111 
112  if ($namespace[0] == "_") {
116  #require_once 'Zend/Session/Exception.php';
117  throw new Zend_Session_Exception('Session namespace must not start with an underscore.');
118  }
119 
120  if (preg_match('#(^[0-9])#i', $namespace[0])) {
124  #require_once 'Zend/Session/Exception.php';
125  throw new Zend_Session_Exception('Session namespace must not start with a number.');
126  }
127 
128  if (isset(self::$_singleInstances[$namespace])) {
132  #require_once 'Zend/Session/Exception.php';
133  throw new Zend_Session_Exception("A session namespace object already exists for this namespace ('$namespace'), and no additional accessors (session namespace objects) for this namespace are permitted.");
134  }
135 
136  if ($singleInstance === true) {
137  self::$_singleInstances[$namespace] = true;
138  }
139 
140  $this->_namespace = $namespace;
141 
142  // Process metadata specific only to this namespace.
143  Zend_Session::start(true); // attempt auto-start (throws exception if strict option set)
144 
145  if (self::$_readable === false) {
149  #require_once 'Zend/Session/Exception.php';
150  throw new Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG);
151  }
152 
153  if (!isset($_SESSION['__ZF'])) {
154  return; // no further processing needed
155  }
156 
157  // do not allow write access to namespaces, after stop() or writeClose()
158  if (parent::$_writable === true) {
159  if (isset($_SESSION['__ZF'][$namespace])) {
160 
161  // Expire Namespace by Namespace Hop (ENNH)
162  if (isset($_SESSION['__ZF'][$namespace]['ENNH'])) {
163  $_SESSION['__ZF'][$namespace]['ENNH']--;
164 
165  if ($_SESSION['__ZF'][$namespace]['ENNH'] === 0) {
166  if (isset($_SESSION[$namespace])) {
167  self::$_expiringData[$namespace] = $_SESSION[$namespace];
168  unset($_SESSION[$namespace]);
169  }
170  unset($_SESSION['__ZF'][$namespace]);
171  }
172  }
173 
174  // Expire Namespace Variables by Namespace Hop (ENVNH)
175  if (isset($_SESSION['__ZF'][$namespace]['ENVNH'])) {
176  foreach ($_SESSION['__ZF'][$namespace]['ENVNH'] as $variable => $hops) {
177  $_SESSION['__ZF'][$namespace]['ENVNH'][$variable]--;
178 
179  if ($_SESSION['__ZF'][$namespace]['ENVNH'][$variable] === 0) {
180  if (isset($_SESSION[$namespace][$variable])) {
181  self::$_expiringData[$namespace][$variable] = $_SESSION[$namespace][$variable];
182  unset($_SESSION[$namespace][$variable]);
183  }
184  unset($_SESSION['__ZF'][$namespace]['ENVNH'][$variable]);
185  }
186  }
187  if(empty($_SESSION['__ZF'][$namespace]['ENVNH'])) {
188  unset($_SESSION['__ZF'][$namespace]['ENVNH']);
189  }
190  }
191  }
192 
193  if (empty($_SESSION['__ZF'][$namespace])) {
194  unset($_SESSION['__ZF'][$namespace]);
195  }
196 
197  if (empty($_SESSION['__ZF'])) {
198  unset($_SESSION['__ZF']);
199  }
200  }
201  }
202 
203 
210  public function getIterator()
211  {
212  return new ArrayObject(parent::_namespaceGetAll($this->_namespace));
213  }
214 
215 
221  public function lock()
222  {
223  self::$_namespaceLocks[$this->_namespace] = true;
224  }
225 
226 
232  public function unlock()
233  {
234  unset(self::$_namespaceLocks[$this->_namespace]);
235  }
236 
237 
243  public static function unlockAll()
244  {
245  self::$_namespaceLocks = array();
246  }
247 
248 
254  public function isLocked()
255  {
256  return isset(self::$_namespaceLocks[$this->_namespace]);
257  }
258 
259 
265  public function unsetAll()
266  {
267  return parent::_namespaceUnset($this->_namespace);
268  }
269 
270 
277  public function & __get($name)
278  {
279  if ($name === '') {
283  #require_once 'Zend/Session/Exception.php';
284  throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
285  }
286 
287  return parent::_namespaceGet($this->_namespace, $name);
288  }
289 
290 
299  public function __set($name, $value)
300  {
301  if (isset(self::$_namespaceLocks[$this->_namespace])) {
305  #require_once 'Zend/Session/Exception.php';
306  throw new Zend_Session_Exception('This session/namespace has been marked as read-only.');
307  }
308 
309  if ($name === '') {
313  #require_once 'Zend/Session/Exception.php';
314  throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
315  }
316 
317  if (parent::$_writable === false) {
321  #require_once 'Zend/Session/Exception.php';
322  throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
323  }
324 
325  $name = (string) $name;
326 
327  $_SESSION[$this->_namespace][$name] = $value;
328  }
329 
330 
342  public function apply($callback)
343  {
344  $arg_list = func_get_args();
345  $arg_list[0] = $_SESSION[$this->_namespace];
346  return call_user_func_array($callback, $arg_list);
347  }
348 
349 
361  public function applySet($callback)
362  {
363  $arg_list = func_get_args();
364  $arg_list[0] = $_SESSION[$this->_namespace];
365  $result = call_user_func_array($callback, $arg_list);
366  if (!is_array($result)) {
370  #require_once 'Zend/Session/Exception.php';
371  throw new Zend_Session_Exception('Result must be an array. Got: ' . gettype($result));
372  }
373  $_SESSION[$this->_namespace] = $result;
374  return $result;
375  }
376 
377 
384  public function __isset($name)
385  {
386  if ($name === '') {
390  #require_once 'Zend/Session/Exception.php';
391  throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
392  }
393 
394  return parent::_namespaceIsset($this->_namespace, $name);
395  }
396 
397 
404  public function __unset($name)
405  {
406  if ($name === '') {
410  #require_once 'Zend/Session/Exception.php';
411  throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
412  }
413 
414  return parent::_namespaceUnset($this->_namespace, $name);
415  }
416 
417 
427  public function setExpirationSeconds($seconds, $variables = null)
428  {
429  if (parent::$_writable === false) {
433  #require_once 'Zend/Session/Exception.php';
434  throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
435  }
436 
437  if ($seconds <= 0) {
441  #require_once 'Zend/Session/Exception.php';
442  throw new Zend_Session_Exception('Seconds must be positive.');
443  }
444 
445  if ($variables === null) {
446 
447  // apply expiration to entire namespace
448  $_SESSION['__ZF'][$this->_namespace]['ENT'] = time() + $seconds;
449 
450  } else {
451 
452  if (is_string($variables)) {
453  $variables = array($variables);
454  }
455 
456  foreach ($variables as $variable) {
457  if (!empty($variable)) {
458  $_SESSION['__ZF'][$this->_namespace]['ENVT'][$variable] = time() + $seconds;
459  }
460  }
461  }
462  }
463 
464 
475  public function setExpirationHops($hops, $variables = null, $hopCountOnUsageOnly = false)
476  {
477  if (parent::$_writable === false) {
481  #require_once 'Zend/Session/Exception.php';
482  throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
483  }
484 
485  if ($hops <= 0) {
489  #require_once 'Zend/Session/Exception.php';
490  throw new Zend_Session_Exception('Hops must be positive number.');
491  }
492 
493  if ($variables === null) {
494 
495  // apply expiration to entire namespace
496  if ($hopCountOnUsageOnly === false) {
497  $_SESSION['__ZF'][$this->_namespace]['ENGH'] = $hops;
498  } else {
499  $_SESSION['__ZF'][$this->_namespace]['ENNH'] = $hops;
500  }
501 
502  } else {
503 
504  if (is_string($variables)) {
505  $variables = array($variables);
506  }
507 
508  foreach ($variables as $variable) {
509  if (!empty($variable)) {
510  if ($hopCountOnUsageOnly === false) {
511  $_SESSION['__ZF'][$this->_namespace]['ENVGH'][$variable] = $hops;
512  } else {
513  $_SESSION['__ZF'][$this->_namespace]['ENVNH'][$variable] = $hops;
514  }
515  }
516  }
517  }
518  }
519 
525  public function getNamespace()
526  {
527  return $this->_namespace;
528  }
529 }
$variable
Definition: variable.php:7
$value
Definition: gender.phtml:16
setExpirationHops($hops, $variables=null, $hopCountOnUsageOnly=false)
Definition: Namespace.php:475
__construct($namespace='Default', $singleInstance=false)
Definition: Namespace.php:102
setExpirationSeconds($seconds, $variables=null)
Definition: Namespace.php:427
__set($name, $value)
Definition: Namespace.php:299
static start($options=false)
Definition: Session.php:421
static resetSingleInstance($namespaceName=null)
Definition: Namespace.php:79
if(!isset($_GET['name'])) $name
Definition: log.php:14