Session.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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-2012 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-2012 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. */
  61. public function getSaveHandler()
  62. {
  63. if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
  64. if (is_array($this->_saveHandler)) {
  65. if (!array_key_exists('class', $this->_saveHandler)) {
  66. throw new Zend_Application_Resource_Exception('Session save handler class not provided in options');
  67. }
  68. $options = array();
  69. if (array_key_exists('options', $this->_saveHandler)) {
  70. $options = $this->_saveHandler['options'];
  71. }
  72. $this->_saveHandler = $this->_saveHandler['class'];
  73. $this->_saveHandler = new $this->_saveHandler($options);
  74. } elseif (is_string($this->_saveHandler)) {
  75. $this->_saveHandler = new $this->_saveHandler();
  76. }
  77. if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
  78. throw new Zend_Application_Resource_Exception('Invalid session save handler');
  79. }
  80. }
  81. return $this->_saveHandler;
  82. }
  83. /**
  84. * @return bool
  85. */
  86. protected function _hasSaveHandler()
  87. {
  88. return ($this->_saveHandler !== null);
  89. }
  90. /**
  91. * Defined by Zend_Application_Resource_Resource
  92. *
  93. * @return void
  94. */
  95. public function init()
  96. {
  97. $options = array_change_key_case($this->getOptions(), CASE_LOWER);
  98. if (isset($options['savehandler'])) {
  99. unset($options['savehandler']);
  100. }
  101. if (count($options) > 0) {
  102. Zend_Session::setOptions($options);
  103. }
  104. if ($this->_hasSaveHandler()) {
  105. Zend_Session::setSaveHandler($this->getSaveHandler());
  106. }
  107. }
  108. }