Completed
Pull Request — master (#50)
by
unknown
03:45
created

RestlerEnhancer::setAspects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Aoe\Restler\System\TYPO3;
4
5
use Aoe\Restler\System\Restler\Builder as RestlerBuilder;
6
use TYPO3\CMS\Core\Routing\Aspect\AspectInterface;
7
use TYPO3\CMS\Core\Routing\Enhancer\DecoratingEnhancerInterface;
8
use TYPO3\CMS\Core\Routing\RouteCollection;
9
use TYPO3\CMS\Core\Utility\GeneralUtility;
10
use TYPO3\CMS\Extbase\Object\ObjectManager;
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 implements DecoratingEnhancerInterface
23
{
24
    /**
25
     * @var RestlerBuilder
26
     */
27
    private $restlerBuilder;
28
29
    private $default;
30
31
    public function __construct($configuration)
32
    {
33
        $default = $configuration['default'] ?? '';
34
35
        if (!is_string($default)) {
36
            throw new \InvalidArgumentException('default must be string', 1538327508);
37
        }
38
39
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
40
        $this->restlerBuilder = $objectManager->get(RestlerBuilder::class);
41
        $this->default = $default;
42
    }
43
44
45
    /**
46
     * Gets pattern that can be used to redecorate (undecorate)
47
     * a potential previously decorated route path.
48
     *
49
     * Example:
50
     * + route path: 'first/second.html'
51
     * + redecoration pattern: '(?:\.html|\.json)$'
52
     * -> 'first/second' might be the redecorated route path after
53
     *    applying the redecoration pattern to preg_match/preg_replace
54
     *
55
     * @return string regular expression pattern
56
     */
57
    public function getRoutePathRedecorationPattern(): string
58
    {
59
        return preg_quote($this->default, '#') . '$';
60
    }
61
62
    /**
63
     * Decorates route collection to be processed during URL resolving.
64
     * Executed before invoking routing enhancers.
65
     *
66
     * @param RouteCollection $collection
67
     * @param string $routePath URL path
68
     */
69
    public function decorateForMatching(RouteCollection $collection, string $routePath): void
70
    {
71
        $this->restlerBuilder->build(null);
72
73
        // set path according to typo3/sysext/core/Classes/Routing/PageRouter.php:132
74
        $prefixedUrlPath = '/' . trim($routePath, '/');
75
        
76
        if ($this->isRestlerUrl($prefixedUrlPath)) {
77
            $defaultRoute = $collection->get('default');
78
            $defaultRoute->setPath($prefixedUrlPath);
79
            $collection->add('restler', $defaultRoute);
0 ignored issues
show
Bug introduced by
It seems like $defaultRoute defined by $collection->get('default') on line 77 can be null; however, Symfony\Component\Routing\RouteCollection::add() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
        }
81
    }
82
83
    /**
84
     * Decorates route collection during URL URL generation.
85
     * Executed before invoking routing enhancers.
86
     *
87
     * @param RouteCollection $collection
88
     * @param array $parameters query parameters
89
     */
90
    public function decorateForGeneration(RouteCollection $collection, array $parameters): void
91
    {
92
    }
93
94
    /**
95
     * @param AspectInterface[] $aspects
96
     */
97
    public function setAspects(array $aspects): void
98
    {
99
    }
100
101
    /**
102
     * @return AspectInterface[]
103
     */
104
    public function getAspects(): array
105
    {
106
        return [];
107
    }
108
109
    private function isRestlerUrl($uri): bool
110
    {
111
        return \Aoe\Restler\System\Restler\Routes::containsUrl($uri);
112
    }
113
114
}
115