yii\web\AssetConverter::convert PHP Method

convert() public method

Converts a given asset file into a CSS or JS file.
public convert ( string $asset, string $basePath ) : string
$asset string the asset file path, relative to $basePath
$basePath string the directory the $asset is relative to.
return string the converted asset file path, relative to $basePath.
    public function convert($asset, $basePath)
    {
        $pos = strrpos($asset, '.');
        if ($pos !== false) {
            $ext = substr($asset, $pos + 1);
            if (isset($this->commands[$ext])) {
                list($ext, $command) = $this->commands[$ext];
                $result = substr($asset, 0, $pos + 1) . $ext;
                if ($this->forceConvert || @filemtime("{$basePath}/{$result}") < @filemtime("{$basePath}/{$asset}")) {
                    $this->runCommand($command, $basePath, $asset, $result);
                }
                return $result;
            }
        }
        return $asset;
    }

Usage Example

 /**
  * Converts a given asset file into a CSS or JS file.
  * @param string $asset the asset file path, relative to $basePath
  * @param string $basePath the directory the $asset is relative to.
  * @return string the converted asset file path, relative to $basePath.
  */
 public function convert($asset, $basePath)
 {
     $pos = strrpos($asset, '.');
     if ($pos === false) {
         return parent::convert($asset, $basePath);
     }
     $ext = substr($asset, $pos + 1);
     if (!isset($this->parsers[$ext])) {
         return parent::convert($asset, $basePath);
     }
     $parserConfig = ArrayHelper::merge($this->defaultParsersOptions[$ext], $this->parsers[$ext]);
     $this->destinationDir = $this->destinationDir ? trim($this->destinationDir, '/') : '';
     $resultFile = strlen($this->destinationDir) > 0 ? $this->destinationDir . '/' : '' . ltrim(substr($asset, 0, $pos + 1), '/') . $parserConfig['output'];
     $from = $basePath . '/' . ltrim($asset, '/');
     $to = $basePath . '/' . $resultFile;
     if (!$this->needRecompile($from, $to) && !$this->force) {
         return $resultFile;
     }
     $this->checkDestinationDir($basePath, $resultFile);
     $parser = new $parserConfig['class']($parserConfig['options']);
     $parserOptions = isset($parserConfig['options']) ? $parserConfig['options'] : array();
     $parser->parse($from, $to, $parserOptions);
     if (YII_DEBUG) {
         Yii::info("Converted {$asset} into {$resultFile} ", __CLASS__);
     }
     return $resultFile;
 }
All Usage Examples Of yii\web\AssetConverter::convert
AssetConverter