2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Routing\Matcher\Dumper;
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Routing\Route;
|
2010-02-17 13:53:31 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-05-06 12:25:53 +01:00
|
|
|
$req = array_map('strtolower', (array) $req);
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$conditions[] = sprintf("isset(\$this->context['method']) && in_array(strtolower(\$this->context['method']), %s)", str_replace("\n", '', var_export($req, true)));
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($compiledRoute->getStaticPrefix()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $compiledRoute->getRegex());
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$conditions = implode(' && ', $conditions);
|
|
|
|
|
|
|
|
$code[] = sprintf(<<<EOF
|
2010-05-08 14:53:22 +01:00
|
|
|
if ($conditions) {
|
2010-05-06 12:25:53 +01: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
|
|
|
}
|