3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Cache\Pattern
;
12 use Zend\Cache\Exception
;
14 class OutputCache
extends AbstractPattern
21 protected $keyStack = [];
26 * @param PatternOptions $options
27 * @return OutputCache Provides a fluent interface
28 * @throws Exception\InvalidArgumentException
30 public function setOptions(PatternOptions
$options)
32 parent
::setOptions($options);
34 if (! $options->getStorage()) {
35 throw new Exception\
InvalidArgumentException("Missing option 'storage'");
42 * if there is a cached item with the given key display it's data and return true
43 * else start buffering output until end() is called or the script ends.
45 * @param string $key Key
46 * @throws Exception\MissingKeyException if key is missing
49 public function start($key)
51 if (($key = (string) $key) === '') {
52 throw new Exception\
MissingKeyException('Missing key to read/write output from cache');
56 $data = $this->getOptions()->getStorage()->getItem($key, $success);
64 $this->keyStack
[] = $key;
69 * Stops buffering output, write buffered data to cache using the given key on start()
70 * and displays the buffer.
72 * @throws Exception\RuntimeException if output cache not started or buffering not active
73 * @return bool TRUE on success, FALSE on failure writing to cache
77 $key = array_pop($this->keyStack
);
79 throw new Exception\
RuntimeException('Output cache not started');
82 $output = ob_get_flush();
83 if ($output === false) {
84 throw new Exception\
RuntimeException('Output buffering not active');
87 return $this->getOptions()->getStorage()->setItem($key, $output);