Session.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Application
  17. * @subpackage Resource
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Application_Resource_ResourceAbstract
  24. */
  25. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  26. /**
  27. * Resource for setting session options
  28. *
  29. * @uses Zend_Application_Resource_ResourceAbstract
  30. * @category Zend
  31. * @package Zend_Application
  32. * @subpackage Resource
  33. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Application_Resource_Session extends Zend_Application_Resource_ResourceAbstract
  37. {
  38. /**
  39. * Save handler to use
  40. *
  41. * @var Zend_Session_SaveHandler_Interface
  42. */
  43. protected $_saveHandler = null;
  44. /**
  45. * Set session save handler
  46. *
  47. * @param array|string|Zend_Session_SaveHandler_Interface $saveHandler
  48. * @return Zend_Application_Resource_Session
  49. * @throws Zend_Application_Resource_Exception When $saveHandler is not a valid save handler
  50. */
  51. public function setSaveHandler($saveHandler)
  52. {
  53. $this->_saveHandler = $saveHandler;
  54. return $this;
  55. }
  56. /**
  57. * Get session save handler
  58. *
  59. * @return Zend_Session_SaveHandler_Interface
  60. * @throws Zend_Application_Resource_Exception
  61. */
  62. public function getSaveHandler()
  63. {
  64. if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
  65. if (is_array($this->_saveHandler)) {
  66. if (!array_key_exists('class', $this->_saveHandler)) {
  67. throw new Zend_Application_Resource_Exception('Session save handler class not provided in options');
  68. }
  69. $options = array();
  70. if (array_key_exists('options', $this->_saveHandler)) {
  71. $options = $this->_saveHandler['options'];
  72. }
  73. $this->_saveHandler = $this->_saveHandler['class'];
  74. $this->_saveHandler = new $this->_saveHandler($options);
  75. } elseif (is_string($this->_saveHandler)) {
  76. $this->_saveHandler = new $this->_saveHandler();
  77. }
  78. if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
  79. throw new Zend_Application_Resource_Exception('Invalid session save handler');
  80. }
  81. }
  82. return $this->_saveHandler;
  83. }
  84. /**
  85. * @return bool
  86. */
  87. protected function _hasSaveHandler()
  88. {
  89. return ($this->_saveHandler !== null);
  90. }
  91. /**
  92. * Defined by Zend_Application_Resource_Resource
  93. *
  94. * @return void
  95. */
  96. public function init()
  97. {
  98. $options = array_change_key_case($this->getOptions(), CASE_LOWER);
  99. if (isset($options['savehandler'])) {
  100. unset($options['savehandler']);
  101. }
  102. if (count($options) > 0) {
  103. Zend_Session::setOptions($options);
  104. }
  105. if ($this->_hasSaveHandler()) {
  106. Zend_Session::setSaveHandler($this->getSaveHandler());
  107. }
  108. }
  109. }