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

187 lines
5.6 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@symfony.com>
2010-02-17 13:53:31 +00:00
*
* 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@symfony.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()
;
}
2011-03-23 18:24:18 +00:00
private function addMatcher()
{
$code = array();
2010-02-17 13:53:31 +00:00
2011-03-23 18:24:18 +00:00
foreach ($this->getRoutes()->all() as $name => $route) {
$compiledRoute = $route->compile();
$conditions = array();
$hasTrailingSlash = false;
$matches = false;
if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
if (substr($m['url'], -1) === '/') {
$conditions[] = sprintf("rtrim(\$pathinfo, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
$hasTrailingSlash = true;
} else {
$conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
}
} else {
if ($compiledRoute->getStaticPrefix()) {
$conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
}
2010-02-17 13:53:31 +00:00
$regex = $compiledRoute->getRegex();
if ($pos = strpos($regex, '/$')) {
2011-04-05 11:13:47 +01:00
$regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
$hasTrailingSlash = true;
}
2011-04-05 11:13:47 +01:00
$conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
$matches = true;
}
2010-02-17 13:53:31 +00:00
$conditions = implode(' && ', $conditions);
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
$code[] = <<<EOF
// $name
if ($conditions) {
EOF;
if ($req = $route->getRequirement('_method')) {
$req = implode('\', \'', array_map('strtolower', explode('|', $req)));
$code[] = <<<EOF
if (isset(\$this->context['method']) && !in_array(strtolower(\$this->context['method']), array('$req'))) {
\$allow = array_merge(\$allow, array('$req'));
goto $gotoname;
}
EOF;
}
if ($hasTrailingSlash) {
$code[] = sprintf(<<<EOF
if (substr(\$pathinfo, -1) !== '/') {
return array('_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction', 'url' => \$this->context['base_url'].\$pathinfo.'/', 'permanent' => true, '_route' => '%s');
}
EOF
2011-04-05 11:13:47 +01:00
, $name);
}
// optimize parameters array
if (true === $matches && $compiledRoute->getDefaults()) {
$code[] = sprintf(" return array_merge(\$this->mergeDefaults(\$matches, %s), array('_route' => '%s'));"
, str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
} elseif (true === $matches) {
$code[] = sprintf(" \$matches['_route'] = '%s';\n return \$matches;", $name);
} elseif ($compiledRoute->getDefaults()) {
$code[] = sprintf(' return %s;', str_replace("\n", '', var_export(array_merge($compiledRoute->getDefaults(), array('_route' => $name)), true)));
} else {
$code[] = sprintf(" return array('_route' => '%s');", $name);
}
$code[] = " }";
if ($req) {
$code[] = " $gotoname:";
}
$code[] = '';
}
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(\$pathinfo)
{
\$allow = array();
2010-02-17 13:53:31 +00:00
$code
throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new NotFoundException();
}
2010-02-17 13:53:31 +00:00
EOF;
}
2010-02-17 13:53:31 +00:00
2011-03-23 18:24:18 +00:00
private function startClass($class, $baseClass)
{
return <<<EOF
2010-02-17 13:53:31 +00:00
<?php
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
2010-02-17 13:53:31 +00:00
/**
* $class
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class $class extends $baseClass
{
EOF;
}
2011-03-23 18:24:18 +00:00
private 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
2011-03-23 18:24:18 +00:00
private function endClass()
{
return <<<EOF
2010-02-17 13:53:31 +00:00
}
EOF;
}
2010-02-17 13:53:31 +00:00
}