lithium\net\http\Media::asset()
Calculates the web-accessible path to a static asset, usually a JavaScript, CSS or image file.
Parameters
-
string
$pathThe path to the asset, relative to the given
$types path and without a suffix. If the path contains a URI Scheme (eg.http://), no path munging will occur. -
string
$typeThe asset type. See
Media::$_assetsorMedia::assets(). -
array
$optionsContains setting for finding and handling the path, where the keys are the following:
'base': The base URL of your application. Defaults tonullfor no base path. This is usually set with the return value of a call toenv('base')on an instance oflithium\action\Request.'check': Check for the existence of the file before returning. Defaults tofalse.'filter': An array of key/value pairs representing simple string replacements to be done on a path once it is generated.'paths': An array of paths to search for the asset in. The paths should useText::insert()formatting. SeeMedia::$_assetsfor more.suffix: The suffix to attach to the path, generally a file extension.'timestamp': Appends the last modified time of the file to the path iftrue. Defaults tofalse.'library': The name of the library from which to load the asset. Defaults totrue, for the default library.
Returns
stringReturns the publicly-accessible absolute path to the static asset. If checking
for the asset's existence ($options['check']), returns false if it does not exist
in your /webroot directory, or the /webroot directories of one of your included
plugins.
Filter
This method can be filtered.
Source
public static function asset($path, $type, array $options = []) {
$options = static::_assetOptions($path, $type, $options);
$params = compact('path', 'type', 'options');
return Filters::run(get_called_class(), __FUNCTION__, $params, function($params) {
$path = $params['path'];
$type = $params['type'];
$options = $params['options'];
$library = $options['library'];
if (preg_match('/^(?:[a-z0-9-]+:)?\/\//i', $path)) {
return $path;
}
$config = Libraries::get($library);
$paths = $options['paths'];
$config['default'] ? end($paths) : reset($paths);
$options['library'] = basename($config['path']);
if ($options['suffix'] && strpos($path, $options['suffix']) === false) {
$path .= $options['suffix'];
}
return static::filterAssetPath($path, $paths, $config, compact('type') + $options);
});
}