sfRoutingConfigHandler.class.php 3.2 KB

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