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

Skip to content

[WIP] A new Asset Component #13143

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

Closed
wants to merge 13 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.7.0
-----

* added AssetExtension (provides the `asset` function)

2.5.0
-----

Expand Down
110 changes: 110 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/AssetExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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\Component\Twig\Extension;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Asset\Packages;

/**
* Twig extension for the Symfony Asset component.
*
* @author Fabien Potencier <[email protected]>
*/
class AssetExtension extends \Twig_Extension
{
private $packages;
private $requestStack;

public function __construct(Packages $packages, RequestStack $requestStack = null)
{
$this->packages = $packages;
$this->requestStack = $requestStack;
}

/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')),
new \Twig_SimpleFunction('assets_version', array($this, 'getAssetsVersion')),
);
}

/**
* Returns the public path of an asset.
*
* Absolute paths (i.e. http://...) are returned unmodified.
*
* @param string $path A public path
* @param string $packageName The name of the asset package to use
* @param bool $absolute Whether to return an absolute URL or a relative one
* @param string|bool|null $version A specific version
*
* @return string A public path which takes into account the base path and URL path
*/
public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
{
$url = $this->packages->getUrl($path, $packageName, $version);

if (!$absolute) {
return $url;
}

return $this->ensureUrlIsAbsolute($url);
}

/**
* Returns the version of the assets in a package.
*
* @param string $packageName
*
* @return int
*/
public function getAssetsVersion($packageName = null)
{
return $this->packages->getVersion($packageName);
}

/**
* Ensures an URL is absolute, if possible.
*
* @param string $url The URL that has to be absolute
*
* @throws \RuntimeException
*
* @return string The absolute URL
*/
private function ensureUrlIsAbsolute($url)
{
if (false !== strpos($url, '://') || '//' === substr($url, 0, 2)) {
return $url;
}

if (null === $this->requestStack) {
throw new \RuntimeException('To generate an absolute URL for an asset, the Symfony Routing component is required.');
}

return $this->requestStack->getMasterRequest()->getUriForPath($url);
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'asset';
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"suggest": {
"symfony/finder": "",
"symfony/asset": "For using the AssetExtension",
"symfony/form": "For using the FormExtension",
"symfony/http-kernel": "For using the HttpKernelExtension",
"symfony/routing": "For using the RoutingExtension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,13 @@

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
* @deprecated since 2.7, will be removed in 3.0
*/
class TemplatingAssetHelperPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('templating.helper.assets')) {
return;
}

$assetsHelperDefinition = $container->getDefinition('templating.helper.assets');
$args = $assetsHelperDefinition->getArguments();

if ('request' === $this->getPackageScope($container, $args[0])) {
$assetsHelperDefinition->setScope('request');

return;
}

if (!array_key_exists(1, $args)) {
return;
}

if (!is_array($args[1])) {
return;
}

foreach ($args[1] as $arg) {
if ('request' === $this->getPackageScope($container, $arg)) {
$assetsHelperDefinition->setScope('request');

break;
}
}
}

private function getPackageScope(ContainerBuilder $container, $package)
{
if ($package instanceof Reference) {
return $container->findDefinition((string) $package)->getScope();
}

if ($package instanceof Definition) {
return $package->getScope();
}

// Someone did some voodoo with a compiler pass. So we ignore this
// 'package'. Can we be sure, it's a package anyway?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ private function addRequestSection(ArrayNodeDefinition $rootNode)

private function addTemplatingSection(ArrayNodeDefinition $rootNode)
{
/** @deprecated, should be removed in 3.0 */
$organizeUrls = function ($urls) {
$urls += array(
'http' => array(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ 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');
$loader->load('assets.xml');

$this->createPackageDefinitions($config, $container);

$links = array(
'textmate' => 'txmt://open?url=file://%%f&line=%%l',
Expand All @@ -468,12 +470,9 @@ 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')
Expand All @@ -482,25 +481,8 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
$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');
}

// 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']);
$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);
$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 (!empty($config['loaders'])) {
$loaders = array_map(function ($loader) { return new Reference($loader); }, $config['loaders']);

Expand Down Expand Up @@ -531,6 +513,23 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
));

if (in_array('php', $config['engines'], true)) {
$loader->load('templating_php.xml');

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

$packages = $container->getDefinition('templating.asset.packages');
$container->getDefinition('templating.helper.assets')
->replaceArgument(0, $packages->getArgument(0))
->replaceArgument(1, $packages->getArgument(1))
;

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',
Expand All @@ -552,71 +551,36 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
}
}

/**
* Returns a definition for an asset package.
*/
private function createPackageDefinition(ContainerBuilder $container, array $httpUrls, array $sslUrls, $version, $format, $name = null)
private function createPackageDefinitions(array $config, ContainerBuilder $container)
{
if (!$httpUrls) {
$package = new DefinitionDecorator('templating.asset.path_package');
$package
->setPublic(false)
->setScope('request')
->replaceArgument(1, $version)
->replaceArgument(2, $format)
;

return $package;
}

if ($httpUrls == $sslUrls) {
$package = new DefinitionDecorator('templating.asset.url_package');
$package
->setPublic(false)
->replaceArgument(0, $sslUrls)
->replaceArgument(1, $version)
->replaceArgument(2, $format)
;
// 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']);
$container->setDefinition('templating.asset.default_package', $defaultPackage);

return $package;
$namedPackages = array();
foreach ($config['packages'] as $name => $package) {
$namedPackage[$name] = $this->createPackageDefinition($container, $package['base_urls']['http'], $package['base_urls']['ssl'], $package['version'], $package['version_format']);
}

$prefix = $name ? 'templating.asset.package.'.$name : 'templating.asset.default_package';

$httpPackage = new DefinitionDecorator('templating.asset.url_package');
$httpPackage
->replaceArgument(0, $httpUrls)
->replaceArgument(1, $version)
->replaceArgument(2, $format)
$container->getDefinition('templating.asset.packages')
->replaceArgument(0, $defaultPackage)
->replaceArgument(1, $namedPackages)
;
$container->setDefinition($prefix.'.http', $httpPackage);

if ($sslUrls) {
$sslPackage = new DefinitionDecorator('templating.asset.url_package');
$sslPackage
->replaceArgument(0, $sslUrls)
->replaceArgument(1, $version)
->replaceArgument(2, $format)
;
} else {
$sslPackage = new DefinitionDecorator('templating.asset.path_package');
$sslPackage
->setScope('request')
->replaceArgument(1, $version)
->replaceArgument(2, $format)
;
}
$container->setDefinition($prefix.'.ssl', $sslPackage);
}

/**
* Returns a definition for an asset package.
*/
private function createPackageDefinition(ContainerBuilder $container, array $httpUrls, array $sslUrls, $version, $format)
{
$package = new DefinitionDecorator('templating.asset.package');

$package = new DefinitionDecorator('templating.asset.request_aware_package');
$package
return $package
->setPublic(false)
->setScope('request')
->replaceArgument(1, $prefix.'.http')
->replaceArgument(2, $prefix.'.ssl')
->replaceArgument(1, array_merge($httpUrls, $sslUrls), $version)
->replaceArgument(2, $version)
->replaceArgument(3, $format)
;

return $package;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingAssetHelperPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass;
Expand Down Expand Up @@ -76,7 +75,6 @@ public function build(ContainerBuilder $container)
// but as late as possible to get resolved parameters
$container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->addCompilerPass(new TemplatingPass());
$container->addCompilerPass(new TemplatingAssetHelperPass());
$container->addCompilerPass(new AddConstraintValidatorsPass());
$container->addCompilerPass(new AddValidatorInitializersPass());
$container->addCompilerPass(new AddConsoleCommandPass());
Expand Down
Loading