-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsfRoutingConfigHandler.class.php
111 lines (95 loc) · 3.54 KB
/
sfRoutingConfigHandler.class.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
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @author Fabien Potencier <[email protected]>
*/
class sfRoutingConfigHandler extends sfYamlConfigHandler
{
/**
* Executes this configuration handler.
*
* @param array $configFiles An array of absolute filesystem path to a configuration file
*
* @return string Data to be written to a cache file
*
* @throws sfConfigurationException If a requested configuration file does not exist or is not readable
* @throws sfParseException If a requested configuration file is improperly formatted
*/
public function execute($configFiles)
{
$options = $this->getOptions();
unset($options['cache']);
$data = [];
foreach ($this->parse($configFiles) as $name => $routeConfig) {
$r = new ReflectionClass($routeConfig[0]);
/** @var sfRoute $route */
$route = $r->newInstanceArgs($routeConfig[1]);
$routes = $route instanceof sfRouteCollection ? $route : [$name => $route];
foreach (sfPatternRouting::flattenRoutes($routes) as $name => $route) {
$route->setDefaultOptions($options);
$data[] = sprintf('$this->routes[\'%s\'] = %s;', $name, var_export(serialize($route), true));
}
}
return sprintf(
"<?php\n".
"// auto-generated by sfRoutingConfigHandler\n".
"// date: %s\n%s\n",
date('Y/m/d H:i:s'),
implode("\n", $data)
);
}
public function evaluate($configFiles)
{
$routeDefinitions = $this->parse($configFiles);
$routes = [];
foreach ($routeDefinitions as $name => $route) {
$r = new ReflectionClass($route[0]);
$routes[$name] = $r->newInstanceArgs($route[1]);
}
return $routes;
}
/**
* @see sfConfigHandler
*/
public static function getConfiguration(array $configFiles)
{
return static::parseYamls($configFiles);
}
protected function getOptions()
{
$config = sfFactoryConfigHandler::getConfiguration(sfContext::getInstance()->getConfiguration()->getConfigPaths('config/factories.yml'));
return $config['routing']['param'];
}
protected function parse($configFiles)
{
// parse the yaml
$config = static::getConfiguration($configFiles);
// collect routes
$routes = [];
foreach ($config as $name => $params) {
if (
(isset($params['type']) && 'collection' == $params['type'])
|| (isset($params['class']) && false !== strpos($params['class'], 'Collection'))
) {
$options = $params['options'] ?? [];
$options['name'] = $name;
$options['requirements'] = $params['requirements'] ?? [];
$routes[$name] = [$params['class'] ?? 'sfRouteCollection', [$options]];
} else {
$routes[$name] = [$params['class'] ?? 'sfRoute', [
$params['url'] ?: '/',
$params['params'] ?? $params['param'] ?? [],
$params['requirements'] ?? [],
$params['options'] ?? [],
]];
}
}
return $routes;
}
}