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

Skip to content
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 @@ -166,21 +166,36 @@ private function addTemplatingSection(NodeBuilder $rootNode)
->beforeNormalization()
->ifTrue(function($v){ return !is_array($v); })
->then(function($v){ return array($v); })
->end()
->end()
->prototype('scalar')
->beforeNormalization()
->ifTrue(function($v) { return is_array($v) && isset($v['id']); })
->then(function($v){ return $v['id']; })
->end()
->end()
->end()
->end()
->fixXmlConfig('loader')
->arrayNode('loaders')
->beforeNormalization()
->ifTrue(function($v){ return !is_array($v); })
->then(function($v){ return array($v); })
->end()
->end()
->prototype('scalar')->end()
->end()
->fixXmlConfig('package')
->arrayNode('packages')
->useAttributeAsKey('name')
->prototype('array')
->scalarNode('version')->defaultNull()->end()
->fixXmlConfig('base_url')
->arrayNode('base_urls')
->prototype('scalar')
->beforeNormalization()
->ifTrue(function($v) { return is_array($v) && isset($v['value']); })
->then(function($v){ return $v['value']; })
->end()
->end()
->end()
->end()
->end()
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ private function registerTemplatingConfiguration(array $config, ContainerBuilder
$container->setParameter('templating.assets.base_urls', $config['assets_base_urls']);
}

$packages = array();
foreach ($config['packages'] as $name => $package) {
$packages[$name] = new Definition('Symfony\\Component\\Templating\\Asset\\AssetPackage', array(
$package['base_urls'],
$package['version'],
));
}
$container->setParameter('templating.assets.packages', $packages);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<xsd:element name="loader" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="engine" type="templating_engine" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="assets-base-url" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="package" type="package" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>

<xsd:attribute name="assets-version" type="xsd:string" />
Expand All @@ -102,6 +103,15 @@
<xsd:attribute name="id" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="package">
<xsd:sequence>
<xsd:element name="base-url" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>

<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="version" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="translator">
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="fallback" type="xsd:string" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<argument type="service" id="request" strict="false" />
<argument>%templating.assets.base_urls%</argument>
<argument>%templating.assets.version%</argument>
<argument>%templating.assets.packages%</argument>
</service>

<service id="templating.helper.request" class="%templating.helper.request.class%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class AssetsHelper extends BaseAssetsHelper
* @param Request $request A Request instance
* @param string|array $baseURLs The domain URL or an array of domain URLs
* @param string $version The version
* @param array $packages Asset packages indexed by name
*/
public function __construct(Request $request, $baseURLs = array(), $version = null)
public function __construct(Request $request, $baseURLs = array(), $version = null, $packages = array())
{
parent::__construct($request->getBasePath(), $baseURLs, $version);
parent::__construct($request->getBasePath(), $baseURLs, $version, $packages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
'cache_warmer' => true,
'engines' => array('php', 'twig'),
'loader' => array('loader.foo', 'loader.bar'),
'packages' => array(
'images' => array(
'version' => '1.0.0',
'base_urls' => array('http://images1.example.com', 'http://images2.example.com'),
),
'foo' => array(
'version' => '1.0.0',
),
'bar' => array(
'base_urls' => array('http://bar1.example.com', 'http://bar2.example.com'),
),
),
),
'translator' => array(
'enabled' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
<app:engine id="php" />
<app:engine id="twig" />
<app:assets-base-url>http://cdn.example.com</app:assets-base-url>
<app:package name="images" version="1.0.0">
<app:base-url>http://images1.example.com</app:base-url>
<app:base-url>http://images2.example.com</app:base-url>
</app:package>
<app:package name="foo" version="1.0.0" />
<app:package name="bar">
<app:base-url>http://bar1.example.com</app:base-url>
<app:base-url>http://bar2.example.com</app:base-url>
</app:package>
</app:templating>
<app:translator enabled="true" fallback="fr" />
<app:validation enabled="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ framework:
cache_warmer: true
engines: [php, twig]
loader: [loader.foo, loader.bar]
packages:
images:
version: 1.0.0
base_urls: ["http://images1.example.com", "http://images2.example.com"]
foo:
version: 1.0.0
bar:
base_urls: ["http://images1.example.com", "http://images2.example.com"]
translator:
enabled: true
fallback: fr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public function getUrl($name, array $parameters = array())
return $this->container->get('router')->generate($name, $parameters, true);
}

public function getAssetUrl($location)
public function getAssetUrl($location, $packageName = null)
{
return $this->container->get('templating.helper.assets')->getUrl($location);
return $this->container->get('templating.helper.assets')->getUrl($location, $packageName);
}

/**
Expand Down
63 changes: 63 additions & 0 deletions src/Symfony/Component/Templating/Asset/AssetPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\Templating\Asset;

/**
* An asset package.
*
* @author Kris Wallsmith <[email protected]>
*/
class AssetPackage implements AssetPackageInterface
{
private $baseUrls;
private $version;

/**
* Constructor.
*
* @param array|string $baseUrls The domain URL or an array of domain URLs
* @param string $version The version
*/
public function __construct($baseUrls = array(), $version = null)
{
$this->baseUrls = array();
$this->version = $version;

if (!is_array($baseUrls)) {
$baseUrls = (array) $baseUrls;
}

foreach ($baseUrls as $baseUrl) {
$this->baseUrls[] = rtrim($baseUrl, '/');
}
}

public function getVersion()
{
return $this->version;
}

public function getBaseUrl($path)
{
$count = count($this->baseUrls);

if (0 === $count) {
return '';
}

if (1 === $count) {
return $this->baseUrls[0];
}

return $this->baseUrls[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
}
}
36 changes: 36 additions & 0 deletions src/Symfony/Component/Templating/Asset/AssetPackageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Templating\Asset;

/**
* Asset package interface.
*
* @author Kris Wallsmith <[email protected]>
*/
interface AssetPackageInterface
{
/**
* Returns the asset package version.
*
* @return string The version string
*/
function getVersion();

/**
* Returns a base URL for the supplied path.
*
* @param string $path An asset path
*
* @return string A base URL
*/
function getBaseUrl($path);
}
Loading