Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Hoa\Session\Session Class Reference
Inheritance diagram for Hoa\Session\Session:

Public Member Functions

 __construct ($namespace= '_default', $cache=null, $cacheExpire=null)
 
 getNamespace ()
 
 isEmpty ()
 
 isExpired ()
 
 hasExpired ($exception=true)
 
 isLocked ()
 
 getProfile ()
 
 rememberMe ($modify)
 
 forgetMe ()
 
 offsetExists ($offset)
 
 offsetGet ($offset)
 
 offsetSet ($offset, $value)
 
 offsetUnset ($offset)
 
 count ()
 
 getIterator ()
 
 writeAndClose ()
 
 clean ()
 
 delete ()
 

Static Public Member Functions

static start ($cache=null, $cacheExpire=null)
 
static isStarted ()
 
static destroy ()
 
static getId ()
 
static newId ($deleteOldSession=false)
 

Public Attributes

const EVENT_CHANNEL = 'hoa://Event/Session/'
 
const TOP_NAMESPACE = '__Hoa__'
 
const PROFILE = 0
 
const BUCKET = 1
 
const NO_CACHE = 'nocache'
 
const CACHE_PUBLIC = 'public'
 
const CACHE_PRIVATE = 'private'
 
const CACHE_PRIVATE_NO_EXPIRE = 'private_no_expire'
 

Protected Member Functions

 initialize ($reset=false)
 

Protected Attributes

 $_namespace = null
 
 $_profile = null
 
 $_bucket = null
 

Static Protected Attributes

static $_started = false
 
static $_lock = []
 

Detailed Description

Class .

Object represents a “namespace” in a session, i.e. an entry in the $_SESSION global array. Class represents some useful operations (or aliases) on sessions.

Definition at line 52 of file Session.php.

Constructor & Destructor Documentation

Hoa\Session\Session::__construct (   $namespace = '_default',
  $cache = null,
  $cacheExpire = null 
)

Manipulate a namespace. If session has not been previously started, it will be done automatically.

Parameters
string$namespaceNamespace.
string$cacheCache value (please, see static::CACHE constants).
int$cacheExpireCache expire (in seconds).
Returns
void
Exceptions

Definition at line 167 of file Session.php.

171  {
172  if (false !== strpos($namespace, '/')) {
173  throw new Exception(
174  'Namespace must not contain a slash (/); given %s.',
175  0,
176  $namespace
177  );
178  }
179 
180  $this->_namespace = $namespace;
181 
182  if (false === array_key_exists($namespace, static::$_lock)) {
183  static::$_lock[$namespace] = false;
184  }
185 
186  if (true === $this->isLocked()) {
187  throw new Exception\Locked(
188  'Namespace %s is locked because it has been unset.',
189  1,
190  $namespace
191  );
192  }
193 
194  static::start($cache, $cacheExpire);
195  $this->initialize();
196 
197  $channel = static::EVENT_CHANNEL . $namespace;
198  $expired = $channel . ':expired';
199 
200  if (false === Core\Event::eventExists($channel)) {
201  Core\Event::register($channel, 'Hoa\Session');
202  }
203 
204  if (false === Core\Event::eventExists($expired)) {
205  Core\Event::register($expired, 'Hoa\Session');
206  }
207 
208  if (true === $this->isExpired()) {
209  $this->hasExpired();
210  }
211 
212  $this->_profile['last_used']->setTimestamp(time());
213 
214  return;
215  }
hasExpired($exception=true)
Definition: Session.php:375
initialize($reset=false)
Definition: Session.php:279

Here is the call graph for this function:

Member Function Documentation

Hoa\Session\Session::clean ( )

Remove all data from the namespace. It does not touch the profile or the session, only the data.

Returns
void

Definition at line 632 of file Session.php.

633  {
634  $this->_bucket = [];
635 
636  return;
637  }
Hoa\Session\Session::count ( )

Count number of data.

Returns
int

Definition at line 573 of file Session.php.

574  {
575  if (true === $this->isLocked()) {
576  return 0;
577  }
578 
579  return count($this->_bucket);
580  }

Here is the call graph for this function:

Hoa\Session\Session::delete ( )

Destroy the namespace. The namespace will be locked and considered as expired (only the event will be fired, not the exception).

Returns
void

Definition at line 646 of file Session.php.

647  {
648  $namespace = $this->getNamespace();
649  $channel = static::EVENT_CHANNEL . $namespace;
650  $this->hasExpired(false);
651  unset($_SESSION[static::TOP_NAMESPACE][$namespace]);
652  Core\Event::unregister($channel);
653  Core\Event::unregister($channel . ':expired');
654  static::$_lock[$namespace] = true;
655 
656  return;
657  }
hasExpired($exception=true)
Definition: Session.php:375

Here is the call graph for this function:

static Hoa\Session\Session::destroy ( )
static

Destroy the session (including all namespaces and cookie). If session has not been previously started, it will be done automatically. It won't modify current lock, be careful.

Returns
void
Exceptions

Definition at line 667 of file Session.php.

668  {
669  static::start();
670 
671  if (true == ini_get('session.use_cookies')) {
672  if (headers_sent($filename, $line)) {
673  throw new Exception(
674  'Headers have been already sent, cannot destroy cookie; ' .
675  'output started in %s at line %d.',
676  14,
677  [$filename, $line]
678  );
679  }
680 
681  $parameters = session_get_cookie_params();
682  setcookie(
683  session_name(),
684  '',
685  time() - 1,
686  $parameters['path'],
687  $parameters['domain'],
688  $parameters['secure'],
689  $parameters['httponly']
690  );
691  }
692 
693  session_destroy();
694  static::$_started = false;
695  // let locks unchanged.
696 
697  return;
698  }
Hoa\Session\Session::forgetMe ( )

Set lifetime to 0. This method is different from self::hasExpired() because it will only modify the lifetime and it will not throw or fire anything.

Returns

Definition at line 466 of file Session.php.

467  {
468  if (true === $this->isLocked()) {
469  return null;
470  }
471 
472  return $this->_profile['lifetime']->setTimestamp(time() - 1);
473  }

Here is the call graph for this function:

static Hoa\Session\Session::getId ( )
static

Get current session ID.

Returns
string

Definition at line 705 of file Session.php.

706  {
707  return session_id();
708  }
Hoa\Session\Session::getIterator ( )

Iterate over data in the namespace.

Returns
Exceptions

Definition at line 588 of file Session.php.

589  {
590  if (true === $this->isLocked()) {
591  throw new Exception\Locked(
592  'Namespace %s is locked because it has been unset.',
593  12,
594  $this->getNamespace()
595  );
596  }
597 
598  return new Iterator\Map($this->_bucket);
599  }

Here is the call graph for this function:

Hoa\Session\Session::getNamespace ( )

Get current namespace.

Returns
string

Definition at line 312 of file Session.php.

313  {
314  return $this->_namespace;
315  }

Here is the caller graph for this function:

Hoa\Session\Session::getProfile ( )

Get profile of the namespace.

Returns
array
Exceptions

Definition at line 425 of file Session.php.

426  {
427  if (true === $this->isLocked()) {
428  throw new Exception\Locked(
429  'Namespace %s is locked because it has been unset.',
430  6,
431  $this->getNamespace()
432  );
433  }
434 
435  return $this->_profile;
436  }

Here is the call graph for this function:

Hoa\Session\Session::hasExpired (   $exception = true)

Declare the session as “expired”. It will fire an event on static::EVENT_CHANNEL . $namespace . ':expired' if this channel is listened, else it will throw an exception. Moreover, it will re-initialize the namespace.

Parameters
bool$exceptionWhether throw an exception if needed or not.
Returns
void
Exceptions

Definition at line 375 of file Session.php.

376  {
377  $namespace = $this->getNamespace();
378 
379  if (true === $this->isLocked()) {
380  throw new Exception\Locked(
381  'Namespace %s is locked because it has been unset.',
382  4,
383  $namespace
384  );
385  }
386 
387  $this->initialize(true);
388  $expired = static::EVENT_CHANNEL . $namespace . ':expired';
389 
390  if (true === $exception &&
391  false === event($expired)->isListened()) {
392  throw new Exception\Expired(
393  'Namespace %s has expired. All data belonging to this ' .
394  'namespace are lost.',
395  5,
396  $namespace
397  );
398  }
399 
400  Core\Event::notify(
401  $expired,
402  $this,
403  new Core\Event\Bucket()
404  );
405 
406  return;
407  }
initialize($reset=false)
Definition: Session.php:279

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Session\Session::initialize (   $reset = false)
protected

Initialize the namespace.

Parameters
bool$resetRe-initialize.
Returns
void

Definition at line 279 of file Session.php.

280  {
281  $namespace = $this->getNamespace();
282 
283  if (true === $reset) {
284  unset($_SESSION[static::TOP_NAMESPACE][$namespace]);
285  }
286 
287  if (!isset($_SESSION[static::TOP_NAMESPACE][$namespace])) {
288  $_SESSION[static::TOP_NAMESPACE][$namespace] = [
289  static::PROFILE => [
290  'started' => new \DateTime(),
291  'last_used' => new \DateTime(),
292  'lifetime' => new \DateTime(
293  '+' . ini_get('session.gc_maxlifetime') . ' second'
294  )
295  ],
296  static::BUCKET => []
297  ];
298  }
299 
300  $handle = &$_SESSION[static::TOP_NAMESPACE][$namespace];
301  $this->_profile = &$handle[static::PROFILE];
302  $this->_bucket = &$handle[static::BUCKET];
303 
304  return;
305  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Session\Session::isEmpty ( )

Check if the namespace is empty, i.e. if it does not contain data.

Returns
bool

Definition at line 332 of file Session.php.

333  {
334  if (true === $this->isLocked()) {
335  $this->__destruct();
336  }
337 
338  return empty($this->_bucket);
339  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Session\Session::isExpired ( )

Check if the namespace is expired or not (test the lifetime).

Returns
bool

Definition at line 346 of file Session.php.

347  {
348  if (true === $this->isLocked()) {
349  $this->__destruct();
350 
351  return true;
352  }
353 
354  $lifetime = $this->_profile['lifetime'];
355  $current = new \DateTime();
356 
357  if ($lifetime > $current) {
358  return false;
359  }
360 
361  return true;
362  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Session\Session::isLocked ( )

Check if the namespace is not locked.

Returns
bool

Definition at line 414 of file Session.php.

415  {
416  return static::$_lock[$this->getNamespace()];
417  }

Here is the call graph for this function:

Here is the caller graph for this function:

static Hoa\Session\Session::isStarted ( )
static

Check if the session is started or not.

Returns
bool

Definition at line 322 of file Session.php.

323  {
324  return static::$_started;
325  }
static Hoa\Session\Session::newId (   $deleteOldSession = false)
static

Update the current session ID with a newly generated one.

Parameters
bool$deleteOldSessionDelete the old session file or not.
Returns
bool

Definition at line 716 of file Session.php.

717  {
718  return session_regenerate_id($deleteOldSession);
719  }
Hoa\Session\Session::offsetExists (   $offset)

Check if a data exists.

Parameters
mixed$offsetData name.
Returns
bool
Exceptions

Definition at line 482 of file Session.php.

483  {
484  if (true === $this->isLocked()) {
485  throw new Exception\Locked(
486  'Namespace %s is locked because it has been unset.',
487  8,
488  $this->getNamespace()
489  );
490  }
491 
492  return array_key_exists($offset, $this->_bucket);
493  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Session\Session::offsetGet (   $offset)

Get a data.

Parameters
mixed$offsetData name.
Returns
mixed
Exceptions

Definition at line 502 of file Session.php.

503  {
504  if (true === $this->isLocked()) {
505  throw new Exception\Locked(
506  'Namespace %s is locked because it has been unset.',
507  9,
508  $this->getNamespace()
509  );
510  }
511 
512  if (false === $this->offsetExists($offset)) {
513  return null;
514  }
515 
516  return $this->_bucket[$offset];
517  }
offsetExists($offset)
Definition: Session.php:482

Here is the call graph for this function:

Hoa\Session\Session::offsetSet (   $offset,
  $value 
)

Set a data.

Parameters
mixed$offsetData name.
mixed$valueData value.
Returns
Exceptions

Definition at line 527 of file Session.php.

528  {
529  if (true === $this->isLocked()) {
530  throw new Exception\Locked(
531  'Namespace %s is locked because it has been unset.',
532  10,
533  $this->getNamespace()
534  );
535  }
536 
537  if (null === $offset) {
538  $this->_bucket[] = $value;
539  } else {
540  $this->_bucket[$offset] = $value;
541  }
542 
543  return $this;
544  }

Here is the call graph for this function:

Hoa\Session\Session::offsetUnset (   $offset)

Unset a data.

Parameters
mixed$offsetData name.
Returns
void
Exceptions

Definition at line 553 of file Session.php.

554  {
555  if (true === $this->isLocked()) {
556  throw new Exception\Locked(
557  'Namespace %s is locked because it has been unset.',
558  11,
559  $this->getNamespace()
560  );
561  }
562 
563  unset($this->_bucket[$offset]);
564 
565  return;
566  }

Here is the call graph for this function:

Hoa\Session\Session::rememberMe (   $modify)

Modify the lifetime of the namespace. Reference value: session.gc_maxlifetime seconds.

Parameters
string$modifyPlease, see ::modify().
Returns
Exceptions

Definition at line 446 of file Session.php.

447  {
448  if (true === $this->isLocked()) {
449  throw new Exception\Locked(
450  'Namespace %s is locked because it has been unset.',
451  7,
452  $this->getNamespace()
453  );
454  }
455 
456  return $this->_profile['lifetime']->modify($modify);
457  }

Here is the call graph for this function:

static Hoa\Session\Session::start (   $cache = null,
  $cacheExpire = null 
)
static

Start the session.

Parameters
string$cacheCache value (please, see static::CACHE constants).
int$cacheExpireCache expire (in seconds).
Returns
void
Exceptions

Definition at line 226 of file Session.php.

227  {
228  if (null === $cache) {
229  $cache = session_cache_limiter();
230  }
231 
232  if (null === $cacheExpire) {
233  $cacheExpire = session_cache_expire();
234  }
235 
236  if (true === static::$_started) {
237  return;
238  }
239 
240  if (headers_sent($filename, $line)) {
241  throw new Exception(
242  'Session must be started before any ouput; ' .
243  'output started in %s at line %d.',
244  2,
245  [$filename, $line]
246  );
247  }
248 
249  if (false === defined('SID')) {
250  session_cache_limiter($cache);
251 
252  if (static::NO_CACHE !== $cache) {
253  session_cache_expire($cacheExpire);
254  }
255 
256  if (false === session_start()) {
257  throw new Exception(
258  'Error when starting session. Cannot send session cookie.',
259  3
260  );
261  }
262  }
263 
264  static::$_started = true;
265 
266  if (!isset($_SESSION[static::TOP_NAMESPACE])) {
267  $_SESSION[static::TOP_NAMESPACE] = [];
268  }
269 
270  return;
271  }
Hoa\Session\Session::writeAndClose ( )

Write and close the session (including all namespaces).

Returns
bool
Exceptions

Definition at line 607 of file Session.php.

608  {
609  if (true === $this->isLocked()) {
610  throw new Exception\Locked(
611  'Namespace %s is locked because it has been unset.',
612  13,
613  $this->getNamespace()
614  );
615  }
616 
617  if (false === static::$_started) {
618  return false;
619  }
620 
621  session_write_close();
622 
623  return true;
624  }

Here is the call graph for this function:

Member Data Documentation

Hoa\Session\Session::$_bucket = null
protected

Definition at line 143 of file Session.php.

Hoa\Session\Session::$_lock = []
staticprotected

Definition at line 150 of file Session.php.

Hoa\Session\Session::$_namespace = null
protected

Definition at line 129 of file Session.php.

Hoa\Session\Session::$_profile = null
protected

Definition at line 136 of file Session.php.

Hoa\Session\Session::$_started = false
staticprotected

Definition at line 122 of file Session.php.

const Hoa\Session\Session::BUCKET = 1

Data bucket of the namespace, i.e. $_SESSION[static::TOP_NAMESPACE][<namespace>][static::BUCKET].

string

Definition at line 87 of file Session.php.

const Hoa\Session\Session::CACHE_PRIVATE = 'private'

HTTP cache control: private.

string

Definition at line 108 of file Session.php.

const Hoa\Session\Session::CACHE_PRIVATE_NO_EXPIRE = 'private_no_expire'

HTTP cache control: private and no expiration.

string

Definition at line 115 of file Session.php.

const Hoa\Session\Session::CACHE_PUBLIC = 'public'

HTTP cache control: public.

string

Definition at line 101 of file Session.php.

const Hoa\Session\Session::EVENT_CHANNEL = 'hoa://Event/Session/'

Event channel.

string

Definition at line 63 of file Session.php.

const Hoa\Session\Session::NO_CACHE = 'nocache'

HTTP cache control: no cache.

string

Definition at line 94 of file Session.php.

const Hoa\Session\Session::PROFILE = 0

Profile of the namespace, i.e. $_SESSION[static::TOP_NAMESPACE][<namespace>][static::PROFILE].

string

Definition at line 79 of file Session.php.

const Hoa\Session\Session::TOP_NAMESPACE = '__Hoa__'

Top-namespace: entry where namespaces are located in $_SESSION, i.e. $_SESSION[static::TOP_NAMESPACE][<namespace>].

string

Definition at line 71 of file Session.php.


The documentation for this class was generated from the following file: