Session.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. use Pimf\Session\Payload;
  10. use Pimf\Session\Storages as Storage;
  11. /**
  12. * Using the session
  13. *
  14. * <code>
  15. *
  16. * // Retrieve the session instance and get an item
  17. * Session::instance()->get('name');
  18. *
  19. * // Retrieve the session instance and place an item in the session
  20. * Session::instance()->put('name', 'Robin');
  21. *
  22. * // Retrieve a value from the session
  23. * $value = Session::get('name');
  24. *
  25. * // Write a value to the session storage
  26. * $value = Session::put('name', 'Robin');
  27. *
  28. * // Equivalent statement using the "instance" method
  29. * $value = Session::instance()->put('name', 'Robin');
  30. *
  31. * </code>
  32. *
  33. * @package Pimf
  34. * @author Gjero Krsteski <[email protected]>
  35. *
  36. * @method static save()
  37. */
  38. class Session
  39. {
  40. /**
  41. * The session singleton instance for the request.
  42. *
  43. * @var Payload
  44. */
  45. public static $instance;
  46. /**
  47. * The string name of the CSRF token stored in the session.
  48. *
  49. * @var string
  50. */
  51. const CSRF = 'csrf_token';
  52. /**
  53. * Create the session payload and load the session.
  54. *
  55. * @return void
  56. */
  57. public static function load()
  58. {
  59. $conf = Registry::get('conf');
  60. static::start($conf['session']['storage']);
  61. static::$instance->load(Cookie::get($conf['session']['cookie']));
  62. }
  63. /**
  64. * Create the session payload instance for the request.
  65. *
  66. * @param string $storage
  67. *
  68. * @return void
  69. */
  70. public static function start($storage)
  71. {
  72. static::$instance = new Payload(static::factory($storage));
  73. }
  74. /**
  75. * Create a new session storage instance.
  76. *
  77. * @param string $storage
  78. *
  79. * @return Storage\Storage
  80. * @throws \RuntimeException
  81. */
  82. public static function factory($storage)
  83. {
  84. $conf = Registry::get('conf');
  85. switch ($storage) {
  86. case 'apc':
  87. return new Storage\Apc(Cache::storage('apc'));
  88. case 'cookie':
  89. return new Storage\Cookie();
  90. case 'file':
  91. return new Storage\File($conf['session']['storage_path']);
  92. case 'pdo':
  93. return new Storage\Pdo(Pdo\Factory::get($conf['session']['database']));
  94. case 'memcached':
  95. return new Storage\Memcached(Cache::storage('memcached'));
  96. case 'memory':
  97. return new Storage\Memory();
  98. case 'redis':
  99. return new Storage\Redis(Cache::storage('redis'));
  100. case 'dba':
  101. return new Storage\Dba(Cache::storage('dba'));
  102. default:
  103. throw new \RuntimeException("Session storage [$storage] is not supported.");
  104. }
  105. }
  106. /**
  107. * Retrieve the active session payload instance for the request.
  108. *
  109. * @return Payload
  110. * @throws \RuntimeException
  111. */
  112. public static function instance()
  113. {
  114. if (static::started()) {
  115. return static::$instance;
  116. }
  117. throw new \RuntimeException("A storage must be set before using the session.");
  118. }
  119. /**
  120. * Determine if session handling has been started for the request.
  121. *
  122. * @return bool
  123. */
  124. public static function started()
  125. {
  126. return (static::$instance !== null);
  127. }
  128. /**
  129. * Magic Method for calling the methods on the session singleton instance.
  130. *
  131. * @param $method
  132. * @param $parameters
  133. *
  134. * @return mixed
  135. */
  136. public static function __callStatic($method, $parameters)
  137. {
  138. return call_user_func_array(
  139. array(static::instance(), $method), $parameters
  140. );
  141. }
  142. }