Skip to content

Commit

Permalink
[Routing] removed cyclic reference Route<->CompiledRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion committed Aug 23, 2012
1 parent 2cf3cb5 commit 4f57d69
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 83 deletions.
55 changes: 1 addition & 54 deletions src/Symfony/Component/Routing/CompiledRoute.php
Expand Up @@ -18,7 +18,6 @@
*/
class CompiledRoute
{
private $route;
private $variables;
private $tokens;
private $staticPrefix;
Expand All @@ -27,31 +26,19 @@ class CompiledRoute
/**
* Constructor.
*
* @param Route $route A original Route instance
* @param string $staticPrefix The static prefix of the compiled route
* @param string $regex The regular expression to use to match this route
* @param array $tokens An array of tokens to use to generate URL for this route
* @param array $variables An array of variables
*/
public function __construct(Route $route, $staticPrefix, $regex, array $tokens, array $variables)
public function __construct($staticPrefix, $regex, array $tokens, array $variables)
{
$this->route = $route;
$this->staticPrefix = $staticPrefix;
$this->regex = $regex;
$this->tokens = $tokens;
$this->variables = $variables;
}

/**
* Returns the Route instance.
*
* @return Route A Route instance
*/
public function getRoute()
{
return $this->route;
}

/**
* Returns the static prefix.
*
Expand Down Expand Up @@ -91,44 +78,4 @@ public function getVariables()
{
return $this->variables;
}

/**
* Returns the pattern.
*
* @return string The pattern
*/
public function getPattern()
{
return $this->route->getPattern();
}

/**
* Returns the options.
*
* @return array The options
*/
public function getOptions()
{
return $this->route->getOptions();
}

/**
* Returns the defaults.
*
* @return array The defaults
*/
public function getDefaults()
{
return $this->route->getDefaults();
}

/**
* Returns the requirements.
*
* @return array The requirements
*/
public function getRequirements()
{
return $this->route->getRequirements();
}
}
Expand Up @@ -90,8 +90,8 @@ private function generateDeclaredRoutes()

$properties = array();
$properties[] = $compiledRoute->getVariables();
$properties[] = $compiledRoute->getDefaults();
$properties[] = $compiledRoute->getRequirements();
$properties[] = $route->getDefaults();
$properties[] = $route->getRequirements();
$properties[] = $compiledRoute->getTokens();

$routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
Expand Down
Expand Up @@ -271,14 +271,14 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
}

// optimize parameters array
if (true === $matches && $compiledRoute->getDefaults()) {
if (true === $matches && $route->getDefaults()) {
$code .= sprintf(" return array_merge(\$this->mergeDefaults(\$matches, %s), array('_route' => '%s'));\n"
, str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
, str_replace("\n", '', var_export($route->getDefaults(), true)), $name);
} elseif (true === $matches) {
$code .= sprintf(" \$matches['_route'] = '%s';\n\n", $name);
$code .= " return \$matches;\n";
} elseif ($compiledRoute->getDefaults()) {
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_merge($compiledRoute->getDefaults(), array('_route' => $name)), true)));
} elseif ($route->getDefaults()) {
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_merge($route->getDefaults(), array('_route' => $name)), true)));
} else {
$code .= sprintf(" return array('_route' => '%s');\n", $name);
}
Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Component/Routing/RouteCompiler.php
Expand Up @@ -85,7 +85,6 @@ public function compile(Route $route)
}

return new CompiledRoute(
$route,
'text' === $tokens[0][0] ? $tokens[0][1] : '',
self::REGEX_DELIMITER.'^'.$regexp.'$'.self::REGEX_DELIMITER.'s',
array_reverse($tokens),
Expand Down
27 changes: 5 additions & 22 deletions src/Symfony/Component/Routing/Tests/CompiledRouteTest.php
Expand Up @@ -12,32 +12,15 @@
namespace Symfony\Component\Routing\Tests;

use Symfony\Component\Routing\CompiledRoute;
use Symfony\Component\Routing\Route;

class CompiledRouteTest extends \PHPUnit_Framework_TestCase
{
public function testAccessors()
{
$route = new Route('/{foo}', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'));

$compiled = new CompiledRoute($route, 'prefix', 'regex', array('tokens'), array('variables'));
$this->assertEquals($route, $compiled->getRoute(), '__construct() takes a route as its first argument');
$this->assertEquals('prefix', $compiled->getStaticPrefix(), '__construct() takes a static prefix as its second argument');
$this->assertEquals('regex', $compiled->getRegex(), '__construct() takes a regexp as its third argument');
$this->assertEquals(array('tokens'), $compiled->getTokens(), '__construct() takes an array of tokens as its fourth argument');
$this->assertEquals(array('variables'), $compiled->getVariables(), '__construct() takes an array of variables as its fifth argument');
}

public function testgetPatterngetDefaultsgetOptionsgetRequirements()
{
$route = new Route('/{foo}', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'));

$compiled = new CompiledRoute($route, 'prefix', 'regex', array('tokens'), array('variables'));
$this->assertEquals('/{foo}', $compiled->getPattern(), '->getPattern() returns the route pattern');
$this->assertEquals(array('foo' => 'bar'), $compiled->getDefaults(), '->getDefaults() returns the route defaults');
$this->assertEquals(array('foo' => '\d+'), $compiled->getRequirements(), '->getRequirements() returns the route requirements');
$this->assertEquals(array_merge(array(
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
), array('foo' => 'bar')), $compiled->getOptions(), '->getOptions() returns the route options');
$compiled = new CompiledRoute('prefix', 'regex', array('tokens'), array('variables'));
$this->assertEquals('prefix', $compiled->getStaticPrefix(), '__construct() takes a static prefix as its first argument');
$this->assertEquals('regex', $compiled->getRegex(), '__construct() takes a regexp as its second argument');
$this->assertEquals(array('tokens'), $compiled->getTokens(), '__construct() takes an array of tokens as its third argument');
$this->assertEquals(array('variables'), $compiled->getVariables(), '__construct() takes an array of variables as its forth argument');
}
}

0 comments on commit 4f57d69

Please sign in to comment.