This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

168 lines
4.5 KiB
PHP
Raw Normal View History

2010-02-17 13:53:31 +00:00
<?php
/*
* This file is part of the Symfony package.
2010-02-17 13:53:31 +00:00
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2010-02-17 13:53:31 +00:00
*/
namespace Symfony\Component\Routing\Matcher\Dumper;
use Symfony\Component\Routing\Route;
2010-02-17 13:53:31 +00:00
/**
* PhpMatcherDumper creates a PHP class able to match URLs for a given set of routes.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2010-02-17 13:53:31 +00:00
*/
class PhpMatcherDumper extends MatcherDumper
{
/**
* Dumps a set of routes to a PHP class.
*
* Available options:
*
* * class: The class name
* * base_class: The base class name
*
* @param array $options An array of options
*
* @return string A PHP class representing the matcher class
*/
public function dump(array $options = array())
2010-02-17 13:53:31 +00:00
{
$options = array_merge(array(
'class' => 'ProjectUrlMatcher',
'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
), $options);
return
$this->startClass($options['class'], $options['base_class']).
$this->addConstructor().
$this->addMatcher().
$this->endClass()
;
}
protected function addMatcher()
{
$code = array();
2010-02-17 13:53:31 +00:00
foreach ($this->routes->all() as $name => $route) {
$compiledRoute = $route->compile();
2010-02-17 13:53:31 +00:00
$conditions = array();
2010-02-17 13:53:31 +00:00
if ($req = $route->getRequirement('_method')) {
$conditions[] = sprintf("isset(\$this->context['method']) && preg_match('#^(%s)$#xi', \$this->context['method'])", $req);
}
2010-02-17 13:53:31 +00:00
$hasTrailingSlash = false;
if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
if (substr($m['url'], -1) === '/' && $m['url'] !== '/') {
$conditions[] = sprintf("rtrim(\$url, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
$hasTrailingSlash = true;
} else {
$conditions[] = sprintf("\$url === '%s'", str_replace('\\', '', $m['url']));
}
$matches = 'array()';
} else {
if ($compiledRoute->getStaticPrefix()) {
$conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
}
2010-02-17 13:53:31 +00:00
$regex = $compiledRoute->getRegex();
if ($pos = strpos($regex, '/$')) {
$regex = substr($regex, 0, $pos) . '/?$' . substr($regex, $pos+2);
$conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $regex);
$hasTrailingSlash = true;
} else {
$conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $regex);
}
$matches = '$matches';
}
2010-02-17 13:53:31 +00:00
$conditions = implode(' && ', $conditions);
$code[] = <<<EOF
if ($conditions) {
EOF;
if ($hasTrailingSlash) {
$code[] = sprintf(<<<EOF
if (substr(\$url, -1) !== '/') {
return array('_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction', 'url' => \$this->context['base_url'].\$url.'/', 'permanent' => true, '_route' => '%s');
}
EOF
, $name);
}
$code[] = sprintf(<<<EOF
return array_merge(\$this->mergeDefaults($matches, %s), array('_route' => '%s'));
}
2010-02-17 13:53:31 +00:00
EOF
, str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
}
2010-02-17 13:53:31 +00:00
$code = implode("\n", $code);
2010-02-17 13:53:31 +00:00
return <<<EOF
2010-02-17 13:53:31 +00:00
public function match(\$url)
{
\$url = \$this->normalizeUrl(\$url);
2010-02-17 13:53:31 +00:00
$code
return false;
}
2010-02-17 13:53:31 +00:00
EOF;
}
2010-02-17 13:53:31 +00:00
protected function startClass($class, $baseClass)
{
return <<<EOF
2010-02-17 13:53:31 +00:00
<?php
/**
* $class
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class $class extends $baseClass
{
EOF;
}
protected function addConstructor()
{
return <<<EOF
/**
* Constructor.
*/
public function __construct(array \$context = array(), array \$defaults = array())
{
\$this->context = \$context;
\$this->defaults = \$defaults;
}
2010-02-17 13:53:31 +00:00
EOF;
}
2010-02-17 13:53:31 +00:00
protected function endClass()
{
return <<<EOF
2010-02-17 13:53:31 +00:00
}
EOF;
}
2010-02-17 13:53:31 +00:00
}