added a way to provide asset base URLs in configuration
This commit is contained in:
parent
7514177b51
commit
d5a61e3bc5
@ -151,6 +151,10 @@ class WebExtension extends Extension
|
|||||||
$container->setParameter('templating.assets.version', $config['assets_version']);
|
$container->setParameter('templating.assets.version', $config['assets_version']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('assets_base_urls', $config)) {
|
||||||
|
$container->setParameter('templating.assets.base_urls', $config['assets_base_urls']);
|
||||||
|
}
|
||||||
|
|
||||||
// template paths
|
// template paths
|
||||||
$dirs = array('%kernel.root_dir%/views/%%bundle%%/%%controller%%/%%name%%%%format%%.%%renderer%%');
|
$dirs = array('%kernel.root_dir%/views/%%bundle%%/%%controller%%/%%name%%%%format%%.%%renderer%%');
|
||||||
foreach ($container->getParameter('kernel.bundle_dirs') as $dir) {
|
foreach ($container->getParameter('kernel.bundle_dirs') as $dir) {
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
<parameter key="templating.helper.code.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper</parameter>
|
<parameter key="templating.helper.code.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper</parameter>
|
||||||
<parameter key="templating.output_escaper">false</parameter>
|
<parameter key="templating.output_escaper">false</parameter>
|
||||||
<parameter key="templating.assets.version">null</parameter>
|
<parameter key="templating.assets.version">null</parameter>
|
||||||
|
<parameter key="templating.assets.base_urls" type="collection"></parameter>
|
||||||
<parameter key="debug.file_link_format">null</parameter>
|
<parameter key="debug.file_link_format">null</parameter>
|
||||||
</parameters>
|
</parameters>
|
||||||
|
|
||||||
@ -64,7 +65,7 @@
|
|||||||
<service id="templating.helper.assets" class="%templating.helper.assets.class%">
|
<service id="templating.helper.assets" class="%templating.helper.assets.class%">
|
||||||
<tag name="templating.helper" alias="assets" />
|
<tag name="templating.helper" alias="assets" />
|
||||||
<argument type="service" id="request" />
|
<argument type="service" id="request" />
|
||||||
<argument />
|
<argument>%templating.assets.base_urls%</argument>
|
||||||
<argument>%templating.assets.version%</argument>
|
<argument>%templating.assets.version%</argument>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
@ -148,15 +148,16 @@ class AssetsHelper extends Helper
|
|||||||
*/
|
*/
|
||||||
public function getUrl($path)
|
public function getUrl($path)
|
||||||
{
|
{
|
||||||
if (strpos($path, '://')) {
|
if (false !== strpos($path, '://')) {
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$base = $this->getBaseURL($path);
|
||||||
if (0 !== strpos($path, '/')) {
|
if (0 !== strpos($path, '/')) {
|
||||||
$path = $this->basePath.$path;
|
$path = $base ? '/'.$path : $this->basePath.$path;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->getBaseURL($path).$path.($this->version ? '?'.$this->version : '');
|
return $base.$path.($this->version ? '?'.$this->version : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,14 +82,14 @@ class AssetsHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL with a path');
|
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL with a path');
|
||||||
|
|
||||||
$helper = new AssetsHelper('/foo', 'http://www.example.com/');
|
$helper = new AssetsHelper('/foo', 'http://www.example.com/');
|
||||||
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
|
$this->assertEquals('http://www.example.com/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
|
||||||
$this->assertEquals('http://www.example.com/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL but not the base path on absolute paths');
|
$this->assertEquals('http://www.example.com/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL but not the base path on absolute paths');
|
||||||
|
|
||||||
$helper = new AssetsHelper('/bar', 'http://www.example.com/foo');
|
$helper = new AssetsHelper('/bar', 'http://www.example.com/foo');
|
||||||
$this->assertEquals('http://www.example.com/foo/bar/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
|
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
|
||||||
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL but not the base path on absolute paths');
|
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL but not the base path on absolute paths');
|
||||||
|
|
||||||
$helper = new AssetsHelper('/bar', 'http://www.example.com/foo', 'abcd');
|
$helper = new AssetsHelper('/bar', 'http://www.example.com/foo', 'abcd');
|
||||||
$this->assertEquals('http://www.example.com/foo/bar/foo.js?abcd', $helper->getUrl('foo.js'), '->getUrl() appends the version if defined');
|
$this->assertEquals('http://www.example.com/foo/foo.js?abcd', $helper->getUrl('foo.js'), '->getUrl() appends the version if defined');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user