1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aoe\Restler\System\TYPO3; |
4
|
|
|
|
5
|
|
|
use Aoe\Restler\System\RestlerBuilderAware; |
6
|
|
|
use TYPO3\CMS\Core\Routing\Aspect\AspectInterface; |
7
|
|
|
use TYPO3\CMS\Core\Routing\Enhancer\DecoratingEnhancerInterface; |
8
|
|
|
use TYPO3\CMS\Core\Routing\RouteCollection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Puts the URLs which should be handled by restler into the current route collection |
12
|
|
|
* and maps them to the current default route. |
13
|
|
|
* |
14
|
|
|
* Please be aware that this seems to have some side effects on other DecoratingEnhancers like PageTypeDecorator |
15
|
|
|
* routeEnhancers: |
16
|
|
|
* RestlerEnhancer: |
17
|
|
|
* type: Restler |
18
|
|
|
* default: '.json' |
19
|
|
|
*/ |
20
|
|
|
class RestlerEnhancer extends RestlerBuilderAware implements DecoratingEnhancerInterface |
21
|
|
|
{ |
22
|
|
|
private $default; |
23
|
|
|
|
24
|
|
|
public function __construct($configuration) |
25
|
|
|
{ |
26
|
|
|
parent::__construct(null); |
27
|
|
|
$default = $configuration['default'] ?? ''; |
28
|
|
|
|
29
|
|
|
if (!is_string($default)) { |
30
|
|
|
throw new \InvalidArgumentException('default must be string', 1538327508); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->default = $default; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Gets pattern that can be used to redecorate (undecorate) |
38
|
|
|
* a potential previously decorated route path. |
39
|
|
|
* |
40
|
|
|
* Example: |
41
|
|
|
* + route path: 'first/second.html' |
42
|
|
|
* + redecoration pattern: '(?:\.html|\.json)$' |
43
|
|
|
* -> 'first/second' might be the redecorated route path after |
44
|
|
|
* applying the redecoration pattern to preg_match/preg_replace |
45
|
|
|
* |
46
|
|
|
* @return string regular expression pattern |
47
|
|
|
*/ |
48
|
|
|
public function getRoutePathRedecorationPattern(): string |
49
|
|
|
{ |
50
|
|
|
return preg_quote($this->default, '#') . '$'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Decorates route collection to be processed during URL resolving. |
55
|
|
|
* Executed before invoking routing enhancers. |
56
|
|
|
* |
57
|
|
|
* @param RouteCollection $collection |
58
|
|
|
* @param string $routePath URL path |
59
|
|
|
*/ |
60
|
|
|
public function decorateForMatching(RouteCollection $collection, string $routePath): void |
61
|
|
|
{ |
62
|
|
|
// set path according to typo3/sysext/core/Classes/Routing/PageRouter.php:132 |
63
|
|
|
$prefixedUrlPath = '/' . trim($routePath, '/'); |
64
|
|
|
|
65
|
|
|
if ($this->isRestlerPrefix($prefixedUrlPath)) { |
66
|
|
|
$this->getRestlerBuilder()->build(null); |
67
|
|
|
|
68
|
|
|
if ($this->isRestlerUrl($prefixedUrlPath)) { |
69
|
|
|
$defaultRoute = $collection->get('default'); |
70
|
|
|
$defaultRoute->setPath($prefixedUrlPath); |
71
|
|
|
$collection->add('restler', $defaultRoute); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Decorates route collection during URL URL generation. |
78
|
|
|
* Executed before invoking routing enhancers. |
79
|
|
|
* |
80
|
|
|
* @param RouteCollection $collection |
81
|
|
|
* @param array $parameters query parameters |
82
|
|
|
*/ |
83
|
|
|
public function decorateForGeneration(RouteCollection $collection, array $parameters): void |
84
|
|
|
{ |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param AspectInterface[] $aspects |
89
|
|
|
*/ |
90
|
|
|
public function setAspects(array $aspects): void |
91
|
|
|
{ |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return AspectInterface[] |
96
|
|
|
*/ |
97
|
|
|
public function getAspects(): array |
98
|
|
|
{ |
99
|
|
|
return []; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function isRestlerUrl($uri): bool |
103
|
|
|
{ |
104
|
|
|
return \Aoe\Restler\System\Restler\Routes::containsUrl($uri); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: