Completed
Push — master ( 73bcf9...57af29 )
by
unknown
40:31 queued 24:44
created

Router   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 13 3
A matchRequest() 0 17 1
A addRoute() 0 4 1
A getRoutes() 0 3 1
A __construct() 0 3 1
A getRouteCollection() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Backend\Routing;
17
18
use Psr\Http\Message\ServerRequestInterface;
19
use Symfony\Component\Routing\Matcher\UrlMatcher;
20
use Symfony\Component\Routing\RequestContext;
21
use Symfony\Component\Routing\Route as SymfonyRoute;
22
use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
23
use TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException;
24
use TYPO3\CMS\Core\SingletonInterface;
25
use TYPO3\CMS\Core\Utility\HttpUtility;
26
27
/**
28
 * Implementation of a class for adding routes, collecting throughout the Bootstrap
29
 * to register all sorts of Backend Routes, and to fetch the main Collection in order
30
 * to resolve a route (see ->match() and ->matchRequest()).
31
 *
32
 * Ideally, the Router is solely instantiated and accessed via the Bootstrap, the RequestHandler and the UriBuilder.
33
 *
34
 * See \TYPO3\CMS\Backend\Http\RequestHandler for more details on route matching() and Bootstrap->initializeBackendRouting().
35
 *
36
 * The architecture is inspired by the Symfony Routing Component.
37
 */
38
class Router implements SingletonInterface
39
{
40
    /**
41
     * All routes used in the Backend
42
     * @var SymfonyRouteCollection
43
     */
44
    protected $routeCollection;
45
46
    public function __construct()
47
    {
48
        $this->routeCollection = new SymfonyRouteCollection();
49
    }
50
    /**
51
     * Adds a new route with the identifiers
52
     *
53
     * @param string $routeIdentifier
54
     * @param Route $route
55
     */
56
    public function addRoute($routeIdentifier, $route)
57
    {
58
        $symfonyRoute = new SymfonyRoute($route->getPath(), [], [], $route->getOptions());
59
        $this->routeCollection->add($routeIdentifier, $symfonyRoute);
60
    }
61
62
    /**
63
     * Fetch all registered routes, only use in UriBuilder
64
     *
65
     * @return Route[]
66
     */
67
    public function getRoutes(): iterable
68
    {
69
        return $this->routeCollection->getIterator();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->routeCollection->getIterator() returns the type ArrayIterator which is incompatible with the documented return type TYPO3\CMS\Backend\Routing\Route[].
Loading history...
70
    }
71
72
    /**
73
     * @internal only use in Core, this should not be exposed
74
     * @return SymfonyRouteCollection
75
     */
76
    public function getRouteCollection(): SymfonyRouteCollection
77
    {
78
        return $this->routeCollection;
79
    }
80
81
    /**
82
     * Tries to match a URL path with a set of routes.
83
     *
84
     * @param string $pathInfo The path info to be parsed
85
     * @return Route the first Route object found
86
     * @throws ResourceNotFoundException If the resource could not be found
87
     */
88
    public function match($pathInfo)
89
    {
90
        foreach ($this->routeCollection->getIterator() as $routeIdentifier => $route) {
91
            // This check is done in a simple way as there are no parameters yet (get parameters only)
92
            if ($route->getPath() === $pathInfo) {
93
                $routeResult = new Route($route->getPath(), $route->getOptions());
94
                // Store the name of the Route in the _identifier option so the token can be checked against that
95
                $routeResult->setOption('_identifier', $routeIdentifier);
96
                return $routeResult;
97
            }
98
        }
99
100
        throw new ResourceNotFoundException('The requested resource "' . $pathInfo . '" was not found.', 1425389240);
101
    }
102
103
    /**
104
     * Tries to match a URI against the registered routes
105
     *
106
     * @param ServerRequestInterface $request
107
     * @return Route the first Route object found
108
     */
109
    public function matchRequest(ServerRequestInterface $request)
110
    {
111
        // Allow the login page to be displayed if routing is not used and on index.php
112
        // (consolidate RouteDispatcher::evaluateReferrer() when changing 'login' to something different)
113
        $path = $request->getQueryParams()['route'] ?? $request->getParsedBody()['route'] ?? '/login';
114
115
        $context = new RequestContext(
116
            '',
117
            $request->getMethod(),
118
            (string)HttpUtility::idn_to_ascii($request->getUri()->getHost()),
119
            $request->getUri()->getScheme()
120
        );
121
        $result = (new UrlMatcher($this->routeCollection, $context))->match($path);
122
        $matchedSymfonyRoute = $this->routeCollection->get($result['_route']);
123
        $route = new Route($matchedSymfonyRoute->getPath(), $matchedSymfonyRoute->getOptions());
124
        $route->setOption('_identifier', $result['_route']);
125
        return $route;
126
    }
127
}
128