sfStorage.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfStorage allows you to customize the way symfony stores its persistent data.
  12. *
  13. * @package symfony
  14. * @subpackage storage
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author Sean Kerr <sean@code-box.org>
  17. * @version SVN: $Id: sfStorage.class.php 9942 2008-06-27 18:00:49Z fabien $
  18. */
  19. abstract class sfStorage
  20. {
  21. protected
  22. $options = array();
  23. /**
  24. * Class constructor.
  25. *
  26. * @see initialize()
  27. */
  28. public function __construct($options = array())
  29. {
  30. $this->initialize($options);
  31. if ($this->options['auto_shutdown'])
  32. {
  33. register_shutdown_function(array($this, 'shutdown'));
  34. }
  35. }
  36. /**
  37. * Initializes this Storage instance.
  38. *
  39. * Available options:
  40. *
  41. * * auto_shutdown: Whether to automatically save the changes to the session (true by default)
  42. *
  43. * @param array $options An associative array of options
  44. *
  45. * @return bool true, if initialization completes successfully, otherwise false
  46. *
  47. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfStorage
  48. */
  49. public function initialize($options = array())
  50. {
  51. $this->options = array_merge(array(
  52. 'auto_shutdown' => true,
  53. ), $options);
  54. }
  55. /**
  56. * Returns the option array.
  57. *
  58. * @return array The array of options
  59. */
  60. public function getOptions()
  61. {
  62. return $this->options;
  63. }
  64. /**
  65. * Reads data from this storage.
  66. *
  67. * The preferred format for a key is directory style so naming conflicts can be avoided.
  68. *
  69. * @param string $key A unique key identifying your data
  70. *
  71. * @return mixed Data associated with the key
  72. *
  73. * @throws <b>sfStorageException</b> If an error occurs while reading data from this storage
  74. */
  75. abstract public function read($key);
  76. /**
  77. * Regenerates id that represents this storage.
  78. *
  79. * @param boolean destroy Destroy session when regenerating?
  80. *
  81. * @return boolean True if session regenerated, false if error
  82. *
  83. * @throws <b>sfStorageException</b> If an error occurs while regenerating this storage
  84. */
  85. abstract public function regenerate($destroy = false);
  86. /**
  87. * Removes data from this storage.
  88. *
  89. * The preferred format for a key is directory style so naming conflicts can be avoided.
  90. *
  91. * @param string $key A unique key identifying your data
  92. *
  93. * @return mixed Data associated with the key
  94. *
  95. * @throws <b>sfStorageException</b> If an error occurs while removing data from this storage
  96. */
  97. abstract public function remove($key);
  98. /**
  99. * Executes the shutdown procedure.
  100. *
  101. * @throws <b>sfStorageException</b> If an error occurs while shutting down this storage
  102. */
  103. abstract public function shutdown();
  104. /**
  105. * Writes data to this storage.
  106. *
  107. * The preferred format for a key is directory style so naming conflicts can be avoided.
  108. *
  109. * @param string $key A unique key identifying your data
  110. * @param mixed $data Data associated with your key
  111. *
  112. * @throws <b>sfStorageException</b> If an error occurs while writing to this storage
  113. */
  114. abstract public function write($key, $data);
  115. }