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