1
|
|
|
<?php namespace Comodojo\Dispatcher\Cache; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
4
|
|
|
use \Comodojo\Dispatcher\Router\Route; |
5
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
6
|
|
|
use \Comodojo\Cache\Cache; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @package Comodojo Dispatcher |
10
|
|
|
* @author Marco Giovinazzi <[email protected]> |
11
|
|
|
* @author Marco Castiello <[email protected]> |
12
|
|
|
* @license GPL-3.0+ |
13
|
|
|
* |
14
|
|
|
* LICENSE: |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class ServerCache extends AbstractCache { |
32
|
|
|
|
33
|
|
|
// @NOTE: Server cache will not consider cacheable POST or PUT requests |
34
|
|
|
// because of dispatcher internal structure: if post request is cached |
35
|
|
|
// subsequent requests will never reach the service. |
36
|
|
|
private static $cachable_methods = array('GET', 'HEAD'); |
37
|
|
|
|
38
|
|
|
private static $cachable_statuses = array(200, 203, 300, 301, 302, 404, 410); |
39
|
|
|
|
40
|
1 |
|
public function read( |
41
|
|
|
Request $request, |
42
|
|
|
Response $response |
43
|
|
|
) { |
44
|
|
|
|
45
|
1 |
|
$name = (string) $request->method . (string) $request->uri; |
|
|
|
|
46
|
|
|
|
47
|
1 |
|
$cache_object = $this->cache->setNamespace('dispatcherservice')->get($name); |
48
|
|
|
|
49
|
1 |
|
if ( is_null($cache_object) ) return false; |
50
|
|
|
|
51
|
|
|
$response->import($cache_object); |
52
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function dump( |
58
|
|
|
Request $request, |
59
|
|
|
Response $response, |
60
|
|
|
Route $route |
61
|
|
|
) { |
62
|
|
|
|
63
|
|
|
$cache = strtoupper($route->getParameter('cache')); |
64
|
|
|
|
65
|
|
|
$ttl = $route->getParameter('ttl'); |
66
|
|
|
|
67
|
|
|
$name = (string) $request->method . (string) $request->uri; |
|
|
|
|
68
|
|
|
|
69
|
|
|
$method = $request->method->get(); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$status = $response->status->get(); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ( |
74
|
|
|
( $cache == 'SERVER' || $cache == 'BOTH' ) && |
75
|
|
|
in_array($method, self::$cachable_methods) && |
76
|
|
|
in_array($status, self::$cachable_statuses) |
77
|
|
|
){ |
78
|
|
|
|
79
|
|
|
$this->cache |
|
|
|
|
80
|
|
|
->setNamespace('dispatcherservice') |
81
|
|
|
->set($name, $response->export(), $ttl === null ? self::DEFAULTTTL : intval($ttl)); |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.