Skip to content

Commit 143b5ff

Browse files
committed
bug #22570 Fixes twig bundle project root dir discovery (Pierre Rineau)
This PR was squashed before being merged into the 3.3-dev branch (closes #22570). Discussion ---------- Fixes twig bundle project root dir discovery | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22149 | License | MIT | Doc PR | none <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. - Please fill in this template according to the PR you're about to submit. - Replace this comment by a description of what your PR is solving. --> TwigBundle ExtensionPass uses a directory lookup algorithm to find the composer.json file. When working under open_basedir restrictions, if the project root folder is not allowed PHP runtime to read, the composer.json will never be found, throwing PHP notices along the way and ending up using the filesystem root instead. Since that 3.3 introduced the kernel getProjectRoot() method and kernel.project_dir variable, the TwigBundle can now leverage it, in order to get more consistent, and allow site builders to override the getProjectRoot() kernel method, hence bypass the open_basedir restrictions in reliable and safe way. Commits ------- 3cb2e5b Fixes twig bundle project root dir discovery
2 parents 9d9f628 + 3cb2e5b commit 143b5ff

File tree

5 files changed

+4
-20
lines changed

5 files changed

+4
-20
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php

-17
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,13 @@ public function process(ContainerBuilder $container)
8080
$container->getDefinition('twig.extension.debug')->addTag('twig.extension');
8181
}
8282

83-
$composerRootDir = $this->getComposerRootDir($container->getParameter('kernel.root_dir'));
8483
$twigLoader = $container->getDefinition('twig.loader.native_filesystem');
8584
if ($container->has('templating')) {
8685
$loader = $container->getDefinition('twig.loader.filesystem');
8786
$loader->setMethodCalls(array_merge($twigLoader->getMethodCalls(), $loader->getMethodCalls()));
88-
$loader->replaceArgument(2, $composerRootDir);
8987

9088
$twigLoader->clearTag('twig.loader');
9189
} else {
92-
$twigLoader->replaceArgument(1, $composerRootDir);
9390
$container->setAlias('twig.loader.filesystem', new Alias('twig.loader.native_filesystem', false));
9491
}
9592

@@ -109,18 +106,4 @@ public function process(ContainerBuilder $container)
109106
$container->getDefinition('twig.extension.expression')->addTag('twig.extension');
110107
}
111108
}
112-
113-
private function getComposerRootDir($rootDir)
114-
{
115-
$dir = $rootDir;
116-
while (!file_exists($dir.'/composer.json')) {
117-
if ($dir === dirname($dir)) {
118-
return $rootDir;
119-
}
120-
121-
$dir = dirname($dir);
122-
}
123-
124-
return $dir;
125-
}
126109
}

src/Symfony/Bundle/TwigBundle/Resources/config/templating.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<service id="twig.loader.filesystem" class="Symfony\Bundle\TwigBundle\Loader\FilesystemLoader" public="false">
88
<argument type="service" id="templating.locator" />
99
<argument type="service" id="templating.name_parser" />
10-
<argument /> <!-- project's root dir -->
10+
<argument>%kernel.project_dir%</argument>
1111
<tag name="twig.loader"/>
1212
</service>
1313

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
<service id="twig.loader.native_filesystem" class="Twig_Loader_Filesystem" public="false">
5050
<argument type="collection" /> <!-- paths -->
51-
<argument /> <!-- project's root dir -->
51+
<argument>%kernel.project_dir%</argument>
5252
<tag name="twig.loader"/>
5353
</service>
5454

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ private function createContainer()
256256
$container = new ContainerBuilder(new ParameterBag(array(
257257
'kernel.cache_dir' => __DIR__,
258258
'kernel.root_dir' => __DIR__.'/Fixtures',
259+
'kernel.project_dir' => __DIR__,
259260
'kernel.charset' => 'UTF-8',
260261
'kernel.debug' => false,
261262
'kernel.bundles' => array(

src/Symfony/Bundle/TwigBundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/config": "~3.2",
2121
"symfony/twig-bridge": "^3.2.1",
2222
"symfony/http-foundation": "~2.8|~3.0",
23-
"symfony/http-kernel": "~2.8.16|~3.1.9|^3.2.2",
23+
"symfony/http-kernel": "^3.3",
2424
"twig/twig": "^1.32|^2.2"
2525
},
2626
"require-dev": {

0 commit comments

Comments
 (0)