Session.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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-2009 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. * Resource for setting session options
  24. *
  25. * @uses Zend_Application_Resource_ResourceAbstract
  26. * @category Zend
  27. * @package Zend_Application
  28. * @subpackage Resource
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Application_Resource_Session extends Zend_Application_Resource_ResourceAbstract
  33. {
  34. /**
  35. * Save handler to use
  36. *
  37. * @var Zend_Session_SaveHandler_Interface
  38. */
  39. protected $_saveHandler = null;
  40. /**
  41. * Set session save handler
  42. *
  43. * @param array|string|Zend_Session_SaveHandler_Interface $saveHandler
  44. * @return Zend_Application_Resource_Session
  45. * @throws Zend_Application_Resource_Exception When $saveHandler is no valid save handler
  46. */
  47. public function setSaveHandler($saveHandler)
  48. {
  49. $this->_saveHandler = $saveHandler;
  50. return $this;
  51. }
  52. /**
  53. * Get session save handler
  54. *
  55. * @return Zend_Session_SaveHandler_Interface
  56. */
  57. public function getSaveHandler()
  58. {
  59. if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
  60. if (is_array($this->_saveHandler)) {
  61. if (!array_key_exists('class', $this->_saveHandler)) {
  62. throw new Zend_Application_Resource_Exception('Session save handler class not provided in options');
  63. }
  64. if (array_key_exists('options', $this->_saveHandler)) {
  65. $options = $this->_saveHandler['options'];
  66. }
  67. $this->_saveHandler = $this->_saveHandler['class'];
  68. $this->_saveHandler = new $this->_saveHandler($options);
  69. } elseif (is_string($this->_saveHandler)) {
  70. $this->_saveHandler = new $this->_saveHandler();
  71. }
  72. if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
  73. throw new Zend_Application_Resource_Exception('Invalid session save handler');
  74. }
  75. }
  76. return $this->_saveHandler;
  77. }
  78. /**
  79. * Defined by Zend_Application_Resource_Resource
  80. *
  81. * @return void
  82. */
  83. public function init()
  84. {
  85. $options = array_change_key_case($this->getOptions(), CASE_LOWER);
  86. if (isset($options['savehandler'])) {
  87. unset($options['savehandler']);
  88. }
  89. if (count($options) > 0) {
  90. Zend_Session::setOptions($options);
  91. }
  92. if ($this->_saveHandler !== null) {
  93. Zend_Session::setSaveHandler($this->getSaveHandler());
  94. }
  95. }
  96. }