Description
Description
The framework.assets.base_urls
allows setting base paths for assets (e.g. CDNs). The problem is on local we don't want to hardcode any values while giving an option to set it using environment variable.
The final configuration:
framework:
assets:
json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'
base_urls: '%env(CDN_PATH)%'
However this poses a problem: you cannot set empty path, since they only way to set empty path is to not include base_urls
property at all.
Solution?
The base_urls
is set by the following code:
Which later on is processed by:
symfony/src/Symfony/Component/Asset/UrlPackage.php
Lines 129 to 135 in 7be3bb2
This check explodes when it encounters null
. Since I want to use environment variable it cannot be done simply by modifying normalization, but needs to be handled on runtime. I don't really have a perfect solution for this, however modifying UrlPackage
by changing:
foreach ($baseUrls as $baseUrl) {
$this->baseUrls[] = rtrim($baseUrl, '/');
}
to
foreach ($baseUrls as $baseUrl) {
if ($baseUrl === null) {
$baseUrl = '/';
}
$this->baseUrls[] = rtrim($baseUrl, '/');
}
while adding && $url !== null
to elseif ('http://' !== substr($url, 0, 7)
seems to work.
Workaround
Currently I found there's a way to exploit how UrlPackage
works and just add env with //
value like so:
parameters:
env(CDN_PATH): '//'
framework:
assets:
json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'
base_urls: '%env(CDN_PATH)%'
This however smells like a hack...
Any suggestions how we could maybe accomplish this and if it will be desired?