Passed
Push — master ( fb25eb...6e5001 )
by
unknown
38:26 queued 22:19
created

ServiceProvider::getBackendMiddlewares()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Backend;
19
20
use ArrayObject;
21
use Psr\Container\ContainerInterface;
22
use TYPO3\CMS\Backend\Http\Application;
23
use TYPO3\CMS\Backend\Http\RequestHandler;
24
use TYPO3\CMS\Backend\Http\RouteDispatcher;
25
use TYPO3\CMS\Backend\Routing\Route;
26
use TYPO3\CMS\Backend\Routing\Router;
27
use TYPO3\CMS\Backend\Routing\UriBuilder;
28
use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
29
use TYPO3\CMS\Core\Configuration\ConfigurationManager;
30
use TYPO3\CMS\Core\Context\Context;
31
use TYPO3\CMS\Core\Core\Environment;
32
use TYPO3\CMS\Core\Exception as CoreException;
33
use TYPO3\CMS\Core\Http\MiddlewareDispatcher;
34
use TYPO3\CMS\Core\Http\MiddlewareStackResolver;
35
use TYPO3\CMS\Core\Information\Typo3Version;
36
use TYPO3\CMS\Core\Package\AbstractServiceProvider;
37
38
/**
39
 * @internal
40
 */
41
class ServiceProvider extends AbstractServiceProvider
42
{
43
    protected static function getPackagePath(): string
44
    {
45
        return __DIR__ . '/../';
46
    }
47
48
    public function getFactories(): array
49
    {
50
        return [
51
            Application::class => [ static::class, 'getApplication' ],
52
            RequestHandler::class => [ static::class, 'getRequestHandler' ],
53
            RouteDispatcher::class => [ static::class, 'getRouteDispatcher' ],
54
            UriBuilder::class => [ static::class, 'getUriBuilder' ],
55
            'backend.middlewares' => [ static::class, 'getBackendMiddlewares' ],
56
            'backend.routes' => [ static::class, 'getBackendRoutes' ],
57
        ];
58
    }
59
60
    public function getExtensions(): array
61
    {
62
        return [
63
            Router::class => [ static::class, 'configureBackendRouter' ],
64
        ] + parent::getExtensions();
65
    }
66
67
    public static function getApplication(ContainerInterface $container): Application
68
    {
69
        $requestHandler = new MiddlewareDispatcher(
70
            $container->get(RequestHandler::class),
71
            $container->get('backend.middlewares'),
72
            $container
73
        );
74
        return new Application(
75
            $requestHandler,
76
            $container->get(ConfigurationManager::class),
77
            $container->get(Context::class)
78
        );
79
    }
80
81
    public static function getRequestHandler(ContainerInterface $container): RequestHandler
82
    {
83
        return new RequestHandler(
84
            $container->get(RouteDispatcher::class),
85
            $container->get(UriBuilder::class)
86
        );
87
    }
88
89
    public static function getRouteDispatcher(ContainerInterface $container): RouteDispatcher
90
    {
91
        return self::new($container, RouteDispatcher::class, [$container]);
92
    }
93
94
    public static function getUriBuilder(ContainerInterface $container): UriBuilder
95
    {
96
        return self::new($container, UriBuilder::class, [
97
            $container->get(Router::class)
98
        ]);
99
    }
100
101
    /**
102
     * @param ContainerInterface $container
103
     * @return ArrayObject
104
     * @throws InvalidDataException
105
     * @throws CoreException
106
     */
107
    public static function getBackendMiddlewares(ContainerInterface $container): ArrayObject
108
    {
109
        return new ArrayObject($container->get(MiddlewareStackResolver::class)->resolve('backend'));
110
    }
111
112
    public static function configureBackendRouter(ContainerInterface $container, Router $router = null): Router
113
    {
114
        $router = $router ?? self::new($container, Router::class);
115
        $cache = $container->get('cache.core');
116
117
        $cacheIdentifier = 'BackendRoutes_' . sha1((string)(new Typo3Version()) . Environment::getProjectPath() . 'BackendRoutes');
118
        $routesFromPackages = $cache->require($cacheIdentifier);
119
        if ($routesFromPackages === false) {
120
            $routesFromPackages = $container->get('backend.routes')->getArrayCopy();
121
            $cache->set($cacheIdentifier, 'return ' . var_export($routesFromPackages, true) . ';');
122
        }
123
124
        foreach ($routesFromPackages as $name => $options) {
125
            $path = $options['path'];
126
            $methods = $options['methods'] ?? [];
127
            unset($options['path']);
128
            unset($options['methods']);
129
            $route = new Route($path, $options);
130
            if (count($methods) > 0) {
131
                $route->setMethods($methods);
132
            }
133
            $router->addRoute($name, $route);
134
        }
135
136
        return $router;
137
    }
138
139
    public static function getBackendRoutes(ContainerInterface $container): ArrayObject
140
    {
141
        return new ArrayObject();
142
    }
143
}
144