PhpMatcherDumper.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\Routing\Matcher\Dumper;
  11. use Symfony\Component\Routing\Route;
  12. use Symfony\Component\Routing\RouteCollection;
  13. /**
  14. * PhpMatcherDumper creates a PHP class able to match URLs for a given set of routes.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class PhpMatcherDumper extends MatcherDumper
  19. {
  20. /**
  21. * Dumps a set of routes to a PHP class.
  22. *
  23. * Available options:
  24. *
  25. * * class: The class name
  26. * * base_class: The base class name
  27. *
  28. * @param array $options An array of options
  29. *
  30. * @return string A PHP class representing the matcher class
  31. */
  32. public function dump(array $options = array())
  33. {
  34. $options = array_merge(array(
  35. 'class' => 'ProjectUrlMatcher',
  36. 'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
  37. ), $options);
  38. // trailing slash support is only enabled if we know how to redirect the user
  39. $interfaces = class_implements($options['base_class']);
  40. $supportsRedirections = isset($interfaces['Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface']);
  41. return
  42. $this->startClass($options['class'], $options['base_class']).
  43. $this->addConstructor().
  44. $this->addMatcher($supportsRedirections).
  45. $this->endClass()
  46. ;
  47. }
  48. private function addMatcher($supportsRedirections)
  49. {
  50. $code = implode("\n", $this->compileRoutes($this->getRoutes(), $supportsRedirections));
  51. return <<<EOF
  52. public function match(\$pathinfo)
  53. {
  54. \$allow = array();
  55. $code
  56. throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();
  57. }
  58. EOF;
  59. }
  60. private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null)
  61. {
  62. $code = array();
  63. $routes = clone $routes;
  64. $routeIterator = $routes->getIterator();
  65. $keys = array_keys($routeIterator->getArrayCopy());
  66. $keysCount = count($keys);
  67. $i = 0;
  68. foreach ($routeIterator as $name => $route) {
  69. $i++;
  70. $route = clone $route;
  71. if ($route instanceof RouteCollection) {
  72. $prefix = $route->getPrefix();
  73. $optimizable = $prefix && count($route->all()) > 1 && false === strpos($route->getPrefix(), '{');
  74. $indent = '';
  75. if ($optimizable) {
  76. for ($j = $i; $j < $keysCount; $j++) {
  77. if ($keys[$j] === null) {
  78. continue;
  79. }
  80. $testRoute = $routeIterator->offsetGet($keys[$j]);
  81. $isCollection = ($testRoute instanceof RouteCollection);
  82. $testPrefix = $isCollection ? $testRoute->getPrefix() : $testRoute->getPattern();
  83. if (0 === strpos($testPrefix, $prefix)) {
  84. $routeIterator->offsetUnset($keys[$j]);
  85. if ($isCollection) {
  86. $route->addCollection($testRoute);
  87. } else {
  88. $route->add($keys[$j], $testRoute);
  89. }
  90. $i++;
  91. $keys[$j] = null;
  92. }
  93. }
  94. $code[] = sprintf(" if (0 === strpos(\$pathinfo, '%s')) {", $prefix);
  95. $indent = ' ';
  96. }
  97. foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
  98. foreach (explode("\n", $line) as $l) {
  99. $code[] = $indent.$l;
  100. }
  101. }
  102. if ($optimizable) {
  103. $code[] = " throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();";
  104. $code[] = " }\n";
  105. }
  106. } else {
  107. foreach ($this->compileRoute($route, $name, $supportsRedirections, $parentPrefix) as $line) {
  108. $code[] = $line;
  109. }
  110. }
  111. }
  112. return $code;
  113. }
  114. private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null)
  115. {
  116. $code = array();
  117. $compiledRoute = $route->compile();
  118. $conditions = array();
  119. $hasTrailingSlash = false;
  120. $matches = false;
  121. if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', str_replace(array("\n", ' '), '', $compiledRoute->getRegex()), $m)) {
  122. if ($supportsRedirections && substr($m['url'], -1) === '/') {
  123. $conditions[] = sprintf("rtrim(\$pathinfo, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
  124. $hasTrailingSlash = true;
  125. } else {
  126. $conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
  127. }
  128. } else {
  129. if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() != $parentPrefix) {
  130. $conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
  131. }
  132. $regex = str_replace(array("\n", ' '), '', $compiledRoute->getRegex());
  133. if ($supportsRedirections && $pos = strpos($regex, '/$')) {
  134. $regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
  135. $hasTrailingSlash = true;
  136. }
  137. $conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
  138. $matches = true;
  139. }
  140. $conditions = implode(' && ', $conditions);
  141. $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
  142. $code[] = <<<EOF
  143. // $name
  144. if ($conditions) {
  145. EOF;
  146. if ($req = $route->getRequirement('_method')) {
  147. $methods = explode('|', strtoupper($req));
  148. // GET and HEAD are equivalent
  149. if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
  150. $methods[] = 'HEAD';
  151. }
  152. if (1 === count($methods)) {
  153. $code[] = <<<EOF
  154. if (\$this->context->getMethod() != '$methods[0]') {
  155. \$allow[] = '$methods[0]';
  156. goto $gotoname;
  157. }
  158. EOF;
  159. } else {
  160. $methods = implode('\', \'', $methods);
  161. $code[] = <<<EOF
  162. if (!in_array(\$this->context->getMethod(), array('$methods'))) {
  163. \$allow = array_merge(\$allow, array('$methods'));
  164. goto $gotoname;
  165. }
  166. EOF;
  167. }
  168. }
  169. if ($hasTrailingSlash) {
  170. $code[] = sprintf(<<<EOF
  171. if (substr(\$pathinfo, -1) !== '/') {
  172. return \$this->redirect(\$pathinfo.'/', '%s');
  173. }
  174. EOF
  175. , $name);
  176. }
  177. if ($scheme = $route->getRequirement('_scheme')) {
  178. if (!$supportsRedirections) {
  179. throw new \LogicException('The "_scheme" requirement is only supported for route dumper that implements RedirectableUrlMatcherInterface.');
  180. }
  181. $code[] = sprintf(<<<EOF
  182. if (\$this->context->getScheme() !== '$scheme') {
  183. return \$this->redirect(\$pathinfo, '%s', '$scheme');
  184. }
  185. EOF
  186. , $name);
  187. }
  188. // optimize parameters array
  189. if (true === $matches && $compiledRoute->getDefaults()) {
  190. $code[] = sprintf(" return array_merge(\$this->mergeDefaults(\$matches, %s), array('_route' => '%s'));"
  191. , str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
  192. } elseif (true === $matches) {
  193. $code[] = sprintf(" \$matches['_route'] = '%s';", $name);
  194. $code[] = sprintf(" return \$matches;", $name);
  195. } elseif ($compiledRoute->getDefaults()) {
  196. $code[] = sprintf(' return %s;', str_replace("\n", '', var_export(array_merge($compiledRoute->getDefaults(), array('_route' => $name)), true)));
  197. } else {
  198. $code[] = sprintf(" return array('_route' => '%s');", $name);
  199. }
  200. $code[] = " }";
  201. if ($req) {
  202. $code[] = " $gotoname:";
  203. }
  204. $code[] = '';
  205. return $code;
  206. }
  207. private function startClass($class, $baseClass)
  208. {
  209. return <<<EOF
  210. <?php
  211. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  212. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  213. use Symfony\Component\Routing\RequestContext;
  214. /**
  215. * $class
  216. *
  217. * This class has been auto-generated
  218. * by the Symfony Routing Component.
  219. */
  220. class $class extends $baseClass
  221. {
  222. EOF;
  223. }
  224. private function addConstructor()
  225. {
  226. return <<<EOF
  227. /**
  228. * Constructor.
  229. */
  230. public function __construct(RequestContext \$context)
  231. {
  232. \$this->context = \$context;
  233. }
  234. EOF;
  235. }
  236. private function endClass()
  237. {
  238. return <<<EOF
  239. }
  240. EOF;
  241. }
  242. }