PhpMatcherDumper.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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\ExpressionLanguage\ExpressionFunctionProviderInterface;
  12. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. /**
  16. * PhpMatcherDumper creates a PHP class able to match URLs for a given set of routes.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Tobias Schultze <http://tobion.de>
  20. * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. class PhpMatcherDumper extends MatcherDumper
  24. {
  25. private $expressionLanguage;
  26. private $signalingException;
  27. /**
  28. * @var ExpressionFunctionProviderInterface[]
  29. */
  30. private $expressionLanguageProviders = [];
  31. /**
  32. * Dumps a set of routes to a PHP class.
  33. *
  34. * Available options:
  35. *
  36. * * class: The class name
  37. * * base_class: The base class name
  38. *
  39. * @param array $options An array of options
  40. *
  41. * @return string A PHP class representing the matcher class
  42. */
  43. public function dump(array $options = [])
  44. {
  45. $options = array_replace([
  46. 'class' => 'ProjectUrlMatcher',
  47. 'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
  48. ], $options);
  49. // trailing slash support is only enabled if we know how to redirect the user
  50. $interfaces = class_implements($options['base_class']);
  51. return <<<EOF
  52. <?php
  53. use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherTrait;
  54. use Symfony\Component\Routing\RequestContext;
  55. /**
  56. * This class has been auto-generated
  57. * by the Symfony Routing Component.
  58. */
  59. class {$options['class']} extends {$options['base_class']}
  60. {
  61. use PhpMatcherTrait;
  62. public function __construct(RequestContext \$context)
  63. {
  64. \$this->context = \$context;
  65. {$this->generateProperties()} }
  66. }
  67. EOF;
  68. }
  69. public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
  70. {
  71. $this->expressionLanguageProviders[] = $provider;
  72. }
  73. /**
  74. * Generates the code for the match method implementing UrlMatcherInterface.
  75. */
  76. private function generateProperties(): string
  77. {
  78. // Group hosts by same-suffix, re-order when possible
  79. $matchHost = false;
  80. $routes = new StaticPrefixCollection();
  81. foreach ($this->getRoutes()->all() as $name => $route) {
  82. if ($host = $route->getHost()) {
  83. $matchHost = true;
  84. $host = '/'.strtr(strrev($host), '}.{', '(/)');
  85. }
  86. $routes->addRoute($host ?: '/(.*)', [$name, $route]);
  87. }
  88. if ($matchHost) {
  89. $code = '$this->matchHost = true;'."\n";
  90. $routes = $routes->populateCollection(new RouteCollection());
  91. } else {
  92. $code = '';
  93. $routes = $this->getRoutes();
  94. }
  95. list($staticRoutes, $dynamicRoutes) = $this->groupStaticRoutes($routes);
  96. $conditions = [null];
  97. $code .= $this->compileStaticRoutes($staticRoutes, $conditions);
  98. $chunkLimit = \count($dynamicRoutes);
  99. while (true) {
  100. try {
  101. $this->signalingException = new \RuntimeException('preg_match(): Compilation failed: regular expression is too large');
  102. $code .= $this->compileDynamicRoutes($dynamicRoutes, $matchHost, $chunkLimit, $conditions);
  103. break;
  104. } catch (\Exception $e) {
  105. if (1 < $chunkLimit && $this->signalingException === $e) {
  106. $chunkLimit = 1 + ($chunkLimit >> 1);
  107. continue;
  108. }
  109. throw $e;
  110. }
  111. }
  112. unset($conditions[0]);
  113. if (!$conditions) {
  114. return $this->indent($code, 2);
  115. }
  116. foreach ($conditions as $expression => $condition) {
  117. $conditions[$expression] = "case {$condition}: return {$expression};";
  118. }
  119. return $this->indent($code, 2).<<<EOF
  120. \$this->checkCondition = static function (\$condition, \$context, \$request) {
  121. switch (\$condition) {
  122. {$this->indent(implode("\n", $conditions), 4)}
  123. }
  124. };
  125. EOF;
  126. }
  127. /**
  128. * Splits static routes from dynamic routes, so that they can be matched first, using a simple switch.
  129. */
  130. private function groupStaticRoutes(RouteCollection $collection): array
  131. {
  132. $staticRoutes = $dynamicRegex = [];
  133. $dynamicRoutes = new RouteCollection();
  134. foreach ($collection->all() as $name => $route) {
  135. $compiledRoute = $route->compile();
  136. $hostRegex = $compiledRoute->getHostRegex();
  137. $regex = $compiledRoute->getRegex();
  138. if ($hasTrailingSlash = '/' !== $route->getPath()) {
  139. $pos = strrpos($regex, '$');
  140. $hasTrailingSlash = '/' === $regex[$pos - 1];
  141. $regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
  142. }
  143. if (!$compiledRoute->getPathVariables()) {
  144. $host = !$compiledRoute->getHostVariables() ? $route->getHost() : '';
  145. $url = $route->getPath();
  146. if ($hasTrailingSlash) {
  147. $url = substr($url, 0, -1);
  148. }
  149. foreach ($dynamicRegex as list($hostRx, $rx)) {
  150. if (preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
  151. $dynamicRegex[] = [$hostRegex, $regex];
  152. $dynamicRoutes->add($name, $route);
  153. continue 2;
  154. }
  155. }
  156. $staticRoutes[$url][$name] = [$route, $hasTrailingSlash];
  157. } else {
  158. $dynamicRegex[] = [$hostRegex, $regex];
  159. $dynamicRoutes->add($name, $route);
  160. }
  161. }
  162. return [$staticRoutes, $dynamicRoutes];
  163. }
  164. /**
  165. * Compiles static routes in a switch statement.
  166. *
  167. * Condition-less paths are put in a static array in the switch's default, with generic matching logic.
  168. * Paths that can match two or more routes, or have user-specified conditions are put in separate switch's cases.
  169. *
  170. * @throws \LogicException
  171. */
  172. private function compileStaticRoutes(array $staticRoutes, array &$conditions): string
  173. {
  174. if (!$staticRoutes) {
  175. return '';
  176. }
  177. $code = '';
  178. foreach ($staticRoutes as $url => $routes) {
  179. $code .= self::export($url)." => [\n";
  180. foreach ($routes as $name => list($route, $hasTrailingSlash)) {
  181. $code .= $this->compileRoute($route, $name, !$route->compile()->getHostVariables() ? $route->getHost() : $route->compile()->getHostRegex() ?: null, $hasTrailingSlash, false, $conditions);
  182. }
  183. $code .= "],\n";
  184. }
  185. if ($code) {
  186. return "\$this->staticRoutes = [\n{$this->indent($code, 1)}];\n";
  187. }
  188. return $code;
  189. }
  190. /**
  191. * Compiles a regular expression followed by a switch statement to match dynamic routes.
  192. *
  193. * The regular expression matches both the host and the pathinfo at the same time. For stellar performance,
  194. * it is built as a tree of patterns, with re-ordering logic to group same-prefix routes together when possible.
  195. *
  196. * Patterns are named so that we know which one matched (https://pcre.org/current/doc/html/pcre2syntax.html#SEC23).
  197. * This name is used to "switch" to the additional logic required to match the final route.
  198. *
  199. * Condition-less paths are put in a static array in the switch's default, with generic matching logic.
  200. * Paths that can match two or more routes, or have user-specified conditions are put in separate switch's cases.
  201. *
  202. * Last but not least:
  203. * - Because it is not possibe to mix unicode/non-unicode patterns in a single regexp, several of them can be generated.
  204. * - The same regexp can be used several times when the logic in the switch rejects the match. When this happens, the
  205. * matching-but-failing subpattern is blacklisted by replacing its name by "(*F)", which forces a failure-to-match.
  206. * To ease this backlisting operation, the name of subpatterns is also the string offset where the replacement should occur.
  207. */
  208. private function compileDynamicRoutes(RouteCollection $collection, bool $matchHost, int $chunkLimit, array &$conditions): string
  209. {
  210. if (!$collection->all()) {
  211. return '';
  212. }
  213. $code = '';
  214. $state = (object) [
  215. 'regex' => '',
  216. 'routes' => '',
  217. 'mark' => 0,
  218. 'markTail' => 0,
  219. 'hostVars' => [],
  220. 'vars' => [],
  221. ];
  222. $state->getVars = static function ($m) use ($state) {
  223. if ('_route' === $m[1]) {
  224. return '?:';
  225. }
  226. $state->vars[] = $m[1];
  227. return '';
  228. };
  229. $chunkSize = 0;
  230. $prev = null;
  231. $perModifiers = [];
  232. foreach ($collection->all() as $name => $route) {
  233. preg_match('#[a-zA-Z]*$#', $route->compile()->getRegex(), $rx);
  234. if ($chunkLimit < ++$chunkSize || $prev !== $rx[0] && $route->compile()->getPathVariables()) {
  235. $chunkSize = 1;
  236. $routes = new RouteCollection();
  237. $perModifiers[] = [$rx[0], $routes];
  238. $prev = $rx[0];
  239. }
  240. $routes->add($name, $route);
  241. }
  242. foreach ($perModifiers as list($modifiers, $routes)) {
  243. $prev = false;
  244. $perHost = [];
  245. foreach ($routes->all() as $name => $route) {
  246. $regex = $route->compile()->getHostRegex();
  247. if ($prev !== $regex) {
  248. $routes = new RouteCollection();
  249. $perHost[] = [$regex, $routes];
  250. $prev = $regex;
  251. }
  252. $routes->add($name, $route);
  253. }
  254. $prev = false;
  255. $rx = '{^(?';
  256. $code .= "\n {$state->mark} => ".self::export($rx);
  257. $state->mark += \strlen($rx);
  258. $state->regex = $rx;
  259. foreach ($perHost as list($hostRegex, $routes)) {
  260. if ($matchHost) {
  261. if ($hostRegex) {
  262. preg_match('#^.\^(.*)\$.[a-zA-Z]*$#', $hostRegex, $rx);
  263. $state->vars = [];
  264. $hostRegex = '(?i:'.preg_replace_callback('#\?P<([^>]++)>#', $state->getVars, $rx[1]).')\.';
  265. $state->hostVars = $state->vars;
  266. } else {
  267. $hostRegex = '(?:(?:[^./]*+\.)++)';
  268. $state->hostVars = [];
  269. }
  270. $state->mark += \strlen($rx = ($prev ? ')' : '')."|{$hostRegex}(?");
  271. $code .= "\n .".self::export($rx);
  272. $state->regex .= $rx;
  273. $prev = true;
  274. }
  275. $tree = new StaticPrefixCollection();
  276. foreach ($routes->all() as $name => $route) {
  277. preg_match('#^.\^(.*)\$.[a-zA-Z]*$#', $route->compile()->getRegex(), $rx);
  278. $state->vars = [];
  279. $regex = preg_replace_callback('#\?P<([^>]++)>#', $state->getVars, $rx[1]);
  280. if ($hasTrailingSlash = '/' !== $regex && '/' === $regex[-1]) {
  281. $regex = substr($regex, 0, -1);
  282. }
  283. $hasTrailingVar = (bool) preg_match('#\{\w+\}/?$#', $route->getPath());
  284. $tree->addRoute($regex, [$name, $regex, $state->vars, $route, $hasTrailingSlash, $hasTrailingVar]);
  285. }
  286. $code .= $this->compileStaticPrefixCollection($tree, $state, 0, $conditions);
  287. }
  288. if ($matchHost) {
  289. $code .= "\n .')'";
  290. $state->regex .= ')';
  291. }
  292. $rx = ")/?$}{$modifiers}";
  293. $code .= "\n .'{$rx}',";
  294. $state->regex .= $rx;
  295. $state->markTail = 0;
  296. // if the regex is too large, throw a signaling exception to recompute with smaller chunk size
  297. set_error_handler(function ($type, $message) { throw 0 === strpos($message, $this->signalingException->getMessage()) ? $this->signalingException : new \ErrorException($message); });
  298. try {
  299. preg_match($state->regex, '');
  300. } finally {
  301. restore_error_handler();
  302. }
  303. }
  304. unset($state->getVars);
  305. return "\$this->regexpList = [{$code}\n];\n"
  306. ."\$this->dynamicRoutes = [\n{$this->indent($state->routes, 1)}];\n";
  307. }
  308. /**
  309. * Compiles a regexp tree of subpatterns that matches nested same-prefix routes.
  310. *
  311. * @param \stdClass $state A simple state object that keeps track of the progress of the compilation,
  312. * and gathers the generated switch's "case" and "default" statements
  313. */
  314. private function compileStaticPrefixCollection(StaticPrefixCollection $tree, \stdClass $state, int $prefixLen, array &$conditions): string
  315. {
  316. $code = '';
  317. $prevRegex = null;
  318. $routes = $tree->getRoutes();
  319. foreach ($routes as $i => $route) {
  320. if ($route instanceof StaticPrefixCollection) {
  321. $prevRegex = null;
  322. $prefix = substr($route->getPrefix(), $prefixLen);
  323. $state->mark += \strlen($rx = "|{$prefix}(?");
  324. $code .= "\n .".self::export($rx);
  325. $state->regex .= $rx;
  326. $code .= $this->indent($this->compileStaticPrefixCollection($route, $state, $prefixLen + \strlen($prefix), $conditions));
  327. $code .= "\n .')'";
  328. $state->regex .= ')';
  329. ++$state->markTail;
  330. continue;
  331. }
  332. list($name, $regex, $vars, $route, $hasTrailingSlash, $hasTrailingVar) = $route;
  333. $compiledRoute = $route->compile();
  334. $vars = array_merge($state->hostVars, $vars);
  335. if ($compiledRoute->getRegex() === $prevRegex) {
  336. $state->routes = substr_replace($state->routes, $this->compileRoute($route, $name, $vars, $hasTrailingSlash, $hasTrailingVar, $conditions), -3, 0);
  337. continue;
  338. }
  339. $state->mark += 3 + $state->markTail + \strlen($regex) - $prefixLen;
  340. $state->markTail = 2 + \strlen($state->mark);
  341. $rx = sprintf('|%s(*:%s)', substr($regex, $prefixLen), $state->mark);
  342. $code .= "\n .".self::export($rx);
  343. $state->regex .= $rx;
  344. $prevRegex = $compiledRoute->getRegex();
  345. $state->routes .= sprintf("%s => [\n%s],\n", $state->mark, $this->compileRoute($route, $name, $vars, $hasTrailingSlash, $hasTrailingVar, $conditions));
  346. }
  347. return $code;
  348. }
  349. /**
  350. * Compiles a single Route to PHP code used to match it against the path info.
  351. */
  352. private function compileRoute(Route $route, string $name, $vars, bool $hasTrailingSlash, bool $hasTrailingVar, array &$conditions): string
  353. {
  354. $defaults = $route->getDefaults();
  355. if (isset($defaults['_canonical_route'])) {
  356. $name = $defaults['_canonical_route'];
  357. unset($defaults['_canonical_route']);
  358. }
  359. if ($condition = $route->getCondition()) {
  360. $condition = $this->getExpressionLanguage()->compile($condition, ['context', 'request']);
  361. $condition = $conditions[$condition] ?? $conditions[$condition] = (false !== strpos($condition, '$request') ? 1 : -1) * \count($conditions);
  362. } else {
  363. $condition = 'null';
  364. }
  365. return sprintf(
  366. " [%s, %s, %s, %s, %s, %s, %s],\n",
  367. self::export(['_route' => $name] + $defaults),
  368. self::export($vars),
  369. self::export(array_flip($route->getMethods()) ?: null),
  370. self::export(array_flip($route->getSchemes()) ?: null),
  371. self::export($hasTrailingSlash),
  372. self::export($hasTrailingVar),
  373. $condition
  374. );
  375. }
  376. private function getExpressionLanguage()
  377. {
  378. if (null === $this->expressionLanguage) {
  379. if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
  380. throw new \LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
  381. }
  382. $this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
  383. }
  384. return $this->expressionLanguage;
  385. }
  386. private function indent($code, $level = 1)
  387. {
  388. $code = preg_replace('/ => \[\n (\[.+),\n\],/', ' => [$1],', $code);
  389. return preg_replace('/^./m', str_repeat(' ', $level).'$0', $code);
  390. }
  391. /**
  392. * @internal
  393. */
  394. public static function export($value): string
  395. {
  396. if (null === $value) {
  397. return 'null';
  398. }
  399. if (!\is_array($value)) {
  400. if (\is_object($value)) {
  401. throw new \InvalidArgumentException('Symfony\Component\Routing\Route cannot contain objects.');
  402. }
  403. return str_replace("\n", '\'."\n".\'', var_export($value, true));
  404. }
  405. if (!$value) {
  406. return '[]';
  407. }
  408. $i = 0;
  409. $export = '[';
  410. foreach ($value as $k => $v) {
  411. if ($i === $k) {
  412. ++$i;
  413. } else {
  414. $export .= self::export($k).' => ';
  415. if (\is_int($k) && $i < $k) {
  416. $i = 1 + $k;
  417. }
  418. }
  419. $export .= self::export($v).', ';
  420. }
  421. return substr_replace($export, ']', -2);
  422. }
  423. }