2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +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
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +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.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
class PhpMatcherDumper extends MatcherDumper
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$options = array_merge(array(
|
|
|
|
'class' => 'ProjectUrlMatcher',
|
2010-08-20 22:09:55 +01:00
|
|
|
'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
|
2010-05-06 12:25:53 +01:00
|
|
|
), $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
|
|
|
|
2010-11-25 19:38:04 +00:00
|
|
|
foreach ($this->routes->all() as $name => $route) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$compiledRoute = $route->compile();
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$conditions = array();
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($req = $route->getRequirement('_method')) {
|
2010-11-25 14:49:33 +00:00
|
|
|
$conditions[] = sprintf("isset(\$this->context['method']) && preg_match('#^(%s)$#xi', \$this->context['method'])", $req);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2011-02-05 18:06:40 +00:00
|
|
|
$hasTrailingSlash = false;
|
2010-12-20 17:13:02 +00:00
|
|
|
if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
|
2011-02-26 07:56:44 +00:00
|
|
|
if (substr($m['url'], -1) === '/') {
|
2011-02-05 18:06:40 +00:00
|
|
|
$conditions[] = sprintf("rtrim(\$url, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
|
|
|
|
$hasTrailingSlash = true;
|
|
|
|
} else {
|
|
|
|
$conditions[] = sprintf("\$url === '%s'", str_replace('\\', '', $m['url']));
|
|
|
|
}
|
2010-12-20 17:13:02 +00:00
|
|
|
|
|
|
|
$matches = 'array()';
|
|
|
|
} else {
|
|
|
|
if ($compiledRoute->getStaticPrefix()) {
|
|
|
|
$conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2011-02-05 18:06:40 +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);
|
|
|
|
}
|
2010-12-20 17:13:02 +00:00
|
|
|
|
|
|
|
$matches = '$matches';
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$conditions = implode(' && ', $conditions);
|
|
|
|
|
2011-02-05 18:06:40 +00:00
|
|
|
$code[] = <<<EOF
|
2010-05-08 14:53:22 +01:00
|
|
|
if ($conditions) {
|
2011-02-05 18:06:40 +00:00
|
|
|
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
|
2010-12-20 17:13:02 +00:00
|
|
|
return array_merge(\$this->mergeDefaults($matches, %s), array('_route' => '%s'));
|
2010-05-08 14:53:22 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
|
|
|
EOF
|
2010-05-06 12:25:53 +01:00
|
|
|
, str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$code = implode("\n", $code);
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return <<<EOF
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function match(\$url)
|
|
|
|
{
|
|
|
|
\$url = \$this->normalizeUrl(\$url);
|
2010-02-17 13:53:31 +00:00
|
|
|
|
|
|
|
$code
|
2010-05-06 12:25:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
|
|
|
EOF;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01: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;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function endClass()
|
|
|
|
{
|
|
|
|
return <<<EOF
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EOF;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|