/
RouteLoader.php
115 lines (94 loc) · 3.48 KB
/
RouteLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\ManagerBundle\Routing;
use Contao\ManagerPlugin\PluginLoader;
use Contao\ManagerPlugin\Routing\RoutingPluginInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouteCollection;
class RouteLoader
{
/**
* @var LoaderInterface
*/
private $loader;
/**
* @var PluginLoader
*/
private $pluginLoader;
/**
* @var KernelInterface
*/
private $kernel;
/**
* @var string
*/
private $rootDir;
/**
* @internal Do not inherit from this class; decorate the "contao_manager.routing_loader" service instead
*/
public function __construct(LoaderInterface $loader, PluginLoader $pluginLoader, KernelInterface $kernel, string $rootDir)
{
$this->loader = $loader;
$this->pluginLoader = $pluginLoader;
$this->kernel = $kernel;
$this->rootDir = $rootDir;
}
/**
* Returns a route collection build from all plugin routes.
*/
public function loadFromPlugins(): RouteCollection
{
$collection = array_reduce(
$this->pluginLoader->getInstancesOf(PluginLoader::ROUTING_PLUGINS, true),
function (RouteCollection $collection, RoutingPluginInterface $plugin): RouteCollection {
$routes = $plugin->getRouteCollection($this->loader->getResolver(), $this->kernel);
if ($routes instanceof RouteCollection) {
$collection->addCollection($routes);
}
return $collection;
},
new RouteCollection()
);
// Load the routing.yml file if it exists
if ($configFile = $this->getConfigFile()) {
$routes = $this->loader->getResolver()->resolve($configFile)->load($configFile);
if ($routes instanceof RouteCollection) {
$collection->addCollection($routes);
}
}
// Make sure the Contao frontend routes are always loaded last
foreach (['contao_frontend', 'contao_index', 'contao_root', 'contao_catch_all'] as $name) {
if ($route = $collection->get($name)) {
$collection->add($name, $route);
}
}
return $collection;
}
private function getConfigFile(): ?string
{
foreach (['routes.yaml', 'routes.yml', 'routing.yaml', 'routing.yml'] as $file) {
if (file_exists($this->rootDir.'/config/'.$file)) {
if (0 === strncmp($file, 'routing.', 8)) {
@trigger_error(sprintf('Using a "%s" file has been deprecated and will no longer work in Contao 5.0. Rename it to "routes.yaml" instead.', $file), E_USER_DEPRECATED);
}
return $this->rootDir.'/config/'.$file;
}
}
// Fallback to the legacy config file (see #566)
foreach (['routes.yaml', 'routes.yml', 'routing.yaml', 'routing.yml'] as $file) {
if (file_exists($this->rootDir.'/app/config/'.$file)) {
@trigger_error(sprintf('Storing the "%s" file in the "app/config" folder has been deprecated and will no longer work in Contao 5.0. Move it to the "config" folder instead.', $file), E_USER_DEPRECATED);
return $this->rootDir.'/app/config/'.$file;
}
}
return null;
}
}