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
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
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.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.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()
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-03-23 18:24:18 +00:00
|
|
|
private function addMatcher()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$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) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$compiledRoute = $route->compile();
|
|
|
|
$conditions = array();
|
2011-02-05 18:06:40 +00:00
|
|
|
$hasTrailingSlash = false;
|
2011-04-05 10:54:15 +01:00
|
|
|
$matches = 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-03-09 16:25:44 +00:00
|
|
|
$conditions[] = sprintf("rtrim(\$pathinfo, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
|
2011-02-05 18:06:40 +00:00
|
|
|
$hasTrailingSlash = true;
|
|
|
|
} else {
|
2011-03-09 16:25:44 +00:00
|
|
|
$conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
|
2011-02-05 18:06:40 +00:00
|
|
|
}
|
2010-12-20 17:13:02 +00:00
|
|
|
} else {
|
|
|
|
if ($compiledRoute->getStaticPrefix()) {
|
2011-03-09 16:25:44 +00:00
|
|
|
$conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
|
2010-12-20 17:13:02 +00:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2011-02-05 18:06:40 +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);
|
2011-02-05 18:06:40 +00:00
|
|
|
$hasTrailingSlash = true;
|
|
|
|
}
|
2011-04-05 11:13:47 +01:00
|
|
|
$conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
|
2010-12-20 17:13:02 +00:00
|
|
|
|
2011-04-05 10:54:15 +01:00
|
|
|
$matches = true;
|
2010-12-20 17:13:02 +00:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$conditions = implode(' && ', $conditions);
|
|
|
|
|
2011-03-24 08:03:59 +00:00
|
|
|
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
|
2011-04-05 10:54:15 +01:00
|
|
|
|
2011-02-05 18:06:40 +00:00
|
|
|
$code[] = <<<EOF
|
2011-03-21 12:56:53 +00:00
|
|
|
// $name
|
2010-05-08 14:53:22 +01:00
|
|
|
if ($conditions) {
|
2011-02-05 18:06:40 +00:00
|
|
|
EOF;
|
|
|
|
|
2011-03-21 12:56:53 +00:00
|
|
|
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'));
|
2011-03-24 08:03:59 +00:00
|
|
|
goto $gotoname;
|
2011-03-21 12:56:53 +00:00
|
|
|
}
|
|
|
|
EOF;
|
|
|
|
}
|
|
|
|
|
2011-02-05 18:06:40 +00:00
|
|
|
if ($hasTrailingSlash) {
|
|
|
|
$code[] = sprintf(<<<EOF
|
2011-03-09 16:25:44 +00:00
|
|
|
if (substr(\$pathinfo, -1) !== '/') {
|
|
|
|
return array('_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction', 'url' => \$this->context['base_url'].\$pathinfo.'/', 'permanent' => true, '_route' => '%s');
|
2011-02-05 18:06:40 +00:00
|
|
|
}
|
|
|
|
EOF
|
2011-04-05 11:13:47 +01:00
|
|
|
, $name);
|
2011-02-05 18:06:40 +00:00
|
|
|
}
|
|
|
|
|
2011-04-05 10:54:15 +01:00
|
|
|
// 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[] = " }";
|
2011-03-21 12:56:53 +00:00
|
|
|
|
|
|
|
if ($req) {
|
2011-04-05 10:54:15 +01:00
|
|
|
$code[] = " $gotoname:";
|
2011-03-21 12:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$code[] = '';
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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
|
|
|
|
2011-03-09 16:25:44 +00:00
|
|
|
public function match(\$pathinfo)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-03-21 12:56:53 +00:00
|
|
|
\$allow = array();
|
|
|
|
|
2010-02-17 13:53:31 +00:00
|
|
|
$code
|
2011-03-21 12:56:53 +00:00
|
|
|
throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new NotFoundException();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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
|
|
|
|
2011-03-23 18:24:18 +00:00
|
|
|
private function startClass($class, $baseClass)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
return <<<EOF
|
2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
2011-03-21 12:56:53 +00:00
|
|
|
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;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-03-23 18:24:18 +00:00
|
|
|
private function addConstructor()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2011-03-23 18:24:18 +00:00
|
|
|
private function endClass()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|