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

Debug toolbar