0 follower

Final Class Yiisoft\Assets\AssetConverter

InheritanceYiisoft\Assets\AssetConverter
ImplementsYiisoft\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

Hide inherited 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

Hide inherited methods

__construct() public method

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:

[
    'styl' => ['css', '@app/node_modules/bin/stylus < {from} > {to}'],
]
$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);
}

            
convert() public method

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;
}

            
withCommand() public method

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:

$converter = $converter->withCommand('scss', 'css', 'sass {options} {from} {to}');

                public function withCommand(string $from, string $to, string $command): self
{
    $new = clone $this;
    $new->commands[$from] = [$to, $command];
    return $new;
}

            
withForceConvert() public method

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 false. You may want to set this to be true during the development stage to make sure the converted assets are always up-to-date. Do not set this to true on production servers as it will significantly degrade the performance.

                public function withForceConvert(bool $forceConvert): self
{
    $new = clone $this;
    $new->forceConvert = $forceConvert;
    return $new;
}

            
withIsOutdatedCallback() public method

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:

function (string $basePath, string $sourceFile, string $targetFile, string $sourceExtension, string $targetExtension) : bool

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 true is case asset should be reconverted. For example:

function ($basePath, $sourceFile, $targetFile, $sourceExtension, $targetExtension) {
    if (YII_ENV !== 'dev') {
        return false;
    }

    $resultModificationTime = @filemtime("$basePath/$result");
    foreach (FileHelper::findFiles($basePath, ['only' => ["*.{$sourceExtension}"]]) as $filename) {
        if ($resultModificationTime < @filemtime($filename)) {
            return true;
        }
    }

    return false;
}

                public function withIsOutdatedCallback(callable $isOutdatedCallback): self
{
    $new = clone $this;
    $new->isOutdatedCallback = $isOutdatedCallback;
    return $new;
}