Skip to content

[RFC][Asset] Assets Base Path Should Support Empty Values #28391

Closed
@kiler129

Description

@kiler129

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:

->arrayNode('base_urls')
->requiresAtLeastOneElement()
->beforeNormalization()
->ifTrue(function ($v) { return !\is_array($v); })
->then(function ($v) { return array($v); })
->end()
->prototype('scalar')->end()

Which later on is processed by:

foreach ($urls as $url) {
if ('https://' === substr($url, 0, 8) || '//' === substr($url, 0, 2)) {
$sslUrls[] = $url;
} elseif ('http://' !== substr($url, 0, 7)) {
throw new InvalidArgumentException(sprintf('"%s" is not a valid URL', $url));
}
}

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    AssetRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions