/Zend/Application/Resource/Session.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 125 lines · 53 code · 11 blank · 61 comment · 8 complexity · d80b8fd9848b71115369f75c57a9a505 MD5 · raw file

  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Session.php 20814 2010-02-01 20:13:08Z freak $
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\Application\Resource;
  26. use Zend\Session\SaveHandler;
  27. use Zend\Session;
  28. /**
  29. * @see Zend_Application_Resource_ResourceAbstract
  30. */
  31. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  32. /**
  33. * Resource for setting session options
  34. *
  35. * @uses Zend_Application_Resource_ResourceAbstract
  36. * @category Zend
  37. * @package Zend_Application
  38. * @subpackage Resource
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Session extends ResourceAbstract
  43. {
  44. /**
  45. * Save handler to use
  46. *
  47. * @var Zend_Session_SaveHandler_Interface
  48. */
  49. protected $_saveHandler = null;
  50. /**
  51. * Set session save handler
  52. *
  53. * @param array|string|Zend_Session_SaveHandler_Interface $saveHandler
  54. * @return Zend_Application_Resource_Session
  55. * @throws Zend_Application_Resource_Exception When $saveHandler is no valid save handler
  56. */
  57. public function setSaveHandler($saveHandler)
  58. {
  59. $this->_saveHandler = $saveHandler;
  60. return $this;
  61. }
  62. /**
  63. * Get session save handler
  64. *
  65. * @return Zend_Session_SaveHandler_Interface
  66. */
  67. public function getSaveHandler()
  68. {
  69. if (!$this->_saveHandler instanceof SaveHandler\SaveHandlerInterface) {
  70. if (is_array($this->_saveHandler)) {
  71. if (!array_key_exists('class', $this->_saveHandler)) {
  72. throw new Exception('Session save handler class not provided in options');
  73. }
  74. $options = array();
  75. if (array_key_exists('options', $this->_saveHandler)) {
  76. $options = $this->_saveHandler['options'];
  77. }
  78. $this->_saveHandler = $this->_saveHandler['class'];
  79. $this->_saveHandler = new $this->\\saveHandler($options);
  80. } elseif (is_string($this->_saveHandler)) {
  81. $this->_saveHandler = new $this->\\saveHandler();
  82. }
  83. if (!$this->_saveHandler instanceof SaveHandler\SaveHandlerInterface) {
  84. throw new Exception('Invalid session save handler');
  85. }
  86. }
  87. return $this->_saveHandler;
  88. }
  89. /**
  90. * @return bool
  91. */
  92. protected function _hasSaveHandler()
  93. {
  94. return ($this->_saveHandler !== null);
  95. }
  96. /**
  97. * Defined by Zend_Application_Resource_Resource
  98. *
  99. * @return void
  100. */
  101. public function init()
  102. {
  103. $options = array_change_key_case($this->getOptions(), CASE_LOWER);
  104. if (isset($options['savehandler'])) {
  105. unset($options['savehandler']);
  106. }
  107. if (count($options) > 0) {
  108. Session\Session::setOptions($options);
  109. }
  110. if ($this->_hasSaveHandler()) {
  111. Session\Session::setSaveHandler($this->getSaveHandler());
  112. }
  113. }
  114. }