|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.routing |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace midcom\routing; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\Config\Loader\Loader as base; |
|
12
|
|
|
use Symfony\Component\Routing\RouteCollection; |
|
13
|
|
|
use Symfony\Component\Routing\Route; |
|
14
|
|
|
use midcom; |
|
15
|
|
|
use midcom_baseclasses_components_configuration; |
|
16
|
|
|
use Symfony\Component\Routing\Loader\YamlFileLoader; |
|
17
|
|
|
use Symfony\Component\Config\FileLocator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @package midcom.routing |
|
21
|
|
|
*/ |
|
22
|
|
|
class loader extends base |
|
23
|
|
|
{ |
|
24
|
|
|
private ?YamlFileLoader $yaml_loader = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritDoc} |
|
28
|
|
|
* @see \Symfony\Component\Config\Loader\LoaderInterface::load() |
|
29
|
|
|
*/ |
|
30
|
42 |
|
public function load(mixed $input, ?string $type = null): mixed |
|
31
|
|
|
{ |
|
32
|
42 |
|
if (is_string($input)) { |
|
33
|
42 |
|
if (!$this->is_legacy($input)) { |
|
34
|
41 |
|
return $this->get_yaml()->load($this->get_path($input, 'yml'), $type); |
|
35
|
|
|
} |
|
36
|
1 |
|
$input = $this->get_legacy_routes($input); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
$collection = new RouteCollection(); |
|
40
|
|
|
|
|
41
|
1 |
|
foreach ($input as $name => $config) { |
|
42
|
|
|
$path = '/'; |
|
43
|
|
|
$requirements = []; |
|
44
|
|
|
|
|
45
|
|
|
if (empty($config['fixed_args'])) { |
|
46
|
|
|
$config['fixed_args'] = []; |
|
47
|
|
|
} else { |
|
48
|
|
|
$config['fixed_args'] = (array) $config['fixed_args']; |
|
49
|
|
|
$path = '/' . implode('/', $config['fixed_args']) . '/'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!array_key_exists('variable_args', $config)) { |
|
53
|
|
|
$config['variable_args'] = 0; |
|
54
|
|
|
} |
|
55
|
|
|
for ($i = 0; $i < $config['variable_args']; $i++) { |
|
56
|
|
|
$path .= '{args_' . $i . '}/'; |
|
57
|
|
|
if (!empty($config['validation'][$i])) { |
|
58
|
|
|
$requirements['args_' . $i] = $this->translate_validation($config['validation'][$i]); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$defaults = [ |
|
63
|
|
|
'_controller' => implode('::', (array) $config['handler']) |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
$route = new Route($path, $defaults, $requirements); |
|
67
|
|
|
$collection->add($name, $route); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return $collection; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Small transitory helper for old-style route validation configs |
|
75
|
|
|
*/ |
|
76
|
|
|
private function translate_validation(array $input) : string |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($input as &$value) { |
|
79
|
|
|
if (in_array($value, ['is_numeric', 'is_int'])) { |
|
80
|
|
|
$value = '\d+'; |
|
81
|
|
|
} elseif ($value == 'mgd_is_guid') { |
|
82
|
|
|
$value = '[0-9a-f]{21,80}'; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
return implode('|', $input); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function supports(mixed $resource, ?string $type = null) : bool |
|
92
|
|
|
{ |
|
93
|
|
|
if (is_string($resource)) { |
|
94
|
|
|
if (!$this->is_legacy($resource)) { |
|
95
|
|
|
return $this->get_yaml()->supports($this->get_path($resource, 'yml'), $type); |
|
96
|
|
|
} |
|
97
|
|
|
return (!$type || 'string' === $type); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return is_array($resource) && (!$type || 'array' === $type); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
353 |
|
private function is_legacy(string $component) : bool |
|
104
|
|
|
{ |
|
105
|
353 |
|
return !file_exists($this->get_path($component, 'yml')); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
41 |
|
private function get_yaml() : YamlFileLoader |
|
109
|
|
|
{ |
|
110
|
41 |
|
return $this->yaml_loader ??= new YamlFileLoader(new FileLocator); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
353 |
|
private function get_path(string $component, string $suffix) : string |
|
114
|
|
|
{ |
|
115
|
353 |
|
return midcom::get()->componentloader->path_to_snippetpath($component) . '/config/routes.' . $suffix; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
353 |
|
public function get_legacy_routes(string $component) : array |
|
119
|
|
|
{ |
|
120
|
353 |
|
if (!$this->is_legacy($component)) { |
|
121
|
352 |
|
return []; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
return $this->load_routes($component); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
private function load_routes(string $component) : array |
|
128
|
|
|
{ |
|
129
|
1 |
|
$path = $this->get_path($component, 'inc'); |
|
130
|
|
|
// Load and parse the global config |
|
131
|
1 |
|
return midcom_baseclasses_components_configuration::read_array_from_file($path); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|