8000 [Routing] Moving include instruction to separate method · matthieuauger/symfony@60ce31d · GitHub
[go: up one dir, main page]

Skip to content

Commit 60ce31d

Browse files
Stratefabpot
authored andcommitted
[Routing] Moving include instruction to separate method
1 parent 69770bd commit 60ce31d

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

src/Symfony/Component/Routing/Loader/PhpFileLoader.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,10 @@ class PhpFileLoader extends FileLoader
3838
*/
3939
public function load($file, $type = null)
4040
{
41-
// the loader variable is exposed to the included file below
42-
$loader = $this;
43-
4441
$path = $this->locator->locate($file);
4542
$this->setCurrentDir(dirname($path));
4643

47-
$collection = include $path;
44+
$collection = self::includeFile($path, $this);
4845
$collection->addResource(new FileResource($path));
4946

5047
return $collection;
@@ -59,4 +56,17 @@ public function supports($resource, $type = null)
5956
{
6057
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
6158
}
59+
60+
/**
61+
* Safe include. Used for scope isolation.
62+
*
63+
* @param string $file File to include
64+
* @param PhpFileLoader $loader the loader variable is exposed to the included file below
65+
*
66+
* @return RouteCollection
67+
*/
68+
private static function includeFile($file, PhpFileLoader $loader)
69+
{
70+
return include $file;
71+
}
6272
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/** @var $loader \Symfony\Component\Routing\Loader\PhpFileLoader */
4+
/** @var \Symfony\Component\Routing\RouteCollection $collection */
5+
$collection = $loader->import('validpattern.php');
6+
$collection->addDefaults(array(
7+
'foo' => 123,
8+
));
9+
$collection->addRequirements(array(
10+
'foo' => '\d+',
11+
));
12+
$collection->addOptions(array(
13+
'foo' => 'bar',
14+
));
15+
$collection->setCondition('context.getMethod() == "POST"');
16+
$collection->addPrefix('/prefix');
17+
18+
return $collection;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$path = '/1/2/3';
4+
5+
return new \Symfony\Component\Routing\RouteCollection();

src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,38 @@ public function testLoadWithRoute()
4545
$this->assertEquals(array('https'), $route->getSchemes());
4646
}
4747
}
48+
49+
public function testLoadWithImport()
50+
{
51+
$loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
52+
$routeCollection = $loader->load('validresource.php');
53+
$routes = $routeCollection->all();
54+
55+
$this->assertCount(2, $routes, 'Two routes are loaded');
56+
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
57+
58+
foreach ($routes as $route) {
59+
$this->assertSame('/prefix/blog/{slug}', $route->getPath());
60+
$this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
61+
$this->assertSame('{locale}.example.com', $route->getHost());
62+
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
63+
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
64+
$this->assertEquals(array('https'), $route->getSchemes());
65+
}
66+
}
67+
68+
public function testThatDefiningVariableInConfigFileHasNoSideEffects()
69+
{
70+
$locator = new FileLocator(array(__DIR__.'/../Fixtures'));
71+
$loader = new PhpFileLoader($locator);
72+
$routeCollection = $loader->load('with_define_path_variable.php');
73+
$resources = $routeCollection->getResources();
74+
$this->assertCount(1, $resources);
75+
$this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
76+
$fileResource = reset($resources);
77+
$this->assertSame(
78+
realpath($locator->locate('with_define_path_variable.php')),
79+
(string) $fileResource
80+
);
81+
}
4882
}

0 commit comments

Comments
 (0)
0