| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 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 |
$routes = $this->parse($configFiles); |
|---|
| 32 |
|
|---|
| 33 |
$data = array(); |
|---|
| 34 |
foreach ($routes as $name => $route) |
|---|
| 35 |
{ |
|---|
| 36 |
$arguments = array(); |
|---|
| 37 |
foreach ($route[1] as $argument) |
|---|
| 38 |
{ |
|---|
| 39 |
$arguments[] = is_array($argument) ? var_export($argument, true) : sprintf("'%s'", $argument); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
$data[] = sprintf('\'%s\' => new %s(%s),', $name, $route[0], implode(', ', $arguments)); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
return sprintf("<?php\n". |
|---|
| 46 |
"// auto-generated by sfRoutingConfigHandler\n". |
|---|
| 47 |
"// date: %s\nreturn array(\n%s\n);\n", date('Y/m/d H:i:s'), implode("\n", $data) |
|---|
| 48 |
); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
public function evaluate($configFiles) |
|---|
| 52 |
{ |
|---|
| 53 |
$routeDefinitions = $this->parse($configFiles); |
|---|
| 54 |
|
|---|
| 55 |
$routes = array(); |
|---|
| 56 |
foreach ($routeDefinitions as $name => $route) |
|---|
| 57 |
{ |
|---|
| 58 |
$r = new ReflectionClass($route[0]); |
|---|
| 59 |
$routes[$name] = $r->newInstanceArgs($route[1]); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
return $routes; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
protected function parse($configFiles) |
|---|
| 66 |
{ |
|---|
| 67 |
|
|---|
| 68 |
$config = self::getConfiguration($configFiles); |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
$routes = array(); |
|---|
| 72 |
foreach ($config as $name => $params) |
|---|
| 73 |
{ |
|---|
| 74 |
if ( |
|---|
| 75 |
(isset($params['type']) && 'collection' == $params['type']) |
|---|
| 76 |
|| |
|---|
| 77 |
(isset($params['class']) && false !== strpos($params['class'], 'Collection')) |
|---|
| 78 |
) |
|---|
| 79 |
{ |
|---|
| 80 |
$options = isset($params['options']) ? $params['options'] : array(); |
|---|
| 81 |
$options['name'] = $name; |
|---|
| 82 |
$options['requirements'] = isset($params['requirements']) ? $params['requirements'] : array(); |
|---|
| 83 |
|
|---|
| 84 |
$routes[$name] = array(isset($params['class']) ? $params['class'] : 'sfRouteCollection', array($options)); |
|---|
| 85 |
} |
|---|
| 86 |
else |
|---|
| 87 |
{ |
|---|
| 88 |
$routes[$name] = array(isset($params['class']) ? $params['class'] : 'sfRoute', array( |
|---|
| 89 |
$params['url'] ? $params['url'] : '/', |
|---|
| 90 |
isset($params['params']) ? $params['params'] : (isset($params['param']) ? $params['param'] : array()), |
|---|
| 91 |
isset($params['requirements']) ? $params['requirements'] : array(), |
|---|
| 92 |
isset($params['options']) ? $params['options'] : array(), |
|---|
| 93 |
)); |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
return $routes; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
* @see sfConfigHandler |
|---|
| 102 |
*/ |
|---|
| 103 |
static public function getConfiguration(array $configFiles) |
|---|
| 104 |
{ |
|---|
| 105 |
return self::parseYamls($configFiles); |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|