RouteCompiler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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;
  11. /**
  12. * RouteCompiler compiles Route instances to CompiledRoute instances.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class RouteCompiler implements RouteCompilerInterface
  17. {
  18. /**
  19. * Compiles the current route instance.
  20. *
  21. * @param Route $route A Route instance
  22. *
  23. * @return CompiledRoute A CompiledRoute instance
  24. */
  25. public function compile(Route $route)
  26. {
  27. $pattern = $route->getPattern();
  28. $len = strlen($pattern);
  29. $tokens = array();
  30. $variables = array();
  31. $pos = 0;
  32. preg_match_all('#.\{([\w\d_]+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  33. foreach ($matches as $match) {
  34. if ($text = substr($pattern, $pos, $match[0][1] - $pos)) {
  35. $tokens[] = array('text', $text);
  36. }
  37. $seps = array($pattern[$pos]);
  38. $pos = $match[0][1] + strlen($match[0][0]);
  39. $var = $match[1][0];
  40. if ($req = $route->getRequirement($var)) {
  41. $regexp = $req;
  42. } else {
  43. if ($pos !== $len) {
  44. $seps[] = $pattern[$pos];
  45. }
  46. $regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), '#'));
  47. }
  48. $tokens[] = array('variable', $match[0][0][0], $regexp, $var);
  49. $variables[] = $var;
  50. }
  51. if ($pos < $len) {
  52. $tokens[] = array('text', substr($pattern, $pos));
  53. }
  54. // find the first optional token
  55. $firstOptional = INF;
  56. for ($i = count($tokens) - 1; $i >= 0; $i--) {
  57. if ('variable' === $tokens[$i][0] && $route->hasDefault($tokens[$i][3])) {
  58. $firstOptional = $i;
  59. } else {
  60. break;
  61. }
  62. }
  63. // compute the matching regexp
  64. $regex = '';
  65. $indent = 1;
  66. if (1 === count($tokens) && 0 === $firstOptional) {
  67. $token = $tokens[0];
  68. ++$indent;
  69. $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?:\n", preg_quote($token[1], '#'));
  70. $regex .= str_repeat(' ', $indent * 4).sprintf("(?P<%s>%s)\n", $token[3], $token[2]);
  71. } else {
  72. foreach ($tokens as $i => $token) {
  73. if ('text' === $token[0]) {
  74. $regex .= str_repeat(' ', $indent * 4).preg_quote($token[1], '#')."\n";
  75. } else {
  76. if ($i >= $firstOptional) {
  77. $regex .= str_repeat(' ', $indent * 4)."(?:\n";
  78. ++$indent;
  79. }
  80. $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?P<%s>%s)\n", preg_quote($token[1], '#'), $token[3], $token[2]);
  81. }
  82. }
  83. }
  84. while (--$indent) {
  85. $regex .= str_repeat(' ', $indent * 4).")?\n";
  86. }
  87. return new CompiledRoute(
  88. $route,
  89. 'text' === $tokens[0][0] ? $tokens[0][1] : '',
  90. sprintf("#^\n%s$#x", $regex),
  91. array_reverse($tokens),
  92. $variables
  93. );
  94. }
  95. }