RouteCompiler.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\Tests\Component\Routing;
  11. use Symfony\Component\Routing\RouteCompiler as BaseRouteCompiler;
  12. use Symfony\Component\Routing\Route;
  13. class RouteCompiler extends BaseRouteCompiler
  14. {
  15. protected function tokenizeBufferBefore(&$buffer, &$tokens, &$afterASeparator, &$currentSeparator)
  16. {
  17. if ($afterASeparator && preg_match('#^=([\w\d_]+)#', $buffer, $match)) {
  18. // a labelled variable
  19. $this->tokens[] = array('label', $currentSeparator, $match[0], $match[1]);
  20. $currentSeparator = '';
  21. $buffer = substr($buffer, strlen($match[0]));
  22. $afterASeparator = false;
  23. } else {
  24. return false;
  25. }
  26. }
  27. protected function compileForLabel($separator, $name, $variable)
  28. {
  29. if (null === $requirement = $this->route->getRequirement($variable)) {
  30. $requirement = $this->options['variable_content_regex'];
  31. }
  32. $this->segments[] = preg_quote($separator, '#').$variable.$separator.'(?P<'.$variable.'>'.$requirement.')';
  33. $this->variables[$variable] = $name;
  34. if (!$this->route->getDefault($variable)) {
  35. $this->firstOptional = count($this->segments);
  36. }
  37. }
  38. protected function generateForLabel($optional, $tparams, $separator, $name, $variable)
  39. {
  40. if (!empty($tparams[$variable]) && (!$optional || !isset($this->defaults[$variable]) || $tparams[$variable] != $this->defaults[$variable])) {
  41. return $variable.'/'.urlencode($tparams[$variable]);
  42. }
  43. }
  44. }