Skip to content

Commit 8635a33

Browse files
committed
[Asset] added the component
1 parent 5f2fcb7 commit 8635a33

40 files changed

+1523
-8
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"psr/log": "~1.0"
2323
},
2424
"replace": {
25+
"symfony/asset": "self.version",
2526
"symfony/browser-kit": "self.version",
2627
"symfony/class-loader": "self.version",
2728
"symfony/config": "self.version",

src/Symfony/Bridge/Twig/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.7.0
5+
-----
6+
7+
* added AssetExtension (provides the `asset_path` function)
8+
49
2.5.0
510
-----
611

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\HttpFoundation\RequestStack;
15+
use Symfony\Component\Asset\Packages;
16+
17+
/**
18+
* Twig extension for the Symfony Asset component.
19+
*
20+
* @author Fabien Potencier <fabien@symfony.com>
21+
*/
22+
class AssetExtension extends \Twig_Extension
23+
{
24+
private $packages;
25+
26+
public function __construct(Packages $packages)
27+
{
28+
$this->packages = $packages;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getFunctions()
35+
{
36+
return array(
37+
new \Twig_SimpleFunction('asset_path', array($this, 'getAssetPath')),
38+
new \Twig_SimpleFunction('asset_version', array($this, 'getAssetVersion')),
39+
);
40+
}
41+
42+
/**
43+
* Returns the public path of an asset.
44+
*
45+
* If the package used to generate the path is an instance of
46+
* UrlPackage, you will always get a URL and not a path.
47+
*
48+
* @param string $path A public path
49+
* @param string $packageName The name of the asset package to use
50+
*
51+
* @return string The public path of the asset
52+
*/
53+
public function getAssetPath($path, $packageName = null)
54+
{
55+
return $this->packages->getUrl($path, $packageName);
56+
}
57+
58+
/**
59+
* Returns the version of an asset.
60+
*
61+
* @param string $path A public path
62+
* @param string $packageName The name of the asset package to use
63+
*
64+
* @return string The asset version
65+
*/
66+
public function getAssetVersion($path, $packageName = null)
67+
{
68+
return $this->packages->getVersion($path, $packageName);
69+
}
70+
71+
/**
72+
* Returns the name of the extension.
73+
*
74+
* @return string The extension name
75+
*/
76+
public function getName()
77+
{
78+
return 'asset';
79+
}
80+
}

src/Symfony/Bridge/Twig/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"suggest": {
3939
"symfony/finder": "",
40+
"symfony/asset": "For using the AssetExtension",
4041
"symfony/form": "For using the FormExtension",
4142
"symfony/http-kernel": "For using the HttpKernelExtension",
4243
"symfony/routing": "For using the RoutingExtension",

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TemplatingAssetHelperPass.php

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Reference;
1818

19+
/**
20+
* @deprecated since 2.7, will be removed in 3.0
21+
*/
1922
class TemplatingAssetHelperPass implements CompilerPassInterface
2023
{
2124
public function process(ContainerBuilder $container)

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

+51-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public function getConfigTreeBuilder()
9898
$this->addSessionSection($rootNode);
9999
$this->addRequestSection($rootNode);
100100
$this->addTemplatingSection($rootNode);
101+
$this->addAssetsSection($rootNode);
101102
$this->addTranslatorSection($rootNode);
102103
$this->addValidationSection($rootNode);
103104
$this->addAnnotationsSection($rootNode);
@@ -311,6 +312,7 @@ private function addRequestSection(ArrayNodeDefinition $rootNode)
311312

312313
private function addTemplatingSection(ArrayNodeDefinition $rootNode)
313314
{
315+
/** @deprecated since 2.7, will be removed in 3.0 */
314316
$organizeUrls = function ($urls) {
315317
$urls += array(
316318
'http' => array(),
@@ -337,7 +339,7 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
337339
->info('templating configuration')
338340
->canBeUnset()
339341
->children()
340-
->scalarNode('assets_version')->defaultValue(null)->end()
342+
->scalarNode('assets_version')->defaultNull()->end()
341343
->scalarNode('assets_version_format')->defaultValue('%%s?%%s')->end()
342344
->scalarNode('hinclude_default_template')->defaultNull()->end()
343345
->arrayNode('form')
@@ -442,6 +444,54 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
442444
;
443445
}
444446

447+
private function addAssetsSection(ArrayNodeDefinition $rootNode)
448+
{
449+
$rootNode
450+
->children()
451+
->arrayNode('assets')
452+
->info('assets configuration')
453+
->canBeUnset()
454+
->fixXmlConfig('base_url')
455+
->children()
456+
->scalarNode('version')->defaultNull()->end()
457+
->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
458+
->scalarNode('base_path')->defaultValue('')->end()
459+
->arrayNode('base_urls')
460+
->requiresAtLeastOneElement()
461+
->beforeNormalization()
462+
->ifTrue(function ($v) { return !is_array($v); })
463+
->then(function ($v) { return array($v); })
464+
->end()
465+
->prototype('scalar')->end()
466+
->end()
467+
->end()
468+
->fixXmlConfig('package')
469+
->children()
470+
->arrayNode('packages')
471+
->useAttributeAsKey('name')
472+
->prototype('array')
473+
->fixXmlConfig('base_url')
474+
->children()
475+
->scalarNode('version')->defaultNull()->end()
476+
->scalarNode('version_format')->defaultNull()->end()
477+
->scalarNode('base_path')->defaultValue('')->end()
478+
->arrayNode('base_urls')
479+
->requiresAtLeastOneElement()
480+
->beforeNormalization()
481+
->ifTrue(function ($v) { return !is_array($v); })
482+
->then(function ($v) { return array($v); })
483+
->end()
484+
->prototype('scalar')->end()
485+
->end()
486+
->end()
487+
->end()
488+
->end()
489+
->end()
490+
->end()
491+
->end()
492+
;
493+
}
494+
445495
private function addTranslatorSection(ArrayNodeDefinition $rootNode)
446496
{
447497
$rootNode

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

+86-3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public function load(array $configs, ContainerBuilder $container)
101101

102102
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
103103

104+
if (isset($config['assets'])) {
105+
$this->registerAssetsConfiguration($config['assets'], $container, $loader);
106+
}
107+
104108
if (isset($config['templating'])) {
105109
$this->registerTemplatingConfiguration($config['templating'], $config['ide'], $container, $loader);
106110
}
@@ -488,11 +492,11 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
488492
}
489493

490494
// 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']);
495+
$defaultPackage = $this->createTemplatingPackageDefinition($container, $config['assets_base_urls']['http'], $config['assets_base_urls']['ssl'], $config['assets_version'], $config['assets_version_format']);
492496
$container->setDefinition('templating.asset.default_package', $defaultPackage);
493497
$namedPackages = array();
494498
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);
499+
$namedPackage = $this->createTemplatingPackageDefinition($container, $package['base_urls']['http'], $package['base_urls']['ssl'], $package['version'], $package['version_format'], $name);
496500
$container->setDefinition('templating.asset.package.'.$name, $namedPackage);
497501
$namedPackages[$name] = new Reference('templating.asset.package.'.$name);
498502
}
@@ -555,7 +559,7 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
555559
/**
556560
* Returns a definition for an asset package.
557561
*/
558-
private function createPackageDefinition(ContainerBuilder $container, array $httpUrls, array $sslUrls, $version, $format, $name = null)
562+
private function createTemplatingPackageDefinition(ContainerBuilder $container, array $httpUrls, array $sslUrls, $version, $format, $name = null)
559563
{
560564
if (!$httpUrls) {
561565
$package = new DefinitionDecorator('templating.asset.path_package');
@@ -619,6 +623,85 @@ private function createPackageDefinition(ContainerBuilder $container, array $htt
619623
return $package;
620624
}
621625

626+
/**
627+
* Loads the assets configuration.
628+
*
629+
* @param array $config A assets configuration array
630+
* @param ContainerBuilder $container A ContainerBuilder instance
631+
* @param XmlFileLoader $loader An XmlFileLoader instance
632+
*/
633+
private function registerAssetsConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
634+
{
635+
$loader->load('assets.xml');
636+
637+
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format']);
638+
639+
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
640+
$container->setDefinition('assets._default_package', $defaultPackage);
641+
642+
$namedPackages = array();
643+
foreach ($config['packages'] as $name => $package) {
644+
if (null === $package['version']) {
645+
$version = $defaultVersion;
646+
} else {
647+
$format = $package['version_format'] ?: $config['version_format'];
648+
649+
$version = $this->createVersion($container, $package['version'], $format, $name);
650+
}
651+
652+
$container->setDefinition('assets._package_'.$name, $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version));
653+
$namedPackages[$name] = new Reference('assets._package_'.$name);
654+
}
655+
656+
$container->getDefinition('assets.packages')
657+
->replaceArgument(0, new Reference('assets._default_package'))
658+
->replaceArgument(1, $namedPackages)
659+
;
660+
}
661+
662+
/**
663+
* Returns a definition for an asset package.
664+
*/
665+
private function createPackageDefinition($basePath, array $baseUrls, Reference $version)
666+
{
667+
if ($basePath && $baseUrls) {
668+
throw new \LogicException('An asset package cannot have base URLs and base paths.');
669+
}
670+
671+
if (!$baseUrls) {
672+
$package = new DefinitionDecorator('assets.path_package');
673+
674+
return $package
675+
->setPublic(false)
676+
->replaceArgument(0, $basePath)
677+
->replaceArgument(1, $version)
678+
;
679+
}
680+
681+
$package = new DefinitionDecorator('assets.url_package');
682+
return $package
683+
->setPublic(false)
684+
->replaceArgument(0, $baseUrls)
685+
->replaceArgument(1, $version)
686+
;
687+
}
688+
689+
private function createVersion(ContainerBuilder $container, $version, $format, $name = null)
690+
{
691+
if (!$version) {
692+
return new Reference('assets.empty_version_strategy');
693+
}
694+
695+
$def = new DefinitionDecorator('assets.static_version_strategy');
696+
$def
697+
->replaceArgument(0, $version)
698+
->replaceArgument(1, $format)
699+
;
700+
$container->setDefinition('assets._version_'.$name, $def);
701+
702+
return new Reference('assets._version_'.$name);
703+
}
704+
622705
/**
623706
* Loads the translator configuration.
624707
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
<services>
8+
9+
<service id="assets.packages" class="Symfony\Component\Asset\Packages">
10+
<argument /> <!-- default package -->
11+
<argument type="collection" /> <!-- named packages -->
12+
</service>
13+
14+
<service id="assets.context" class="Symfony\Component\Asset\Context\RequestStackContext">
15+
<argument type="service" id="request_stack" />
16+
</service>
17+
18+
<service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true">
19+
<argument /> <!-- base path -->
20+
<argument type="service" /> <!-- version strategy -->
21+
<call method="setContext">
22+
<argument type="service" id="assets.context" />
23+
</call>
24+
</service>
25+
26+
<service id="assets.url_package" class="Symfony\Component\Asset\UrlPackage" abstract="true">
27+
<argument /> <!-- base URLs -->
28+
<argument type="service" /> <!-- version strategy -->
29+
<call method="setContext">
30+
<argument type="service" id="assets.context" />
31+
</call>
32+
</service>
33+
34+
<service id="assets.static_version_strategy" class="Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy" abstract="true">
35+
<argument /> <!-- version -->
36+
<argument /> <!-- format -->
37+
</service>
38+
39+
<service id="assets.empty_version_strategy" class="Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy" public="false" />
40+
41+
</services>
42+
</container>

src/Symfony/Bundle/FrameworkBundle/Templating/Asset/PackageFactory.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Templating\Asset;
1313

14+
trigger_error('The Symfony\Bundle\FrameworkBundle\Templating\Asset\PackageFactory is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
15+
1416
use Symfony\Component\DependencyInjection\ContainerInterface;
1517
use Symfony\Component\HttpFoundation\Request;
1618
use Symfony\Component\Templating\Asset\PackageInterface;
@@ -19,6 +21,8 @@
1921
* Creates packages based on whether the current request is secure.
2022
*
2123
* @author Kris Wallsmith <kris@symfony.com>
24+
*
25+
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
2226
*/
2327
class PackageFactory
2428
{

src/Symfony/Bundle/FrameworkBundle/Templating/Asset/PathPackage.php

+4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\Templating\Asset\PathPackage as BasePathPackage;
1616

17+
trigger_error('The Symfony\Bundle\FrameworkBundle\Templating\Asset\PathPackage is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
18+
1719
/**
1820
* The path packages adds a version and a base path to asset URLs.
1921
*
2022
* @author Kris Wallsmith <kris@symfony.com>
23+
*
24+
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
2125
*/
2226
class PathPackage extends BasePathPackage
2327
{

0 commit comments

Comments
 (0)