Skip to content

Commit dce8f08

Browse files
committed
feature symfony#28476 Added different protocols to be allowed as asset base_url (alexander-schranz)
This PR was merged into the 4.2-dev branch. Discussion ---------- Added different protocols to be allowed as asset base_url | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | fixes symfony#28238 | License | MIT | Doc PR | symfony/symfony-docs#10347 In some cases you want to use the `file://` as base_url when you are for example generating pdf and want to avoid network requests to improve the pdf generation performance: ```yaml framework: assets: packages: pdf: base_url: "file://%kernel.project_dir%/public" ``` usage: ```twig {{ asset('image.jpg', 'pdf' }} ``` Commits ------- 2e21834 added different protocols to be allowed as asset base_urls
2 parents bc43155 + 2e21834 commit dce8f08

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/Symfony/Component/Asset/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+
4.2.0
5+
-----
6+
7+
* added different protocols to be allowed as asset base_urls
8+
49
3.4.0
510
-----
611

src/Symfony/Component/Asset/Tests/UrlPackageTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,28 @@ public function getConfigs()
3333
array('http://example.net', '', 'http://example.com/foo', 'http://example.com/foo'),
3434
array('http://example.net', '', 'https://example.com/foo', 'https://example.com/foo'),
3535
array('http://example.net', '', '//example.com/foo', '//example.com/foo'),
36+
array('file:///example/net', '', 'file:///example/com/foo', 'file:///example/com/foo'),
37+
array('ftp://example.net', '', 'ftp://example.com', 'ftp://example.com'),
3638

3739
array('http://example.com', '', '/foo', 'http://example.com/foo?v1'),
3840
array('http://example.com', '', 'foo', 'http://example.com/foo?v1'),
3941
array('http://example.com/', '', 'foo', 'http://example.com/foo?v1'),
4042
array('http://example.com/foo', '', 'foo', 'http://example.com/foo/foo?v1'),
4143
array('http://example.com/foo/', '', 'foo', 'http://example.com/foo/foo?v1'),
44+
array('file:///example/com/foo/', '', 'foo', 'file:///example/com/foo/foo?v1'),
4245

4346
array(array('http://example.com'), '', '/foo', 'http://example.com/foo?v1'),
4447
array(array('http://example.com', 'http://example.net'), '', '/foo', 'http://example.com/foo?v1'),
4548
array(array('http://example.com', 'http://example.net'), '', '/fooa', 'http://example.net/fooa?v1'),
49+
array(array('file:///example/com', 'file:///example/net'), '', '/foo', 'file:///example/com/foo?v1'),
50+
array(array('ftp://example.com', 'ftp://example.net'), '', '/fooa', 'ftp://example.net/fooa?v1'),
4651

4752
array('http://example.com', 'version-%2$s/%1$s', '/foo', 'http://example.com/version-v1/foo'),
4853
array('http://example.com', 'version-%2$s/%1$s', 'foo', 'http://example.com/version-v1/foo'),
4954
array('http://example.com', 'version-%2$s/%1$s', 'foo/', 'http://example.com/version-v1/foo/'),
5055
array('http://example.com', 'version-%2$s/%1$s', '/foo/', 'http://example.com/version-v1/foo/'),
56+
array('file:///example/com', 'version-%2$s/%1$s', '/foo/', 'file:///example/com/version-v1/foo/'),
57+
array('ftp://example.com', 'version-%2$s/%1$s', '/foo/', 'ftp://example.com/version-v1/foo/'),
5158
);
5259
}
5360

@@ -97,11 +104,21 @@ public function testNoBaseUrls()
97104
}
98105

99106
/**
107+
* @dataProvider getWrongBaseUrlConfig
108+
*
100109
* @expectedException \Symfony\Component\Asset\Exception\InvalidArgumentException
101110
*/
102-
public function testWrongBaseUrl()
111+
public function testWrongBaseUrl($baseUrls)
103112
{
104-
new UrlPackage(array('not-a-url'), new EmptyVersionStrategy());
113+
new UrlPackage($baseUrls, new EmptyVersionStrategy());
114+
}
115+
116+
public function getWrongBaseUrlConfig()
117+
{
118+
return array(
119+
array('not-a-url'),
120+
array('not-a-url-with-query?query=://'),
121+
);
105122
}
106123

107124
private function getContext($secure)

src/Symfony/Component/Asset/UrlPackage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private function getSslUrls($urls)
129129
foreach ($urls as $url) {
130130
if ('https://' === substr($url, 0, 8) || '//' === substr($url, 0, 2)) {
131131
$sslUrls[] = $url;
132-
} elseif ('http://' !== substr($url, 0, 7)) {
132+
} elseif (null === parse_url($url, PHP_URL_SCHEME)) {
133133
throw new InvalidArgumentException(sprintf('"%s" is not a valid URL', $url));
134134
}
135135
}

0 commit comments

Comments
 (0)