Final Class Yiisoft\Assets\AssetConverter
| Inheritance | Yiisoft\Assets\AssetConverter |
|---|---|
| Implements | Yiisoft\Assets\AssetConverterInterface |
AssetConverter supports conversion of several popular script formats into JavaScript or CSS.
It is used by {@see \Yiisoft\Assets\AssetManager} to convert files after they have been published.
Psalm Types
| Name | Value |
|---|---|
| IsOutdatedCallback | callable |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Assets\AssetConverter | |
| convert() | Yiisoft\Assets\AssetConverter | |
| withCommand() | Returns a new instance with the specified command. | Yiisoft\Assets\AssetConverter |
| withForceConvert() | Returns a new instance with the specified force convert value. | Yiisoft\Assets\AssetConverter |
| withIsOutdatedCallback() | Returns a new instance with a callback that is used to check for outdated result. | Yiisoft\Assets\AssetConverter |
Method Details
| public mixed __construct ( \Yiisoft\Aliases\Aliases $aliases, \Psr\Log\LoggerInterface $logger, array $commands = [], boolean $forceConvert = false ) | ||
| $aliases | \Yiisoft\Aliases\Aliases |
The aliases instance. |
| $logger | \Psr\Log\LoggerInterface |
The logger instance. |
| $commands | array |
The commands that are used to perform the asset conversion. The keys are the asset file extension names, and the values are the corresponding target script types (either "css" or "js") and the commands used for the conversion. You may also use a {@link https://github.com/yiisoft/docs/blob/master/guide/en/concept/aliases.md} to specify the location of the command:
|
| $forceConvert | boolean |
Whether the source asset file should be converted even if its result already exists. See {@see \Yiisoft\Assets\withForceConvert()}. |
public function __construct(
private readonly Aliases $aliases,
private readonly LoggerInterface $logger,
array $commands = [],
private bool $forceConvert = false,
) {
$this->commands = array_merge($this->commands, $commands);
}
| public string convert ( string $asset, string $basePath, array $optionsConverter = [] ) | ||
| $asset | string | |
| $basePath | string | |
| $optionsConverter | array | |
public function convert(string $asset, string $basePath, array $optionsConverter = []): string
{
$pos = strrpos($asset, '.');
if ($pos !== false) {
$srcExt = substr($asset, $pos + 1);
$commandOptions = $this->buildConverterOptions($srcExt, $optionsConverter);
if (isset($this->commands[$srcExt])) {
[$ext, $command] = $this->commands[$srcExt];
$result = substr($asset, 0, $pos + 1) . $ext;
if ($this->forceConvert || $this->isOutdated($basePath, $asset, $result, $srcExt, $ext)) {
$this->runCommand($command, $basePath, $asset, $result, $commandOptions);
}
return $result;
}
}
return $asset;
}
Returns a new instance with the specified command.
Allows you to set a command that is used to perform the asset conversion {@see $commands}.
| public self withCommand ( string $from, string $to, string $command ) | ||
| $from | string |
The file extension of the format converting from. |
| $to | string |
The file extension of the format converting to. |
| $command | string |
The command to execute for conversion. Example:
|
public function withCommand(string $from, string $to, string $command): self
{
$new = clone $this;
$new->commands[$from] = [$to, $command];
return $new;
}
Returns a new instance with the specified force convert value.
| public self withForceConvert ( boolean $forceConvert ) | ||
| $forceConvert | boolean |
Whether the source asset file should be converted even if its result already exists.
Default is |
public function withForceConvert(bool $forceConvert): self
{
$new = clone $this;
$new->forceConvert = $forceConvert;
return $new;
}
Returns a new instance with a callback that is used to check for outdated result.
| public self withIsOutdatedCallback ( callable $isOutdatedCallback ) | ||
| $isOutdatedCallback | callable |
A PHP callback, which should be invoked to check whether asset conversion result is outdated. It will be invoked only if conversion target file exists and its modification time is older then the one of source file. Callback should match following signature:
where $basePath is the asset source directory; $sourceFile is the asset source file path, relative to $basePath; $targetFile is the asset target file path, relative to $basePath; $sourceExtension is the source asset file extension and $targetExtension is the target asset file extension, respectively. It should return
|
public function withIsOutdatedCallback(callable $isOutdatedCallback): self
{
$new = clone $this;
$new->isOutdatedCallback = $isOutdatedCallback;
return $new;
}
Signup or Login in order to comment.