1
|
|
|
<?php |
2
|
|
|
namespace Aoe\Restler\System\TYPO3; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2016 AOE GmbH <[email protected]> |
8
|
|
|
* |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use TYPO3\CMS\Core\Cache\CacheManager; |
29
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; |
30
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @package Restler |
34
|
|
|
*/ |
35
|
|
|
class Cache implements SingletonInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* This is the phpdoc-tag, which defines, that the response of API-method is cacheable via TYPO3-caching-framework |
39
|
|
|
* |
40
|
|
|
* Syntax: |
41
|
|
|
* The PHPdoc-comment must look like this: |
42
|
|
|
* @restler_typo3cache_expires [expires-in-seconds] |
43
|
|
|
* |
44
|
|
|
* Examples: |
45
|
|
|
* When API-method should be cacheable in TYPO3 for 10 minutes, than the PHPdoc-comment must look like this: |
46
|
|
|
* @restler_typo3cache_expires 600 |
47
|
|
|
* When API-method should be cacheable in TYPO3 for endless time, than the PHPdoc-comment must look like this: |
48
|
|
|
* @restler_typo3cache_expires 0 |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
const API_METHOD_TYPO3CACHE_EXPIRES = 'restler_typo3cache_expires'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* This is the phpdoc-tag, which defines, that the cached response of API-method should be tagged with given tags |
56
|
|
|
* |
57
|
|
|
* Syntax: |
58
|
|
|
* The PHPdoc-comment must look like this: |
59
|
|
|
* @restler_typo3cache_tags [comma-separated-list-of-tags] |
60
|
|
|
* |
61
|
|
|
* Example: |
62
|
|
|
* When response of API-method should be tagged with 'tag_a' and 'tag_b', than the PHPdoc-comment must look like this: |
63
|
|
|
* @restler_typo3cache_tags tag_a,tag_b |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
const API_METHOD_TYPO3CACHE_TAGS = 'restler_typo3cache_tags'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var FrontendInterface |
71
|
|
|
*/ |
72
|
|
|
private $cache; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param CacheManager $cacheManager |
76
|
|
|
*/ |
77
|
8 |
|
public function __construct(CacheManager $cacheManager) |
78
|
|
|
{ |
79
|
8 |
|
$this->cache = $cacheManager->getCache('cache_restler'); |
80
|
8 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $requestMethod |
84
|
|
|
* @param array $apiMethodInfoMetadata |
85
|
|
|
* @return boolean |
86
|
|
|
*/ |
87
|
3 |
|
public function isResponseCacheableByTypo3Cache($requestMethod, array $apiMethodInfoMetadata) |
88
|
|
|
{ |
89
|
3 |
|
if ($requestMethod === 'GET' && |
90
|
3 |
|
array_key_exists(self::API_METHOD_TYPO3CACHE_EXPIRES, $apiMethodInfoMetadata) && |
91
|
3 |
|
array_key_exists(self::API_METHOD_TYPO3CACHE_TAGS, $apiMethodInfoMetadata)) { |
92
|
1 |
|
return true; |
93
|
|
|
} |
94
|
2 |
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* cache response |
99
|
|
|
* |
100
|
|
|
* @param int $responseCode |
101
|
|
|
* @param string $requestUri |
102
|
|
|
* @param array $requestGetData |
103
|
|
|
* @param array $apiMethodInfoMetadata |
104
|
|
|
* @param string $responseData |
105
|
|
|
* @param $responseFormatClass |
106
|
|
|
* @param array $responseHeaders |
107
|
|
|
*/ |
108
|
1 |
|
public function cacheResponseByTypo3Cache( |
109
|
|
|
$responseCode, |
110
|
|
|
$requestUri, |
111
|
|
|
array $requestGetData, |
112
|
|
|
array $apiMethodInfoMetadata, |
113
|
|
|
$responseData, |
114
|
|
|
$responseFormatClass, |
115
|
|
|
array $responseHeaders |
116
|
|
|
) { |
117
|
1 |
|
$identifier = $this->buildIdentifier($requestUri, $requestGetData); |
118
|
1 |
|
$frontendCacheExpires = (integer) $apiMethodInfoMetadata['expires']; |
119
|
1 |
|
$typo3CacheExpires = (integer) $apiMethodInfoMetadata[self::API_METHOD_TYPO3CACHE_EXPIRES]; |
120
|
1 |
|
$typo3CacheTags = explode(',', $apiMethodInfoMetadata[self::API_METHOD_TYPO3CACHE_TAGS]); |
121
|
|
|
|
122
|
1 |
|
$cacheData = array(); |
123
|
1 |
|
$cacheData['responseCode'] = $responseCode; |
124
|
1 |
|
$cacheData['requestUri'] = $requestUri; |
125
|
1 |
|
$cacheData['requestGetData'] = $requestGetData; |
126
|
1 |
|
$cacheData['responseData'] = $responseData; |
127
|
1 |
|
$cacheData['responseFormatClass'] = $responseFormatClass; |
128
|
1 |
|
$cacheData['responseHeaders'] = $responseHeaders; |
129
|
1 |
|
$cacheData['frontendCacheExpires'] = $frontendCacheExpires; |
130
|
|
|
|
131
|
1 |
|
$this->cache->set($identifier, $cacheData, $typo3CacheTags, $typo3CacheExpires); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $requestUri |
136
|
|
|
* @param array $getData |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
1 |
|
public function getCacheEntry($requestUri, array $getData) |
140
|
|
|
{ |
141
|
1 |
|
$identifier = $this->buildIdentifier($requestUri, $getData); |
142
|
1 |
|
return $this->cache->get($identifier); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $requestUri |
147
|
|
|
* @param array $getData |
148
|
|
|
* @return boolean |
149
|
|
|
*/ |
150
|
2 |
|
public function hasCacheEntry($requestUri, array $getData) |
151
|
|
|
{ |
152
|
2 |
|
$identifier = $this->buildIdentifier($requestUri, $getData); |
153
|
2 |
|
return $this->cache->has($identifier); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $tag |
158
|
|
|
*/ |
159
|
1 |
|
public function flushByTag($tag) |
160
|
|
|
{ |
161
|
1 |
|
$this->cache->flushByTag($tag); |
162
|
1 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $requestUri |
166
|
|
|
* @param array $getData |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
4 |
|
private function buildIdentifier($requestUri, array $getData) |
170
|
|
|
{ |
171
|
4 |
|
return md5($requestUri . serialize($getData)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|