1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Cache |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Cache\Extension; |
16
|
|
|
|
17
|
|
|
use Phossa2\Cache\CachePool; |
18
|
|
|
use Phossa2\Cache\Message\Message; |
19
|
|
|
use Phossa2\Event\Interfaces\EventInterface; |
20
|
|
|
use Phossa2\Event\EventableExtensionAbstract; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ByPass extension |
24
|
|
|
* |
25
|
|
|
* Bypass the cache if URL has 'nocache=true' set |
26
|
|
|
* |
27
|
|
|
* ```php |
28
|
|
|
* $ext = new ByPass(['trigger' => 'nocache']); |
29
|
|
|
* |
30
|
|
|
* // enable this ext |
31
|
|
|
* $cachePool->addExtension($ext); |
32
|
|
|
* ``` |
33
|
|
|
* |
34
|
|
|
* @package Phossa2\Cache |
35
|
|
|
* @author Hong Zhang <[email protected]> |
36
|
|
|
* @see EventableExtensionAbstract |
37
|
|
|
* @see CachePool |
38
|
|
|
* @version 2.0.1 |
39
|
|
|
* @since 2.0.0 added |
40
|
|
|
* @since 2.0.1 moved to EventableExtensionAbstract |
41
|
|
|
*/ |
42
|
|
|
class ByPass extends EventableExtensionAbstract |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* bypass trigger in the url |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
* @access protected |
49
|
|
|
*/ |
50
|
|
|
protected $trigger = 'nocache'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function methodsAvailable()/*# : array */ |
56
|
|
|
{ |
57
|
|
|
return ['byPassCache']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
|
|
protected function extensionHandles()/*# : array */ |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
['event' => 'cache.*', 'handler' => ['byPassCache', 100]] |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Skip the cache |
72
|
|
|
* |
73
|
|
|
* 1. $this->trigger = '', always bypass the cache |
74
|
|
|
* 2. if sees $this->trigger in $_REQUEST, bypass the cache |
75
|
|
|
* |
76
|
|
|
* @param EventInterface $event |
77
|
|
|
* @return bool |
78
|
|
|
* @access public |
79
|
|
|
*/ |
80
|
|
|
public function byPassCache(EventInterface $event)/*# : bool */ |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
/* @var CachePool $pool */ |
83
|
|
|
$pool = $event->getTarget(); |
84
|
|
|
|
85
|
|
|
if ($this->trigger === '' || isset($_REQUEST[$this->trigger])) { |
86
|
|
|
return $pool->setError( |
87
|
|
|
Message::get(Message::CACHE_EXT_BYPASS), |
88
|
|
|
Message::CACHE_EXT_BYPASS |
89
|
|
|
); |
90
|
|
|
} else { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: