1
|
|
|
<?php namespace Comodojo\Dispatcher\Components; |
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 { |
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
|
|
|
public static function read( |
41
|
|
|
Request $request, |
42
|
|
|
Response $response, |
43
|
|
|
Cache $cache |
44
|
|
|
) { |
45
|
|
|
|
46
|
|
|
$name = (string) $request->method . (string) $this->request->uri; |
|
|
|
|
47
|
|
|
|
48
|
|
|
$cache_object = $this->cache->setNamespace('dispatcherservice')->get($name); |
|
|
|
|
49
|
|
|
|
50
|
|
|
if ( is_null($cache_object) ) return false; |
51
|
|
|
|
52
|
|
|
$response->import($cache); |
53
|
|
|
|
54
|
|
|
return true; |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function dump( |
59
|
|
|
Request $request, |
60
|
|
|
Response $response, |
61
|
|
|
Route $route, |
62
|
|
|
Cache $cache |
|
|
|
|
63
|
|
|
) { |
64
|
|
|
|
65
|
|
|
$cache = strtoupper($route->getParameter('cache')); |
66
|
|
|
|
67
|
|
|
$ttl = $route->getParameter('ttl'); |
68
|
|
|
|
69
|
|
|
$name = (string) $request->method . (string) $request->uri; |
|
|
|
|
70
|
|
|
|
71
|
|
|
$method = $request->method->get(); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$status = $response->status->get(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if ( |
76
|
|
|
( $cache == 'SERVER' || $cache == 'BOTH' ) && |
77
|
|
|
in_array($method, self::$cachable_methods) && |
78
|
|
|
in_array($status, self::$cachable_statuses) |
79
|
|
|
){ |
80
|
|
|
|
81
|
|
|
$cache->setNamespace('dispatcherservice')->set($name, $response->export(), $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.