Output.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_Cache
  17. * @subpackage Zend_Cache_Frontend
  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. */
  21. /**
  22. * @see Zend_Cache_Core
  23. */
  24. require_once 'Zend/Cache/Core.php';
  25. /**
  26. * @package Zend_Cache
  27. * @subpackage Zend_Cache_Frontend
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Cache_Frontend_Output extends Zend_Cache_Core
  32. {
  33. private $_idStack = array();
  34. /**
  35. * Constructor
  36. *
  37. * @param array $options Associative array of options
  38. * @return void
  39. */
  40. public function __construct(array $options = array())
  41. {
  42. parent::__construct($options);
  43. $this->_idStack = array();
  44. }
  45. /**
  46. * Start the cache
  47. *
  48. * @param string $id Cache id
  49. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  50. * @param boolean $echoData If set to true, datas are sent to the browser if the cache is hit (simpy returned else)
  51. * @return mixed True if the cache is hit (false else) with $echoData=true (default) ; string else (datas)
  52. */
  53. public function start($id, $doNotTestCacheValidity = false, $echoData = true)
  54. {
  55. $data = $this->load($id, $doNotTestCacheValidity);
  56. if ($data !== false) {
  57. if ( $echoData ) {
  58. echo($data);
  59. return true;
  60. } else {
  61. return $data;
  62. }
  63. }
  64. ob_start();
  65. ob_implicit_flush(false);
  66. $this->_idStack[] = $id;
  67. return false;
  68. }
  69. /**
  70. * Stop the cache
  71. *
  72. * @param array $tags Tags array
  73. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  74. * @param string $forcedDatas If not null, force written datas with this
  75. * @param boolean $echoData If set to true, datas are sent to the browser
  76. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
  77. * @return void
  78. */
  79. public function end($tags = array(), $specificLifetime = false, $forcedDatas = null, $echoData = true, $priority = 8)
  80. {
  81. if ($forcedDatas === null) {
  82. $data = ob_get_contents();
  83. ob_end_clean();
  84. } else {
  85. $data =& $forcedDatas;
  86. }
  87. $id = array_pop($this->_idStack);
  88. if ($id === null) {
  89. Zend_Cache::throwException('use of end() without a start()');
  90. }
  91. $this->save($data, $id, $tags, $specificLifetime, $priority);
  92. if ($echoData) {
  93. echo($data);
  94. }
  95. }
  96. }