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

Skip to content

Commit 582780a

Browse files
committed
[Asset] added the component
1 parent 5f2fcb7 commit 582780a

37 files changed

+1485
-7
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
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` function)
8+
49
2.5.0
510
-----
611

Lines changed: 110 additions & 0 deletions
Original file line number
Diff line numberDiff line change
@@ -0,0 +1,110 @@
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+
private $requestStack;
26+
27+
public function __construct(Packages $packages, RequestStack $requestStack = null)
28+
{
29+
$this->packages = $packages;
30+
$this->requestStack = $requestStack;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function getFunctions()
37+
{
38+
return array(
39+
new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')),
40+
new \Twig_SimpleFunction('assets_version', array($this, 'getAssetsVersion')),
41+
);
42+
}
43+
44+
/**
45+
* Returns the public path of an asset.
46+
*
47+
* Absolute paths (i.e. http://...) are returned unmodified.
48+
*
49+
* @param string $path A public path
50+
* @param string $packageName The name of the asset package to use
51+
* @param bool $absolute Whether to return an absolute URL or a relative one
52+
* @param string|bool|null $version A specific version
53+
*
54+
* @return string A public path which takes into account the base path and URL path
55+
*/
56+
public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
57+
{
58+
$url = $this->packages->getUrl($path, $packageName, $version);
59+
60+
if (!$absolute) {
61+
return $url;
62+
}
63+
64+
return $this->ensureUrlIsAbsolute($url);
65+
}
66+
67+
/**
68+
* Returns the version of the assets in a package.
69+
*
70+
* @param string $packageName
71+
*
72+
* @return int
73+
*/
74+
public function getAssetsVersion($packageName = null)
75+
{
76+
return $this->packages->getVersion($packageName);
77+
}
78+
79+
/**
80+
* Ensures an URL is absolute, if possible.
81+
*
82+
* @param string $url The URL that has to be absolute
83+
*
84+
* @throws \RuntimeException
85+
*
86+
* @return string The absolute URL
87+
*/
88+
private function ensureUrlIsAbsolute($url)
89+
{
90+
if (false !== strpos($url, '://') || '//' === substr($url, 0, 2)) {
91+
return $url;
92+
}
93+
94+
if (null === $this->requestStack) {
95+
throw new \RuntimeException('To generate an absolute URL for an asset, the Symfony Routing component is required.');
96+
}
97+
98+
return $this->requestStack->getMasterRequest()->getUriForPath($url);
99+
}
100+
101+
/**
102+
* Returns the name of the extension.
103+
*
104+
* @return string The extension name
105+
*/
106+
public function getName()
107+
{
108+
return 'asset';
109+
}
110+
}

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: 50 additions & 0 deletions
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(),
@@ -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')->defaultValue(null)->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

Lines changed: 84 additions & 3 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
}
@@ -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,83 @@ 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+
$this->createPackageDefinitions($config, $container);
638+
639+
// PHP templating engine
640+
/*
641+
$packages = $container->getDefinition('templating.asset.packages');
642+
$container->getDefinition('templating.helper.assets')
643+
->replaceArgument(0, $packages->getArgument(0))
644+
->replaceArgument(1, $packages->getArgument(1))
645+
;
646+
*/
647+
}
648+
649+
private function createPackageDefinitions(array $config, ContainerBuilder $container)
650+
{
651+
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $config['version'], $config['version_format']);
652+
$container->setDefinition('assets._default_package', $defaultPackage);
653+
654+
$namedPackages = array();
655+
foreach ($config['packages'] as $name => $package) {
656+
if (null === $package['version']) {
657+
$package['version'] = $config['version'];
658+
}
659+
660+
if (null === $package['version_format']) {
661+
$package['version_format'] = $config['version_format'];
662+
}
663+
664+
$container->setDefinition('assets._package_'.$name, $this->createPackageDefinition($package['base_path'], $package['base_urls'], $package['version'], $package['version_format']));
665+
$namedPackages[$name] = new Reference('assets._package_'.$name);
666+
}
667+
668+
$container->getDefinition('assets.packages')
669+
->replaceArgument(0, new Reference('assets._default_package'))
670+
->replaceArgument(1, $namedPackages)
671+
;
672+
}
673+
674+
/**
675+
* Returns a definition for an asset package.
676+
*/
677+
private function createPackageDefinition($basePath, array $baseUrls, $version, $format)
678+
{
679+
if ($basePath && $baseUrls) {
680+
throw new \LogicException('An asset package cannot have base URLs and base paths.');
681+
}
682+
683+
if (!$baseUrls) {
684+
$package = new DefinitionDecorator('assets.path_package');
685+
686+
return $package
687+
->setPublic(false)
688+
->replaceArgument(1, $basePath)
689+
->replaceArgument(2, $version)
690+
->replaceArgument(3, $format)
691+
;
692+
}
693+
694+
$package = new DefinitionDecorator('assets.url_package');
695+
return $package
696+
->setPublic(false)
697+
->replaceArgument(1, $baseUrls)
698+
->replaceArgument(2, $version)
699+
->replaceArgument(3, $format)
700+
;
701+
}
702+
622703
/**
623704
* Loads the translator configuration.
624705
*
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.path_package" class="Symfony\Component\Asset\RequestPathPackage" abstract="true">
15+
<argument type="service" id="request_stack" />
16+
<argument /> <!-- base path -->
17+
<argument /> <!-- version -->
18+
<argument /> <!-- version format -->
19+
</service>
20+
21+
<service id="assets.url_package" class="Symfony\Component\Asset\RequestUrlPackage" abstract="true">
22+
<argument type="service" id="request_stack" />
23+
<argument /> <!-- base URLs -->
24+
<argument /> <!-- version -->
25+
<argument /> <!-- version format -->
26+
</service>
27+
28+
</services>
29+
</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
{

0 commit comments

Comments
 (0)
0