PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zend-cache/src/Pattern/OutputCache.php

https://gitlab.com/faisaliqbal/mytripsorter
PHP | 89 lines | 43 code | 12 blank | 34 comment | 5 complexity | 59063d3044b802704e8a56eb30136aeb MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Cache\Pattern;
  10. use Zend\Cache\Exception;
  11. class OutputCache extends AbstractPattern
  12. {
  13. /**
  14. * The key stack
  15. *
  16. * @var array
  17. */
  18. protected $keyStack = [];
  19. /**
  20. * Set options
  21. *
  22. * @param PatternOptions $options
  23. * @return OutputCache
  24. * @throws Exception\InvalidArgumentException
  25. */
  26. public function setOptions(PatternOptions $options)
  27. {
  28. parent::setOptions($options);
  29. if (!$options->getStorage()) {
  30. throw new Exception\InvalidArgumentException("Missing option 'storage'");
  31. }
  32. return $this;
  33. }
  34. /**
  35. * if there is a cached item with the given key display it's data and return true
  36. * else start buffering output until end() is called or the script ends.
  37. *
  38. * @param string $key Key
  39. * @throws Exception\MissingKeyException if key is missing
  40. * @return bool
  41. */
  42. public function start($key)
  43. {
  44. if (($key = (string) $key) === '') {
  45. throw new Exception\MissingKeyException('Missing key to read/write output from cache');
  46. }
  47. $success = null;
  48. $data = $this->getOptions()->getStorage()->getItem($key, $success);
  49. if ($success) {
  50. echo $data;
  51. return true;
  52. }
  53. ob_start();
  54. ob_implicit_flush(0);
  55. $this->keyStack[] = $key;
  56. return false;
  57. }
  58. /**
  59. * Stops buffering output, write buffered data to cache using the given key on start()
  60. * and displays the buffer.
  61. *
  62. * @throws Exception\RuntimeException if output cache not started or buffering not active
  63. * @return bool TRUE on success, FALSE on failure writing to cache
  64. */
  65. public function end()
  66. {
  67. $key = array_pop($this->keyStack);
  68. if ($key === null) {
  69. throw new Exception\RuntimeException('Output cache not started');
  70. }
  71. $output = ob_get_flush();
  72. if ($output === false) {
  73. throw new Exception\RuntimeException('Output buffering not active');
  74. }
  75. return $this->getOptions()->getStorage()->setItem($key, $output);
  76. }
  77. }