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

Skip to content

Twig decoupling from Templating #13354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ private function registerRequestConfiguration(array $config, ContainerBuilder $c
private function registerTemplatingConfiguration(array $config, $ide, ContainerBuilder $container, XmlFileLoader $loader)
{
$loader->load('templating.xml');
$loader->load('templating_php.xml');

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

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

if ($container->getParameter('kernel.debug')) {
$loader->load('templating_debug.xml');

$logger = new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE);

$container->getDefinition('templating.loader.cache')
->addTag('monolog.logger', array('channel' => 'templating'))
->addMethodCall('setLogger', array($logger));
$container->getDefinition('templating.loader.chain')
->addTag('monolog.logger', array('channel' => 'templating'))
->addMethodCall('setLogger', array($logger));

$container->setDefinition('templating.engine.php', $container->findDefinition('debug.templating.engine.php'));
$container->setAlias('debug.templating.engine.php', 'templating.engine.php');
}
$loader->load('old_assets.xml');

// create package definitions and add them to the assets helper
$defaultPackage = $this->createPackageDefinition($container, $config['assets_base_urls']['http'], $config['assets_base_urls']['ssl'], $config['assets_version'], $config['assets_version_format']);
$defaultPackage = $this->createTemplatingPackageDefinition($container, $config['assets_base_urls']['http'], $config['assets_base_urls']['ssl'], $config['assets_version'], $config['assets_version_format']);
$container->setDefinition('templating.asset.default_package', $defaultPackage);
$namedPackages = array();
foreach ($config['packages'] as $name => $package) {
$namedPackage = $this->createPackageDefinition($container, $package['base_urls']['http'], $package['base_urls']['ssl'], $package['version'], $package['version_format'], $name);
$namedPackage = $this->createTemplatingPackageDefinition($container, $package['base_urls']['http'], $package['base_urls']['ssl'], $package['version'], $package['version_format'], $name);
$container->setDefinition('templating.asset.package.'.$name, $namedPackage);
$namedPackages[$name] = new Reference('templating.asset.package.'.$name);
}

$container->getDefinition('templating.helper.assets')->setArguments(array(
new Reference('templating.asset.default_package'),
$namedPackages,
));

if ($container->getParameter('kernel.debug')) {
$logger = new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE);

$container->getDefinition('templating.loader.cache')
->addTag('monolog.logger', array('channel' => 'templating'))
->addMethodCall('setLogger', array($logger));
$container->getDefinition('templating.loader.chain')
->addTag('monolog.logger', array('channel' => 'templating'))
->addMethodCall('setLogger', array($logger));
}

if (!empty($config['loaders'])) {
$loaders = array_map(function ($loader) { return new Reference($loader); }, $config['loaders']);

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

if (in_array('php', $config['engines'], true)) {
$this->addClassesToCompile(array(
'Symfony\\Component\\Templating\\Storage\\FileStorage',
'Symfony\\Bundle\\FrameworkBundle\\Templating\\PhpEngine',
'Symfony\\Bundle\\FrameworkBundle\\Templating\\Loader\\FilesystemLoader',
));
}

$container->setParameter('templating.engines', $config['engines']);
$engines = array_map(function ($engine) { return new Reference('templating.engine.'.$engine); }, $config['engines']);

Expand All @@ -550,12 +538,32 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
}
$container->setAlias('templating', 'templating.engine.delegating');
}

// configure the PHP engine if needed
if (in_array('php', $config['engines'], true)) {
$loader->load('templating_php.xml');

$container->setParameter('templating.helper.form.resources', $config['form']['resources']);

if ($container->getParameter('kernel.debug')) {
$loader->load('templating_debug.xml');

$container->setDefinition('templating.engine.php', $container->findDefinition('debug.templating.engine.php'));
$container->setAlias('debug.templating.engine.php', 'templating.engine.php');
}

$this->addClassesToCompile(array(
'Symfony\\Component\\Templating\\Storage\\FileStorage',
'Symfony\\Bundle\\FrameworkBundle\\Templating\\PhpEngine',
'Symfony\\Bundle\\FrameworkBundle\\Templating\\Loader\\FilesystemLoader',
));
}
}

/**
* Returns a definition for an asset package.
*/
private function createPackageDefinition(ContainerBuilder $container, array $httpUrls, array $sslUrls, $version, $format, $name = null)
private function createTemplatingPackageDefinition(ContainerBuilder $container, array $httpUrls, array $sslUrls, $version, $format, $name = null)
{
if (!$httpUrls) {
$package = new DefinitionDecorator('templating.asset.path_package');
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/old_assets.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="templating.asset.path_package.class">Symfony\Bundle\FrameworkBundle\Templating\Asset\PathPackage</parameter>
<parameter key="templating.asset.url_package.class">Symfony\Component\Templating\Asset\UrlPackage</parameter>
<parameter key="templating.asset.package_factory.class">Symfony\Bundle\FrameworkBundle\Templating\Asset\PackageFactory</parameter>
</parameters>

<services>
<service id="templating.asset.path_package" class="%templating.asset.path_package.class%" abstract="true">
<argument type="service" id="request" />
<argument /> <!-- version -->
<argument /> <!-- version format -->
</service>

<service id="templating.asset.url_package" class="%templating.asset.url_package.class%" abstract="true">
<argument /> <!-- base urls -->
<argument /> <!-- version -->
<argument /> <!-- version format -->
</service>

<service id="templating.asset.request_aware_package" class="Symfony\Component\Templating\Asset\PackageInterface" abstract="true">
<factory service="templating.asset.package_factory" method="getPackage" />
<argument type="service" id="request" strict="false" />
<argument /> <!-- HTTP id -->
<argument /> <!-- SSL id -->
</service>

<service id="templating.asset.package_factory" class="%templating.asset.package_factory.class%">
<argument type="service" id="service_container" />
</service>
</services>
</container>
11 changes: 11 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<parameter key="templating.loader.cache.class">Symfony\Component\Templating\Loader\CacheLoader</parameter>
<parameter key="templating.loader.chain.class">Symfony\Component\Templating\Loader\ChainLoader</parameter>
<parameter key="templating.finder.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder</parameter>
<parameter key="templating.helper.assets.class">Symfony\Component\Templating\Helper\CoreAssetsHelper</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -58,5 +59,15 @@
</service>

<service id="templating.loader" alias="templating.loader.filesystem" />

<!--
This should be in templating_php.xml but unfortunately, Twig depends on this helper.
As the Twig extension depending on this service is deprecated, this will be removed in 3.0.
-->
<service id="templating.helper.assets" class="%templating.helper.assets.class%">
<tag name="templating.helper" alias="assets" />
<argument /> <!-- default package -->
<argument type="collection" /> <!-- named packages -->
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<parameters>
<parameter key="templating.engine.php.class">Symfony\Bundle\FrameworkBundle\Templating\PhpEngine</parameter>
<parameter key="templating.helper.slots.class">Symfony\Component\Templating\Helper\SlotsHelper</parameter>
<parameter key="templating.helper.assets.class">Symfony\Component\Templating\Helper\CoreAssetsHelper</parameter>
<parameter key="templating.helper.actions.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper</parameter>
<parameter key="templating.helper.router.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\RouterHelper</parameter>
<parameter key="templating.helper.request.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\RequestHelper</parameter>
Expand All @@ -19,9 +18,6 @@
<parameter key="templating.form.engine.class">Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine</parameter>
<parameter key="templating.form.renderer.class">Symfony\Component\Form\FormRenderer</parameter>
<parameter key="templating.globals.class">Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables</parameter>
<parameter key="templating.asset.path_package.class">Symfony\Bundle\FrameworkBundle\Templating\Asset\PathPackage</parameter>
<parameter key="templating.asset.url_package.class">Symfony\Component\Templating\Asset\UrlPackage</parameter>
<parameter key="templating.asset.package_factory.class">Symfony\Bundle\FrameworkBundle\Templating\Asset\PackageFactory</parameter>
</parameters>

<services>
Expand All @@ -37,35 +33,6 @@
<tag name="templating.helper" alias="slots" />
</service>

<service id="templating.helper.assets" class="%templating.helper.assets.class%">
<tag name="templating.helper" alias="assets" />
<argument /> <!-- default package -->
<argument type="collection" /> <!-- named packages -->
</service>

<service id="templating.asset.path_package" class="%templating.asset.path_package.class%" abstract="true">
<argument type="service" id="request" />
<argument /> <!-- version -->
<argument /> <!-- version format -->
</service>

<service id="templating.asset.url_package" class="%templating.asset.url_package.class%" abstract="true">
<argument /> <!-- base urls -->
<argument /> <!-- version -->
<argument /> <!-- version format -->
</service>

<service id="templating.asset.request_aware_package" class="Symfony\Component\Templating\Asset\PackageInterface" abstract="true">
<factory service="templating.asset.package_factory" method="getPackage" />
<argument type="service" id="request" strict="false" />
<argument /> <!-- HTTP id -->
<argument /> <!-- SSL id -->
</service>

<service id="templating.asset.package_factory" class="%templating.asset.package_factory.class%">
<argument type="service" id="service_container" />
</service>

<service id="templating.helper.request" class="%templating.helper.request.class%">
<tag name="templating.helper" alias="request" />
<argument type="service" id="request_stack" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Security\Core\SecurityContext;

/**
* GlobalVariables is the entry point for Symfony global variables in Twig templates.
* GlobalVariables is the entry point for Symfony global variables in PHP templates.
*
* @author Fabien Potencier <[email protected]>
*/
Expand Down
160 changes: 160 additions & 0 deletions src/Symfony/Bundle/TwigBundle/AppVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBundle;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;

/**
* Exposes some Symfony parameters and services as an "app" global variable.
*
* @author Fabien Potencier <[email protected]>
*/
class AppVariable
{
private $security;
private $tokenStorage;
private $requestStack;
private $environment;
private $debug;

/**
* @deprecated since version 2.7, to be removed in 3.0.
*/
public function setSecurity(SecurityContextInterface $security)
{
$this->security = $security;
}

public function setTokenStorage(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}

public function setRequestStack(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

public function setEnvironment($environment)
{
$this->environment = $environment;
}

public function setDebug($debug)
{
$this->debug = (bool) $debug;
}

/**
* Returns the security context service.
*
* @deprecated since version 2.6, to be removed in 3.0.
*
* @return SecurityContext|null The security context
*/
public function getSecurity()
{
trigger_error('The "app.security" variable is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);

if (null === $this->security) {
throw new \RuntimeException('The "app.security" variable is not available.');
}

return $this->security;
}

/**
* Returns the current user.
*
* @return mixed
*
* @see TokenInterface::getUser()
*/
public function getUser()
{
if (null === $this->tokenStorage) {
throw new \RuntimeException('The "app.user" variable is not available.');
}

if (!$token = $this->tokenStorage->getToken()) {
return;
}

$user = $token->getUser();
if (is_object($user)) {
return $user;
}
}

/**
* Returns the current request.
*
* @return Request|null The HTTP request object
*/
public function getRequest()
{
if (null === $this->requestStack) {
throw new \RuntimeException('The "app.request" variable is not available.');
}

return $this->requestStack->getCurrentRequest();
}

/**
* Returns the current session.
*
* @return Session|null The session
*/
public function getSession()
{
if (null === $this->requestStack) {
throw new \RuntimeException('The "app.session" variable is not available.');
}

if ($request = $this->getRequest()) {
return $request->getSession();
}
}

/**
* Returns the current app environment.
*
* @return string The current environment string (e.g 'dev')
*/
public function getEnvironment()
{
if (null === $this->environment) {
throw new \RuntimeException('The "app.environment" variable is not available.');
}

return $this->environment;
}

/**
* Returns the current app debug mode.
*
* @return bool The current debug mode
*/
public function getDebug()
{
if (null === $this->debug) {
throw new \RuntimeException('The "app.debug" variable is not available.');
}

return $this->debug;
}
}
Loading