Skip to content

Commit

Permalink
[Routing] Moving include instruction to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
Strate authored and fabpot committed Oct 7, 2014
1 parent 69770bd commit 60ce31d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Symfony/Component/Routing/Loader/PhpFileLoader.php
Expand Up @@ -38,13 +38,10 @@ class PhpFileLoader extends FileLoader
*/
public function load($file, $type = null)
{
// the loader variable is exposed to the included file below
$loader = $this;

$path = $this->locator->locate($file);
$this->setCurrentDir(dirname($path));

$collection = include $path;
$collection = self::includeFile($path, $this);
$collection->addResource(new FileResource($path));

return $collection;
Expand All @@ -59,4 +56,17 @@ public function supports($resource, $type = null)
{
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
}

/**
* Safe include. Used for scope isolation.
*
* @param string $file File to include
* @param PhpFileLoader $loader the loader variable is exposed to the included file below
*
* @return RouteCollection
*/
private static function includeFile($file, PhpFileLoader $loader)
{
return include $file;
}
}
18 changes: 18 additions & 0 deletions src/Symfony/Component/Routing/Tests/Fixtures/validresource.php
@@ -0,0 +1,18 @@
<?php

/** @var $loader \Symfony\Component\Routing\Loader\PhpFileLoader */
/** @var \Symfony\Component\Routing\RouteCollection $collection */
$collection = $loader->import('validpattern.php');
$collection->addDefaults(array(
'foo' => 123,
));
$collection->addRequirements(array(
'foo' => '\d+',
));
$collection->addOptions(array(
'foo' => 'bar',
));
$collection->setCondition('context.getMethod() == "POST"');
$collection->addPrefix('/prefix');

return $collection;
@@ -0,0 +1,5 @@
<?php

$path = '/1/2/3';

return new \Symfony\Component\Routing\RouteCollection();
34 changes: 34 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
Expand Up @@ -45,4 +45,38 @@ public function testLoadWithRoute()
$this->assertEquals(array('https'), $route->getSchemes());
}
}

public function testLoadWithImport()
{
$loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validresource.php');
$routes = $routeCollection->all();

$this->assertCount(2, $routes, 'Two routes are loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);

foreach ($routes as $route) {
$this->assertSame('/prefix/blog/{slug}', $route->getPath());
$this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
$this->assertSame('{locale}.example.com', $route->getHost());
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
$this->assertEquals(array('https'), $route->getSchemes());
}
}

public function testThatDefiningVariableInConfigFileHasNoSideEffects()
{
$locator = new FileLocator(array(__DIR__.'/../Fixtures'));
$loader = new PhpFileLoader($locator);
$routeCollection = $loader->load('with_define_path_variable.php');
$resources = $routeCollection->getResources();
$this->assertCount(1, $resources);
$this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
$fileResource = reset($resources);
$this->assertSame(
realpath($locator->locate('with_define_path_variable.php')),
(string) $fileResource
);
}
}

0 comments on commit 60ce31d

Please sign in to comment.