8000 [Asset] added the component · symfony/symfony@d33c41d · GitHub
[go: up one dir, main page]

Skip to content

Commit d33c41d

Browse files
committed
[Asset] added the component
1 parent ac389b6 commit d33c41d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1605
-5
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added LogoutUrlExtension (provides `logout_url` and `logout_path`)
88
* added an HttpFoundation extension (provides the `absolute_url` and the `relative_path` functions)
9+
* added AssetExtension (provides the `asset_path` function)
910

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

src/Symfony/Bridge/Twig/composer.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function getConfigTreeBuilder()
108108
$this->addSessionSection($rootNode);
109109
$this->addRequestSection($rootNode);
110110
$this->addTemplatingSection($rootNode);
111+
$this->addAssetsSection($rootNode);
111112
$this->addTranslatorSection($rootNode);
112113
$this->addValidationSection($rootNode);
113114
$this->addAnnotationsSection($rootNode);
@@ -347,8 +348,8 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
347348
->info('templating configuration')
348349
->canBeUnset()
349350
->children()
350-
->scalarNode('assets_version')->defaultValue(null)->end()
351-
->scalarNode('assets_version_format')->defaultValue('%%s?%%s')->end()
351+
->scalarNode('assets_version')->defaultNull()->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')->end()
352+
->scalarNode('assets_version_format')->defaultValue('%%s?%%s')->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')->end()
352353
->scalarNode('hinclude_default_template')->defaultNull()->end()
353354
->arrayNode('form')
354355
->addDefaultsIfNotSet()
@@ -370,6 +371,7 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
370371
->fixXmlConfig('assets_base_url')
371372
->children()
372373
->arrayNode('assets_base_urls')
374+
->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')
373375
->performNoDeepMerging()
374376
->addDefaultsIfNotSet()
375377
->beforeNormalization()
@@ -417,6 +419,7 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
417419
->fixXmlConfig('package')
418420
->children()
419421
->arrayNode('packages')
422+
->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')
420423
->useAttributeAsKey('name')
421424
->prototype('array')
422425
->fixXmlConfig('base_url')
@@ -452,6 +455,54 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
452455
;
453456
}
454457

458+
private function addAssetsSection(ArrayNodeDefinition $rootNode)
459+
{
460+
$rootNode
461+
->children()
462+
->arrayNode('assets')
463+
->info('assets configuration')
464+
->canBeUnset()
465+
->fixXmlConfig('base_url')
466+
->children()
467+
->scalarNode('version')->defaultNull()->end()
468+
->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
469+
->scalarNode('base_path')->defaultValue('')->end()
470+
->arrayNode('base_urls')
471+
->requiresAtLeastOneElement()
472+
->beforeNormalization()
473+
->ifTrue(function ($v) { return !is_array($v); })
474+
->then(function ($v) { return array($v); })
475+
->end()
476+
->prototype('scalar')->end()
477+
->end()
478+
->end()
479+
->fixXmlConfig('package')
480+
->children()
481+
->arrayNode('packages')
482+
->useAttributeAsKey('name')
483+
->prototype('array')
484+
->fixXmlConfig('base_url')
485+
->children()
486+
->scalarNode('version')->defaultNull()->end()
487+
->scalarNode('version_format')->defaultNull()->end()
488+
->scalarNode('base_path')->defaultValue('')->end()
489+
->arrayNode('base_urls')
490+
->requiresAtLeastOneElement()
491+
->beforeNormalization()
492+
->ifTrue(function ($v) { return !is_array($v); })
493+
->then(function ($v) { return array($v); })
494+
->end()
495+
->prototype('scalar')->end()
496+
->end()
497+
->end()
498+
->end()
499+
->end()
500+
->end()
501+
->end()
502+
->end()
503+
;
504+
}
505+
455506
private function addTranslatorSection(ArrayNodeDefinition $rootNode)
456507
{
457508
$rootNode

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

Lines changed: 84 additions & 0 deletions
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
}
@@ -632,6 +636,86 @@ private function createTemplatingPackageDefinition(ContainerBuilder $container,
632636
return $package;
633637
}
634638

639+
/**
640+
* Loads the assets configuration.
641+
*
642+
* @param array $config A assets configuration array
643+
* @param ContainerBuilder $container A ContainerBuilder instance
644+
* @param XmlFileLoader $loader An XmlFileLoader instance
645+
*/
646+
private function registerAssetsConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
647+
{
648+
$loader->load('assets.xml');
649+
650+
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format']);
651+
652+
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
653+
$container->setDefinition('assets._default_package', $defaultPackage);
654+
655+
$namedPackages = array();
656+
foreach ($config['packages'] as $name => $package) {
657+
if (null === $package['version']) {
658+
$version = $defaultVersion;
659+
} else {
660+
$format = $package['version_format'] ?: $config['version_format'];
661+
662+
$version = $this->createVersion($container, $package['version'], $format, $name);
663+
}
664+
665+
$container->setDefinition('assets._package_'.$name, $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version));
666+
$namedPackages[$name] = new Reference('assets._package_'.$name);
667+
}
668+
669+
$container->getDefinition('assets.packages')
670+
->replaceArgument(0, new Reference('assets._default_package'))
671+
->replaceArgument(1, $namedPackages)
672+
;
673+
}
674+
675+
/**
676+
* Returns a definition for an asset package.
677+
*/
678+
private function createPackageDefinition($basePath, array $baseUrls, Reference $version)
679+
{
680+
if ($basePath && $baseUrls) {
681+
throw new \LogicException('An asset package cannot have base URLs and base paths.');
682+
}
683+
684+
if (!$baseUrls) {
685+
$package = new DefinitionDecorator('assets.path_package');
686+
687+
return $package
688+
->setPublic(false)
689+
->replaceArgument(0, $basePath)
690+
->replaceArgument(1, $version)
691+
;
692+
}
693+
694+
$package = new DefinitionDecorator('assets.url_package');
695+
696+
10000 return $package
697+
->setPublic(false)
698+
->replaceArgument(0, $baseUrls)
699+
->replaceArgument(1, $version)
700+
;
701+
}
702+
703+
private function createVersion(ContainerBuilder $container, $version, $format, $name = null)
704+
{
705+
if (!$version) {
706+
return new Reference('assets.empty_version_strategy');
707+
}
708+
709+
$def = new DefinitionDecorator('assets.static_version_strategy');
710+
$def
711+
->replaceArgument(0, $version)
712+
->replaceArgument(1, $format)
713+
;
714+
$container->setDefinition('assets._version_'.$name, $def);
715+
716+
return new Reference('assets._version_'.$name);
717+
}
718+
635719
/**
636720
* Loads the translator configuration.
637721
*
Lines changed: 42 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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
{

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"doctrine/annotations": "~1.0"
3333
},
3434
"require-dev": {
35+
"symfony/asset": "~2.7|~3.0.0",
3536
"symfony/browser-kit": "~2.4|~3.0.0",
3637
"symfony/console": "~2.6|~3.0.0",
3738
"symfony/css-selector": "~2.0,>=2.0.5|~3.0.0",

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.7.0
5+
-----
6+
7+
* added support for the new Asset component (from Twig bridge)
8+
* deprecated the assets extension (use the one from the Twig bridge instead)
9+
410
2.6.0
511
-----
612

0 commit comments

Comments
 (0)
0