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/Components/Routing/Matcher/Dumper/PhpMatcherDumper.php

141 lines
3.3 KiB
PHP
Raw Normal View History

2010-02-17 13:53:31 +00:00
<?php
namespace Symfony\Components\Routing\Matcher\Dumper;
use Symfony\Components\Routing\Route;
/*
* 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.
*
* @package Symfony
* @subpackage Components_Routing
2010-02-17 13:53:31 +00:00
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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\\Components\\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->getRoutes() 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'))
{
$req = array_map('strtolower', (array) $req);
2010-02-17 13:53:31 +00: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
if ($compiledRoute->getStaticPrefix())
{
$conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
}
2010-02-17 13:53:31 +00:00
$conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $compiledRoute->getRegex());
2010-02-17 13:53:31 +00:00
$conditions = implode(' && ', $conditions);
$code[] = sprintf(<<<EOF
if ($conditions)
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
}