1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aoe\Restler\System\TYPO3; |
4
|
|
|
|
5
|
|
|
/*************************************************************** |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2024 AOE GmbH <[email protected]> |
9
|
|
|
* |
10
|
|
|
* All rights reserved |
11
|
|
|
* |
12
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
13
|
|
|
* free software; you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU General Public License as published by |
15
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
16
|
|
|
* (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* The GNU General Public License can be found at |
19
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
20
|
|
|
* |
21
|
|
|
* This script 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 General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
27
|
|
|
***************************************************************/ |
28
|
|
|
|
29
|
|
|
use TYPO3\CMS\Core\Cache\CacheManager; |
30
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; |
31
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
32
|
|
|
|
33
|
|
|
class Cache implements SingletonInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* This is the phpdoc-tag, which defines, that the response of API-method is cacheable via TYPO3-caching-framework |
37
|
|
|
* |
38
|
|
|
* Syntax: |
39
|
|
|
* The PHPdoc-comment must look like this: |
40
|
|
|
* @restler_typo3cache_expires [expires-in-seconds] |
41
|
|
|
* |
42
|
|
|
* Examples: |
43
|
|
|
* When API-method should be cacheable in TYPO3 for 10 minutes, than the PHPdoc-comment must look like this: |
44
|
|
|
* @restler_typo3cache_expires 600 |
45
|
|
|
* When API-method should be cacheable in TYPO3 for endless time, than the PHPdoc-comment must look like this: |
46
|
|
|
* @restler_typo3cache_expires 0 |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public const API_METHOD_TYPO3CACHE_EXPIRES = 'restler_typo3cache_expires'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* This is the phpdoc-tag, which defines, that the cached response of API-method should be tagged with given tags |
54
|
|
|
* |
55
|
|
|
* Syntax: |
56
|
|
|
* The PHPdoc-comment must look like this: |
57
|
|
|
* @restler_typo3cache_tags [comma-separated-list-of-tags] |
58
|
|
|
* |
59
|
|
|
* Example: |
60
|
|
|
* When response of API-method should be tagged with 'tag_a' and 'tag_b', than the PHPdoc-comment must look like this: |
61
|
|
|
* @restler_typo3cache_tags tag_a,tag_b |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public const API_METHOD_TYPO3CACHE_TAGS = 'restler_typo3cache_tags'; |
66
|
|
|
|
67
|
|
|
private readonly FrontendInterface $cache; |
68
|
|
|
|
69
|
|
|
public function __construct(CacheManager $cacheManager) |
70
|
|
|
{ |
71
|
|
|
$this->cache = $cacheManager->getCache('restler'); |
|
|
|
|
72
|
8 |
|
} |
73
|
|
|
|
74
|
8 |
|
public function isResponseCacheableByTypo3Cache(string $requestMethod, array $apiMethodInfoMetadata): bool |
75
|
8 |
|
{ |
76
|
|
|
return $requestMethod === 'GET' && |
77
|
3 |
|
array_key_exists(self::API_METHOD_TYPO3CACHE_EXPIRES, $apiMethodInfoMetadata) && |
78
|
|
|
array_key_exists(self::API_METHOD_TYPO3CACHE_TAGS, $apiMethodInfoMetadata); |
79
|
3 |
|
} |
80
|
3 |
|
|
81
|
3 |
|
/** |
82
|
1 |
|
* cache response |
83
|
|
|
*/ |
84
|
2 |
|
public function cacheResponseByTypo3Cache( |
85
|
|
|
int $responseCode, |
86
|
|
|
string $requestUri, |
87
|
|
|
array $requestGetData, |
88
|
|
|
array $apiMethodInfoMetadata, |
89
|
|
|
string $responseData, |
90
|
1 |
|
string $responseFormatClass, |
91
|
|
|
array $responseHeaders |
92
|
|
|
): void { |
93
|
|
|
$identifier = $this->buildIdentifier($requestUri, $requestGetData); |
94
|
|
|
$frontendCacheExpires = (int) ($apiMethodInfoMetadata['expires'] ?? 0); |
95
|
|
|
$typo3CacheExpires = (int) $apiMethodInfoMetadata[self::API_METHOD_TYPO3CACHE_EXPIRES]; |
96
|
|
|
$typo3CacheTags = explode(',', (string) $apiMethodInfoMetadata[self::API_METHOD_TYPO3CACHE_TAGS]); |
97
|
|
|
|
98
|
|
|
$cacheData = []; |
99
|
1 |
|
$cacheData['responseCode'] = $responseCode; |
100
|
1 |
|
$cacheData['requestUri'] = $requestUri; |
101
|
1 |
|
$cacheData['requestGetData'] = $requestGetData; |
102
|
1 |
|
$cacheData['responseData'] = $responseData; |
103
|
|
|
$cacheData['responseFormatClass'] = $responseFormatClass; |
104
|
1 |
|
$cacheData['responseHeaders'] = $responseHeaders; |
105
|
1 |
|
$cacheData['frontendCacheExpires'] = $frontendCacheExpires; |
106
|
1 |
|
|
107
|
1 |
|
$this->cache->set($identifier, $cacheData, $typo3CacheTags, $typo3CacheExpires); |
108
|
1 |
|
} |
109
|
1 |
|
|
110
|
1 |
|
public function getCacheEntry(string $requestUri, array $getData) |
111
|
1 |
|
{ |
112
|
|
|
$identifier = $this->buildIdentifier($requestUri, $getData); |
113
|
1 |
|
return $this->cache->get($identifier); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
1 |
|
public function hasCacheEntry(string $requestUri, array $getData): bool |
117
|
|
|
{ |
118
|
1 |
|
$identifier = $this->buildIdentifier($requestUri, $getData); |
119
|
1 |
|
return $this->cache->has($identifier); |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
public function flushByTag(string $tag): void |
123
|
|
|
{ |
124
|
2 |
|
$this->cache->flushByTag($tag); |
125
|
2 |
|
} |
126
|
|
|
|
127
|
|
|
private function buildIdentifier(string $requestUri, array $getData): string |
128
|
1 |
|
{ |
129
|
|
|
return md5($requestUri . serialize($getData)); |
130
|
1 |
|
} |
131
|
|
|
} |
132
|
|
|
|