lithium\net\http\Media::filterAssetPath()

public static method

Performs checks and applies transformations to asset paths, including verifying that the virtual path exists on the filesystem, appending a timestamp, prepending an asset host, or applying a user-defined filter.

Parameters

  • string $asset

    A full asset path, relative to the public web path of the application.

  • mixed $path

    Path information for the asset type.

  • array $config

    The configuration array of the library from which the asset is being loaded.

  • array $options

    The array of options passed to asset() (see the $options parameter of Media::asset()).

Returns

mixed

Returns a modified path to a web asset, or false, if the path fails a check.

Source

	public static function filterAssetPath($asset, $path, array $config, array $options = []) {
		$config += ['assets' => null];

		if ($options['check'] || $options['timestamp']) {
			$file = static::path($asset, $options['type'], $options);
		}
		if ($options['check'] && !is_file($file)) {
			return false;
		}
		$isAbsolute = ($asset && $asset[0] === '/');

		if ($isAbsolute && $options['base'] && strpos($asset, $options['base']) !== 0) {
			$asset = "{$options['base']}{$asset}";
		} elseif (!$isAbsolute) {
			$asset = Text::insert(key($path), ['path' => $asset] + $options);
		}

		if (is_array($options['filter']) && !empty($options['filter'])) {
			$filter = $options['filter'];
			$asset = str_replace(array_keys($filter), array_values($filter), $asset);
		}

		if ($options['timestamp'] && is_file($file)) {
			$separator = (strpos($asset, '?') !== false) ? '&' : '?';
			$asset .= $separator . filemtime($file);
		}

		if ($host = $config['assets']) {
			$type = $options['type'];
			$env  = Environment::get();
			$base = isset($host[$env][$type]) ? $host[$env][$type] : null;
			$base = (isset($host[$type]) && !$base) ? $host[$type] : $base;

			if ($base) {
				return "{$base}{$asset}";
			}
		}
		return $asset;
	}