Skip to content

Commit

Permalink
inject project root path into twig filesystem loader
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Dec 5, 2016
1 parent ea1d4d5 commit ee1e710
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
6 changes: 5 additions & 1 deletion DependencyInjection/Compiler/ExtensionPass.php
Expand Up @@ -64,9 +64,13 @@ public function process(ContainerBuilder $container)
$container->getDefinition('twig.extension.debug')->addTag('twig.extension');
}

$composerRootDir = $this->getComposerRootDir($container->getParameter('kernel.root_dir'));
$loader = $container->getDefinition('twig.loader.filesystem');
$loader->replaceArgument(2, $composerRootDir);

if (!$container->has('templating')) {
$loader = $container->getDefinition('twig.loader.native_filesystem');
$loader->replaceArgument(1, $this->getComposerRootDir($container->getParameter('kernel.root_dir')));
$loader->replaceArgument(1, $composerRootDir);
$loader->addTag('twig.loader');
$loader->setMethodCalls($container->getDefinition('twig.loader.filesystem')->getMethodCalls());

Expand Down
9 changes: 5 additions & 4 deletions Loader/FilesystemLoader.php
Expand Up @@ -29,12 +29,13 @@ class FilesystemLoader extends \Twig_Loader_Filesystem
/**
* Constructor.
*
* @param FileLocatorInterface $locator A FileLocatorInterface instance
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
* @param FileLocatorInterface $locator A FileLocatorInterface instance
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
* @param string|null $rootPath The root path common to all relative paths (null for getcwd())
*/
public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser)
public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser, $rootPath = null)
{
parent::__construct(array());
parent::__construct(array(), $rootPath);

$this->locator = $locator;
$this->parser = $parser;
Expand Down
5 changes: 3 additions & 2 deletions Resources/config/twig.xml
Expand Up @@ -46,12 +46,13 @@

<service id="twig.loader.native_filesystem" class="Twig_Loader_Filesystem" public="false">
<argument type="collection" /> <!-- paths -->
<argument></argument> <!-- project's root dir -->
<argument /> <!-- project's root dir -->
</service>

<service id="twig.loader.filesystem" class="Symfony\Bundle\TwigBundle\Loader\FilesystemLoader" public="false">
<argument type="service" id="templating.locator" />
<argument type="service" id="templating.name_parser" />
<argument /> <!-- project's root dir -->
<tag name="twig.loader"/>
</service>

Expand Down Expand Up @@ -87,7 +88,7 @@

<service id="twig.extension.code" class="Symfony\Bridge\Twig\Extension\CodeExtension" public="false">
<tag name="twig.extension" />
<argument type="service" id="debug.file_link_formatter"></argument>
<argument type="service" id="debug.file_link_formatter" />
<argument>%kernel.root_dir%</argument>
<argument>%kernel.charset%</argument>
</service>
Expand Down

0 comments on commit ee1e710

Please sign in to comment.