Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4877d60

Browse files
committed
feature #28893 [TwigBundle] Fix usage of TwigBundle without FrameworkBundle (tgalopin)
This PR was squashed before being merged into the 3.4 branch (closes #28893). Discussion ---------- [TwigBundle] Fix usage of TwigBundle without FrameworkBundle | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - According to the composer.json (https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/TwigBundle/composer.json), the FrameworkBundle shouldn't be required to use the bundle (which can be useful in tests of other bundles for instance). However, it is not the case, mainly due to issues with direct references to unavailable services. I target 3.4 because 812fbb4 is the main reason for the issue. We may have added additional problems in 4.0 and 4.1. Commits ------- 246a905 [TwigBundle] Fix usage of TwigBundle without FrameworkBundle
2 parents fdc9e09 + 246a905 commit 4877d60

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function process(ContainerBuilder $container)
6464

6565
if ($container->has('fragment.handler')) {
6666
$container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
67+
$container->getDefinition('twig.runtime.httpkernel')->addTag('twig.runtime');
6768

6869
// inject Twig in the hinclude service if Twig is the only registered templating engine
6970
if ((!$container->hasParameter('templating.engines') || array('twig') == $container->getParameter('templating.engines')) && $container->hasDefinition('fragment.renderer.hinclude')) {
@@ -74,6 +75,10 @@ public function process(ContainerBuilder $container)
7475
}
7576
}
7677

78+
if (!$container->has('http_kernel')) {
79+
$container->removeDefinition('twig.controller.preview_error');
80+
}
81+
7782
if ($container->has('request_stack')) {
7883
$container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
7984
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ public function load(array $configs, ContainerBuilder $container)
3636
$loader->load('twig.xml');
3737

3838
$container->getDefinition('twig.profile')->setPrivate(true);
39-
$container->getDefinition('twig.runtime.httpkernel')->setPrivate(true);
4039
$container->getDefinition('twig.translation.extractor')->setPrivate(true);
4140
$container->getDefinition('workflow.twig_extension')->setPrivate(true);
4241
$container->getDefinition('twig.exception_listener')->setPrivate(true);
4342

43+
if ($container->has('fragment.handler')) {
44+
$container->getDefinition('twig.runtime.httpkernel')->setPrivate(true);
45+
}
46+
4447
if (class_exists('Symfony\Component\Form\Form')) {
4548
$loader->load('form.xml');
4649
$container->getDefinition('twig.form.renderer')->setPrivate(true);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105

106106
<service id="twig.runtime.httpkernel" class="Symfony\Bridge\Twig\Extension\HttpKernelRuntime">
107107
<argument type="service" id="fragment.handler" />
108-
<tag name="twig.runtime" />
109108
</service>
110109

111110
<service id="twig.extension.httpfoundation" class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension">
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle\Tests\Functional;
13+
14+
use Symfony\Bundle\TwigBundle\Tests\TestCase;
15+
use Symfony\Bundle\TwigBundle\TwigBundle;
16+
use Symfony\Component\Config\Loader\LoaderInterface;
17+
use Symfony\Component\HttpKernel\Kernel;
18+
19+
class EmptyAppTest extends TestCase
20+
{
21+
public function testBootEmptyApp()
22+
{
23+
$kernel = new EmptyAppKernel('test', true);
24+
$kernel->boot();
25+
26+
$this->assertTrue($kernel->getContainer()->hasParameter('twig.default_path'));
27+
$this->assertNotEmpty($kernel->getContainer()->getParameter('twig.default_path'));
28+
}
29+
}
30+
31+
class EmptyAppKernel extends Kernel
32+
{
33+
public function registerBundles()
34+
{
35+
return array(new TwigBundle());
36+
}
37+
38+
public function registerContainerConfiguration(LoaderInterface $loader)
39+
{
40+
}
41+
42+
public function getCacheDir()
43+
{
44+
return sys_get_temp_dir().'/'.Kernel::VERSION.'/EmptyAppKernel/cache/'.$this->environment;
45+
}
46+
47+
public function getLogDir()
48+
{
49+
return sys_get_temp_dir().'/'.Kernel::VERSION.'/EmptyAppKernel/logs';
50+
}
51+
}

0 commit comments

Comments
 (0)