RestlerEnhancer::decorateForMatching()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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