UrlPackage
extends Package
in package
Package that adds a base URL to asset URLs in addition to a version.
The package allows to use more than one base URLs in which case it randomly chooses one for each asset; it also guarantees that any given path will always use the same base URL to be nice with HTTP caching mechanisms.
When the request context is available, this package can choose the best base URL to use based on the current request scheme:
- For HTTP request, it chooses between all base URLs;
- For HTTPs requests, it chooses between HTTPs base URLs and relative protocol URLs or falls back to any base URL if no secure ones are available.
Tags
Table of Contents
Properties
- $baseUrls : array<string|int, mixed>
- $context : mixed
- $sslPackage : self|null
- $versionStrategy : mixed
Methods
- __construct() : mixed
- getBaseUrl() : string
- Returns the base URL for a path.
- getUrl() : string
- Returns an absolute or root-relative public path.
- getVersion() : string
- Returns the asset version for an asset.
- chooseBaseUrl() : int
- Determines which base URL to use for the given path.
- getContext() : ContextInterface
- getVersionStrategy() : VersionStrategyInterface
- isAbsoluteUrl() : bool
- getSslUrls() : mixed
Properties
$baseUrls
private
array<string|int, mixed>
$baseUrls
= []
$context
private
mixed
$context
$sslPackage
private
self|null
$sslPackage
= null
$versionStrategy
private
mixed
$versionStrategy
Methods
__construct()
public
__construct(string|array<string|int, string> $baseUrls, VersionStrategyInterface $versionStrategy[, ContextInterface $context = null ]) : mixed
Parameters
- $baseUrls : string|array<string|int, string>
-
Base asset URLs
- $versionStrategy : VersionStrategyInterface
- $context : ContextInterface = null
getBaseUrl()
Returns the base URL for a path.
public
getBaseUrl(string $path) : string
Parameters
- $path : string
Return values
stringgetUrl()
Returns an absolute or root-relative public path.
public
getUrl(string $path) : string
Parameters
- $path : string
Return values
stringgetVersion()
Returns the asset version for an asset.
public
getVersion(string $path) : string
Parameters
- $path : string
Return values
stringchooseBaseUrl()
Determines which base URL to use for the given path.
protected
chooseBaseUrl(string $path) : int
Override this method to change the default distribution strategy. This method should always return the same base URL index for a given path.
Parameters
- $path : string
Return values
intgetContext()
protected
getContext() : ContextInterface
Return values
ContextInterfacegetVersionStrategy()
protected
getVersionStrategy() : VersionStrategyInterface
Return values
VersionStrategyInterfaceisAbsoluteUrl()
protected
isAbsoluteUrl(string $url) : bool
Parameters
- $url : string
Return values
boolgetSslUrls()
private
getSslUrls(array<string|int, mixed> $urls) : mixed
Parameters
- $urls : array<string|int, mixed>