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

Skip to content

Commit 7192b2f

Browse files
committed
feature #13354 Twig decoupling from Templating (fabpot)
This PR was merged into the 2.7 branch. Discussion ---------- Twig decoupling from Templating | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #11748, #11876 | License | MIT | Doc PR | n/a The goal of this PR is to make Twig independent of the Symfony Templating system. With this PR, you can now use the Twig bundle without activating `framework.templating`. Also, even when registering the templating system in FrameworkBundle, the `php` engine is only registered when specified explicitly in `engines`. First, the global variables has been decoupled from FrameworkBundle. Then, the Twig bundle now tries to only rely on native Twig extensions and loaders and use the Templating sub-system only if `framework.templating` is enabled. Commits ------- 18d4c41 [TwigBundle] added some tests 0d537c4 decoupled Twig from the Templating system be5a208 decoupled global variables system in Twig from the Templating one
2 parents 861804b + 18d4c41 commit 7192b2f

File tree

13 files changed

+360
-71
lines changed

13 files changed

+360
-71
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ private function registerRequestConfiguration(array $config, ContainerBuilder $c
458458
private function registerTemplatingConfiguration(array $config, $ide, ContainerBuilder $container, XmlFileLoader $loader)
459459
{
460460
$loader->load('templating.xml');
461-
$loader->load('templating_php.xml');
462461

463462
$links = array(
464463
'textmate' => 'txmt://open?url=file://%%f&line=%%l',
@@ -468,39 +467,36 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
468467
);
469468

470469
$container->setParameter('templating.helper.code.file_link_format', isset($links[$ide]) ? $links[$ide] : $ide);
471-
$container->setParameter('templating.helper.form.resources', $config['form']['resources']);
472470
$container->setParameter('fragment.renderer.hinclude.global_template', $config['hinclude_default_template']);
473471

474-
if ($container->getParameter('kernel.debug')) {
475-
$loader->load('templating_debug.xml');
476-
477-
$logger = new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE);
478-
479-
$container->getDefinition('templating.loader.cache')
480-
->addTag('monolog.logger', array('channel' => 'templating'))
481-
->addMethodCall('setLogger', array($logger));
482-
$container->getDefinition('templating.loader.chain')
483-
->addTag('monolog.logger', array('channel' => 'templating'))
484-
->addMethodCall('setLogger', array($logger));
485-
486-
$container->setDefinition('templating.engine.php', $container->findDefinition('debug.templating.engine.php'));
487-
$container->setAlias('debug.templating.engine.php', 'templating.engine.php');
488-
}
472+
$loader->load('old_assets.xml');
489473

490474
// create package definitions and add them to the assets helper
491-
$defaultPackage = $this->createPackageDefinition($container, $config['assets_base_urls']['http'], $config['assets_base_urls']['ssl'], $config['assets_version'], $config['assets_version_format']);
475+
$defaultPackage = $this->createTemplatingPackageDefinition($container, $config['assets_base_urls']['http'], $config['assets_base_urls']['ssl'], $config['assets_version'], $config['assets_version_format']);
492476
$container->setDefinition('templating.asset.default_package', $defaultPackage);
493477
$namedPackages = array();
494478
foreach ($config['packages'] as $name => $package) {
495-
$namedPackage = $this->createPackageDefinition($container, $package['base_urls']['http'], $package['base_urls']['ssl'], $package['version'], $package['version_format'], $name);
479+
$namedPackage = $this->createTemplatingPackageDefinition($container, $package['base_urls']['http'], $package['base_urls']['ssl'], $package['version'], $package['version_format'], $name);
496480
$container->setDefinition('templating.asset.package.'.$name, $namedPackage);
497481
$namedPackages[$name] = new Reference('templating.asset.package.'.$name);
498482
}
483+
499484
$container->getDefinition('templating.helper.assets')->setArguments(array(
500485
new Reference('templating.asset.default_package'),
501486
$namedPackages,
502487
));
503488

489+
if ($container->getParameter('kernel.debug')) {
490+
$logger = new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE);
491+
492+
$container->getDefinition('templating.loader.cache')
493+
->addTag('monolog.logger', array('channel' => 'templating'))
494+
->addMethodCall('setLogger', array($logger));
495+
$container->getDefinition('templating.loader.chain')
496+
->addTag('monolog.logger', array('channel' => 'templating'))
497+
->addMethodCall('setLogger', array($logger));
498+
}
499+
504500
if (!empty($config['loaders'])) {
505501
$loaders = array_map(function ($loader) { return new Reference($loader); }, $config['loaders']);
506502

@@ -530,14 +526,6 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
530526
$container->findDefinition('templating.locator')->getClass(),
531527
));
532528

533-
if (in_array('php', $config['engines'], true)) {
534-
$this->addClassesToCompile(array(
535-
'Symfony\\Component\\Templating\\Storage\\FileStorage',
536-
'Symfony\\Bundle\\FrameworkBundle\\Templating\\PhpEngine',
537-
'Symfony\\Bundle\\FrameworkBundle\\Templating\\Loader\\FilesystemLoader',
538-
));
539-
}
540-
541529
$container->setParameter('templating.engines', $config['engines']);
542530
$engines = array_map(function ($engine) { return new Reference('templating.engine.'.$engine); }, $config['engines']);
543531

@@ -550,12 +538,32 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
550538
}
551539
$container->setAlias('templating', 'templating.engine.delegating');
552540
}
541+
542+
// configure the PHP engine if needed
543+
if (in_array('php', $config['engines'], true)) {
544+
$loader->load('templating_php.xml');
545+
546+
$container->setParameter('templating.helper.form.resources', $config['form']['resources']);
547+
548+
if ($container->getParameter('kernel.debug')) {
549+
$loader->load('templating_debug.xml');
550+
551+
$container->setDefinition('templating.engine.php', $container->findDefinition('debug.templating.engine.php'));
552+
$container->setAlias('debug.templating.engine.php', 'templating.engine.php');
553+
}
554+
555+
$this->addClassesToCompile(array(
556+
'Symfony\\Component\\Templating\\Storage\\FileStorage',
557+
'Symfony\\Bundle\\FrameworkBundle\\Templating\\PhpEngine',
558+
'Symfony\\Bundle\\FrameworkBundle\\Templating\\Loader\\FilesystemLoader',
559+
));
560+
}
553561
}
554562

555563
/**
556564
* Returns a definition for an asset package.
557565
*/
558-
private function createPackageDefinition(ContainerBuilder $container, array $httpUrls, array $sslUrls, $version, $format, $name = null)
566+
private function createTemplatingPackageDefinition(ContainerBuilder $container, array $httpUrls, array $sslUrls, $version, $format, $name = null)
559567
{
560568
if (!$httpUrls) {
561569
$package = new DefinitionDecorator('templating.asset.path_package');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="templating.asset.path_package.class">Symfony\Bundle\FrameworkBundle\Templating\Asset\PathPackage</parameter>
9+
<parameter key="templating.asset.url_package.class">Symfony\Component\Templating\Asset\UrlPackage</parameter>
10+
<parameter key="templating.asset.package_factory.class">Symfony\Bundle\FrameworkBundle\Templating\Asset\PackageFactory</parameter>
11+
</parameters>
12+
13+
<services>
14+
<service id="templating.asset.path_package" class="%templating.asset.path_package.class%" abstract="true">
15+
<argument type="service" id="request" />
16+
<argument /> <!-- version -->
17+
<argument /> <!-- version format -->
18+
</service>
19+
20+
<service id="templating.asset.url_package" class="%templating.asset.url_package.class%" abstract="true">
21+
<argument /> <!-- base urls -->
22+
<argument /> <!-- version -->
23+
<argument /> <!-- version format -->
24+
</service>
25+
26+
<service id="templating.asset.request_aware_package" class="Symfony\Component\Templating\Asset\PackageInterface" abstract="true">
27+
<factory service="templating.asset.package_factory" method="getPackage" />
28+
<argument type="service" id="request" strict="false" />
29+
<argument /> <!-- HTTP id -->
30+
<argument /> <!-- SSL id -->
31+
</service>
32+
33+
<service id="templating.asset.package_factory" class="%templating.asset.package_factory.class%">
34+
<argument type="service" id="service_container" />
35+
</service>
36+
</services>
37+
</container>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<parameter key="templating.loader.cache.class">Symfony\Component\Templating\Loader\CacheLoader</parameter>
1515
<parameter key="templating.loader.chain.class">Symfony\Component\Templating\Loader\ChainLoader</parameter>
1616
<parameter key="templating.finder.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder</parameter>
17+
<parameter key="templating.helper.assets.class">Symfony\Component\Templating\Helper\CoreAssetsHelper</parameter>
1718
</parameters>
1819

1920
<services>
@@ -58,5 +59,15 @@
5859
</service>
5960

6061
<service id="templating.loader" alias="templating.loader.filesystem" />
62+
63+
<!--
64+
This should be in templating_php.xml but unfortunately, Twig depends on this helper.
65+
As the Twig extension depending on this service is deprecated, this will be removed in 3.0.
66+
-->
67+
<service id="templating.helper.assets" class="%templating.helper.assets.class%">
68+
<tag name="templating.helper" alias="assets" />
69+
<argument /> <!-- default package -->
70+
<argument type="collection" /> <!-- named packages -->
71+
</service>
6172
</services>
6273
</container>

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<parameters>
88
<parameter key="templating.engine.php.class">Symfony\Bundle\FrameworkBundle\Templating\PhpEngine</parameter>
99
<parameter key="templating.helper.slots.class">Symfony\Component\Templating\Helper\SlotsHelper</parameter>
10-
<parameter key="templating.helper.assets.class">Symfony\Component\Templating\Helper\CoreAssetsHelper</parameter>
1110
<parameter key="templating.helper.actions.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper</parameter>
1211
<parameter key="templating.helper.router.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\RouterHelper</parameter>
1312
<parameter key="templating.helper.request.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\RequestHelper</parameter>
@@ -19,9 +18,6 @@
1918
<parameter key="templating.form.engine.class">Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine</parameter>
2019
<parameter key="templating.form.renderer.class">Symfony\Component\Form\FormRenderer</parameter>
2120
<parameter key="templating.globals.class">Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables</parameter>
22-
<parameter key="templating.asset.path_package.class">Symfony\Bundle\FrameworkBundle\Templating\Asset\PathPackage</parameter>
23-
<parameter key="templating.asset.url_package.class">Symfony\Component\Templating\Asset\UrlPackage</parameter>
24-
<parameter key="templating.asset.package_factory.class">Symfony\Bundle\FrameworkBundle\Templating\Asset\PackageFactory</parameter>
2521
</parameters>
2622

2723
<services>
@@ -37,35 +33,6 @@
3733
<tag name="templating.helper" alias="slots" />
3834
</service>
3935

40-
<service id="templating.helper.assets" class="%templating.helper.assets.class%">
41-
<tag name="templating.helper" alias="assets" />
42-
<argument /> <!-- default package -->
43-
<argument type="collection" /> <!-- named packages -->
44-
</service>
45-
46-
<service id="templating.asset.path_package" class="%templating.asset.path_package.class%" abstract="true">
47-
<argument type="service" id="request" />
48-
<argument /> <!-- version -->
49-
<argument /> <!-- version format -->
50-
</service>
51-
52-
<service id="templating.asset.url_package" class="%templating.asset.url_package.class%" abstract="true">
53-
<argument /> <!-- base urls -->
54-
<argument /> <!-- version -->
55-
<argument /> <!-- version format -->
56-
</service>
57-
58-
<service id="templating.asset.request_aware_package" class="Symfony\Component\Templating\Asset\PackageInterface" abstract="true">
59-
<factory service="templating.asset.package_factory" method="getPackage" />
60-
<argument type="service" id="request" strict="false" />
61-
<argument /> <!-- HTTP id -->
62-
<argument /> <!-- SSL id -->
63-
</service>
64-
65-
<service id="templating.asset.package_factory" class="%templating.asset.package_factory.class%">
66-
<argument type="service" id="service_container" />
67-
</service>
68-
6936
<service id="templating.helper.request" class="%templating.helper.request.class%">
7037
<tag name="templating.helper" alias="request" />
7138
<argument type="service" id="request_stack" />

src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Security\Core\SecurityContext;
1919

2020
/**
21-
* GlobalVariables is the entry point for Symfony global variables in Twig templates.
21+
* GlobalVariables is the entry point for Symfony global variables in PHP templates.
2222
*
2323
* @author Fabien Potencier <[email protected]>
2424
*/
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\RequestStack;
16+
use Symfony\Component\HttpFoundation\Session\Session;
17+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19+
use Symfony\Component\Security\Core\SecurityContextInterface;
20+
21+
/**
22+
* Exposes some Symfony parameters and services as an "app" global variable.
23+
*
24+
* @author Fabien Potencier <[email protected]>
25+
*/
26+
class AppVariable
27+
{
28+
private $security;
29+
private $tokenStorage;
30+
private $requestStack;
31+
private $environment;
32+
private $debug;
33+
34+
/**
35+
* @deprecated since version 2.7, to be removed in 3.0.
36+
*/
37+
public function setSecurity(SecurityContextInterface $security)
38+
{
39+
$this->security = $security;
40+
}
41+
42+
public function setTokenStorage(TokenStorageInterface $tokenStorage)
43+
{
44+
$this->tokenStorage = $tokenStorage;
45+
}
46+
47+
public function setRequestStack(RequestStack $requestStack)
48+
{
49+
$this->requestStack = $requestStack;
50+
}
51+
52+
public function setEnvironment($environment)
53+
{
54+
$this->environment = $environment;
55+
}
56+
57+
public function setDebug($debug)
58+
{
59+
$this->debug = (bool) $debug;
60+
}
61+
62+
/**
63+
* Returns the security context service.
64+
*
65+
* @deprecated since version 2.6, to be removed in 3.0.
66+
*
67+
* @return SecurityContext|null The security context
68+
*/
69+
public function getSecurity()
70+
{
71+
trigger_error('The "app.security" variable is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
72+
73+
if (null === $this->security) {
74+
throw new \RuntimeException('The "app.security" variable is not available.');
75+
}
76+
77+
return $this->security;
78+
}
79+
80+
/**
81+
* Returns the current user.
82+
*
83+
* @return mixed
84+
*
85+
* @see TokenInterface::getUser()
86+
*/
87+
public function getUser()
88+
{
89+
if (null === $this->tokenStorage) {
90+
throw new \RuntimeException('The "app.user" variable is not available.');
91+
}
92+
93+
if (!$token = $this->tokenStorage->getToken()) {
94+
return;
95+
}
96+
97+
$user = $token->getUser();
98+
if (is_object($user)) {
99+
return $user;
100+
}
101+
}
102+
103+
/**
104+
* Returns the current request.
105+
*
106+
* @return Request|null The HTTP request object
107+
*/
108+
public function getRequest()
109+
{
110+
if (null === $this->requestStack) {
111+
throw new \RuntimeException('The "app.request" variable is not available.');
112+
}
113+
114+
return $this->requestStack->getCurrentRequest();
115+
}
116+
117+
/**
118+
* Returns the current session.
119+
*
120+
* @return Session|null The session
121+
*/
122+
public function getSession()
123+
{
124+
if (null === $this->requestStack) {
125+
throw new \RuntimeException('The "app.session" variable is not available.');
126+
}
127+
128+
if ($request = $this->getRequest()) {
129+
return $request->getSession();
130+
}
131+
}
132+
133+
/**
134+
* Returns the current app environment.
135+
*
136+
* @return string The current environment string (e.g 'dev')
137+
*/
138+
public function getEnvironment()
139+
{
140+
if (null === $this->environment) {
141+
throw new \RuntimeException('The "app.environment" variable is not available.');
142+
}
143+
144+
return $this->environment;
145+
}
146+
147+
/**
148+
* Returns the current app debug mode.
149+
*
150+
* @return bool The current debug mode
151+
*/
152+
public function getDebug()
153+
{
154+
if (null === $this->debug) {
155+
throw new \RuntimeException('The "app.debug" variable is not available.');
156+
}
157+
158+
return $this->debug;
159+
}
160+
}

0 commit comments

Comments
 (0)