composer package updates
[openemr.git] / vendor / zendframework / zend-cache / src / Pattern / OutputCache.php
blob3714141c04af0b71ea233f867251c0fd1c4ba8fc
1 <?php
2 /**
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
8 */
10 namespace Zend\Cache\Pattern;
12 use Zend\Cache\Exception;
14 class OutputCache extends AbstractPattern
16 /**
17 * The key stack
19 * @var array
21 protected $keyStack = [];
23 /**
24 * Set options
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'");
38 return $this;
41 /**
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
47 * @return bool
49 public function start($key)
51 if (($key = (string) $key) === '') {
52 throw new Exception\MissingKeyException('Missing key to read/write output from cache');
55 $success = null;
56 $data = $this->getOptions()->getStorage()->getItem($key, $success);
57 if ($success) {
58 echo $data;
59 return true;
62 ob_start();
63 ob_implicit_flush(0);
64 $this->keyStack[] = $key;
65 return false;
68 /**
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
75 public function end()
77 $key = array_pop($this->keyStack);
78 if ($key === null) {
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);