PhpMatcherDumper.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. $routeIterator = $routes->getIterator();
  64. $keys = array_keys($routeIterator->getArrayCopy());
  65. $keysCount = count($keys);
  66. $i = 0;
  67. foreach ($routeIterator as $name => $route) {
  68. $i++;
  69. if ($route instanceof RouteCollection) {
  70. $prefix = $route->getPrefix();
  71. $optimizable = $prefix && count($route->all()) > 1 && false === strpos($route->getPrefix(), '{');
  72. $indent = '';
  73. if ($optimizable) {
  74. for ($j = $i; $j < $keysCount; $j++) {
  75. if ($keys[$j] === null) {
  76. continue;
  77. }
  78. $testRoute = $routeIterator->offsetGet($keys[$j]);
  79. $isCollection = ($testRoute instanceof RouteCollection);
  80. $testPrefix = $isCollection ? $testRoute->getPrefix() : $testRoute->getPattern();
  81. if (0 === strpos($testPrefix, $prefix)) {
  82. $routeIterator->offsetUnset($keys[$j]);
  83. if ($isCollection) {
  84. $route->addCollection($testRoute);
  85. }
  86. else {
  87. $route->add($keys[$j], $testRoute);
  88. }
  89. $i++;
  90. $keys[$j] = null;
  91. }
  92. }
  93. $code[] = sprintf(" if (0 === strpos(\$pathinfo, '%s')) {", $prefix);
  94. $indent = ' ';
  95. }
  96. foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
  97. foreach (explode("\n", $line) as $l) {
  98. $code[] = $indent.$l;
  99. }
  100. }
  101. if ($optimizable) {
  102. $code[] = " throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();";
  103. $code[] = " }\n";
  104. }
  105. } else {
  106. foreach ($this->compileRoute($route, $name, $supportsRedirections, $parentPrefix) as $line) {
  107. $code[] = $line;
  108. }
  109. }
  110. }
  111. return $code;
  112. }
  113. private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null)
  114. {
  115. $code = array();
  116. $compiledRoute = $route->compile();
  117. $conditions = array();
  118. $hasTrailingSlash = false;
  119. $matches = false;
  120. if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', str_replace(array("\n", ' '), '', $compiledRoute->getRegex()), $m)) {
  121. if ($supportsRedirections && substr($m['url'], -1) === '/') {
  122. $conditions[] = sprintf("rtrim(\$pathinfo, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
  123. $hasTrailingSlash = true;
  124. } else {
  125. $conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
  126. }
  127. } else {
  128. if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() != $parentPrefix) {
  129. $conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
  130. }
  131. $regex = str_replace(array("\n", ' '), '', $compiledRoute->getRegex());
  132. if ($supportsRedirections && $pos = strpos($regex, '/$')) {
  133. $regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
  134. $hasTrailingSlash = true;
  135. }
  136. $conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
  137. $matches = true;
  138. }
  139. $conditions = implode(' && ', $conditions);
  140. $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
  141. $code[] = <<<EOF
  142. // $name
  143. if ($conditions) {
  144. EOF;
  145. if ($req = $route->getRequirement('_method')) {
  146. $methods = explode('|', strtoupper($req));
  147. // GET and HEAD are equivalent
  148. if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
  149. $methods[] = 'HEAD';
  150. }
  151. if (1 === count($methods)) {
  152. $code[] = <<<EOF
  153. if (\$this->context->getMethod() != '$methods[0]') {
  154. \$allow[] = '$methods[0]';
  155. goto $gotoname;
  156. }
  157. EOF;
  158. } else {
  159. $methods = implode('\', \'', $methods);
  160. $code[] = <<<EOF
  161. if (!in_array(\$this->context->getMethod(), array('$methods'))) {
  162. \$allow = array_merge(\$allow, array('$methods'));
  163. goto $gotoname;
  164. }
  165. EOF;
  166. }
  167. }
  168. if ($hasTrailingSlash) {
  169. $code[] = sprintf(<<<EOF
  170. if (substr(\$pathinfo, -1) !== '/') {
  171. return \$this->redirect(\$pathinfo.'/', '%s');
  172. }
  173. EOF
  174. , $name);
  175. }
  176. if ($scheme = $route->getRequirement('_scheme')) {
  177. if (!$supportsRedirections) {
  178. throw new \LogicException('The "_scheme" requirement is only supported for route dumper that implements RedirectableUrlMatcherInterface.');
  179. }
  180. $code[] = sprintf(<<<EOF
  181. if (\$this->context->getScheme() !== '$scheme') {
  182. return \$this->redirect(\$pathinfo, '%s', '$scheme');
  183. }
  184. EOF
  185. , $name);
  186. }
  187. // optimize parameters array
  188. if (true === $matches && $compiledRoute->getDefaults()) {
  189. $code[] = sprintf(" return array_merge(\$this->mergeDefaults(\$matches, %s), array('_route' => '%s'));"
  190. , str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
  191. } elseif (true === $matches) {
  192. $code[] = sprintf(" \$matches['_route'] = '%s';", $name);
  193. $code[] = sprintf(" return \$matches;", $name);
  194. } elseif ($compiledRoute->getDefaults()) {
  195. $code[] = sprintf(' return %s;', str_replace("\n", '', var_export(array_merge($compiledRoute->getDefaults(), array('_route' => $name)), true)));
  196. } else {
  197. $code[] = sprintf(" return array('_route' => '%s');", $name);
  198. }
  199. $code[] = " }";
  200. if ($req) {
  201. $code[] = " $gotoname:";
  202. }
  203. $code[] = '';
  204. return $code;
  205. }
  206. private function startClass($class, $baseClass)
  207. {
  208. return <<<EOF
  209. <?php
  210. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  211. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  212. use Symfony\Component\Routing\RequestContext;
  213. /**
  214. * $class
  215. *
  216. * This class has been auto-generated
  217. * by the Symfony Routing Component.
  218. */
  219. class $class extends $baseClass
  220. {
  221. EOF;
  222. }
  223. private function addConstructor()
  224. {
  225. return <<<EOF
  226. /**
  227. * Constructor.
  228. */
  229. public function __construct(RequestContext \$context)
  230. {
  231. \$this->context = \$context;
  232. }
  233. EOF;
  234. }
  235. private function endClass()
  236. {
  237. return <<<EOF
  238. }
  239. EOF;
  240. }
  241. }