| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* @package symfony
|
|
|
13 |
* @subpackage config
|
|
|
14 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
15 |
* @version SVN: $Id: sfRoutingConfigHandler.class.php 24962 2009-12-04 20:39:41Z FabianLange $
|
|
|
16 |
*/
|
|
|
17 |
class sfRoutingConfigHandler extends sfYamlConfigHandler
|
|
|
18 |
{
|
|
|
19 |
/**
|
|
|
20 |
* Executes this configuration handler.
|
|
|
21 |
*
|
|
|
22 |
* @param array $configFiles An array of absolute filesystem path to a configuration file
|
|
|
23 |
*
|
|
|
24 |
* @return string Data to be written to a cache file
|
|
|
25 |
*
|
|
|
26 |
* @throws sfConfigurationException If a requested configuration file does not exist or is not readable
|
|
|
27 |
* @throws sfParseException If a requested configuration file is improperly formatted
|
|
|
28 |
*/
|
|
|
29 |
public function execute($configFiles)
|
|
|
30 |
{
|
|
|
31 |
$options = $this->getOptions();
|
|
|
32 |
unset($options['cache']);
|
|
|
33 |
|
|
|
34 |
$data = array();
|
|
|
35 |
foreach ($this->parse($configFiles) as $name => $routeConfig)
|
|
|
36 |
{
|
|
|
37 |
$r = new ReflectionClass($routeConfig[0]);
|
|
|
38 |
$route = $r->newInstanceArgs($routeConfig[1]);
|
|
|
39 |
|
|
|
40 |
$routes = $route instanceof sfRouteCollection ? $route : array($name => $route);
|
|
|
41 |
foreach (sfPatternRouting::flattenRoutes($routes) as $name => $route)
|
|
|
42 |
{
|
|
|
43 |
$route->setDefaultOptions($options);
|
|
|
44 |
$data[] = sprintf('$this->routes[\'%s\'] = unserialize(%s);', $name, var_export(serialize($route), true));
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
return sprintf("<?php\n".
|
|
|
49 |
"// auto-generated by sfRoutingConfigHandler\n".
|
|
|
50 |
"// date: %s\n%s\n", date('Y/m/d H:i:s'), implode("\n", $data)
|
|
|
51 |
);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
protected function getOptions()
|
|
|
55 |
{
|
|
|
56 |
$config = sfFactoryConfigHandler::getConfiguration(sfContext::getInstance()->getConfiguration()->getConfigPaths('config/factories.yml'));
|
|
|
57 |
return $config['routing']['param'];
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public function evaluate($configFiles)
|
|
|
61 |
{
|
|
|
62 |
$routeDefinitions = $this->parse($configFiles);
|
|
|
63 |
|
|
|
64 |
$routes = array();
|
|
|
65 |
foreach ($routeDefinitions as $name => $route)
|
|
|
66 |
{
|
|
|
67 |
$r = new ReflectionClass($route[0]);
|
|
|
68 |
$routes[$name] = $r->newInstanceArgs($route[1]);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
return $routes;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
protected function parse($configFiles)
|
|
|
75 |
{
|
|
|
76 |
// parse the yaml
|
|
|
77 |
$config = self::getConfiguration($configFiles);
|
|
|
78 |
|
|
|
79 |
// collect routes
|
|
|
80 |
$routes = array();
|
|
|
81 |
foreach ($config as $name => $params)
|
|
|
82 |
{
|
|
|
83 |
if (
|
|
|
84 |
(isset($params['type']) && 'collection' == $params['type'])
|
|
|
85 |
||
|
|
|
86 |
(isset($params['class']) && false !== strpos($params['class'], 'Collection'))
|
|
|
87 |
)
|
|
|
88 |
{
|
|
|
89 |
$options = isset($params['options']) ? $params['options'] : array();
|
|
|
90 |
$options['name'] = $name;
|
|
|
91 |
$options['requirements'] = isset($params['requirements']) ? $params['requirements'] : array();
|
|
|
92 |
|
|
|
93 |
$routes[$name] = array(isset($params['class']) ? $params['class'] : 'sfRouteCollection', array($options));
|
|
|
94 |
}
|
|
|
95 |
else
|
|
|
96 |
{
|
|
|
97 |
$routes[$name] = array(isset($params['class']) ? $params['class'] : 'sfRoute', array(
|
|
|
98 |
$params['url'] ? $params['url'] : '/',
|
|
|
99 |
isset($params['params']) ? $params['params'] : (isset($params['param']) ? $params['param'] : array()),
|
|
|
100 |
isset($params['requirements']) ? $params['requirements'] : array(),
|
|
|
101 |
isset($params['options']) ? $params['options'] : array(),
|
|
|
102 |
));
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return $routes;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* @see sfConfigHandler
|
|
|
111 |
*/
|
|
|
112 |
static public function getConfiguration(array $configFiles)
|
|
|
113 |
{
|
|
|
114 |
return self::parseYamls($configFiles);
|
|
|
115 |
}
|
|
|
116 |
}
|