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.
 /**
  * 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;
 }
Beispiel #2
0
    /**
     * @depends testConvert
     */
    public function testForceConvert()
    {
        $tmpPath = $this->tmpPath;
        file_put_contents($tmpPath . '/test.php', <<<EOF
<?php

echo microtime();
EOF
);
        $converter = new AssetConverter();
        $converter->commands['php'] = ['txt', 'php {from} > {to}'];
        $converter->convert('test.php', $tmpPath);
        $initialConvertTime = file_get_contents($tmpPath . '/test.txt');
        usleep(1);
        $converter->convert('test.php', $tmpPath);
        $this->assertEquals($initialConvertTime, file_get_contents($tmpPath . '/test.txt'));
        $converter->forceConvert = true;
        $converter->convert('test.php', $tmpPath);
        $this->assertNotEquals($initialConvertTime, file_get_contents($tmpPath . '/test.txt'));
    }
 /**
  * Converts a given LESS assets file into a CSS
  *
  * @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)
 {
     if (($dotPos = strrpos($asset, '.')) === false) {
         return $asset;
     }
     if (($ext = substr($asset, $dotPos + 1)) !== self::INPUT_EXT) {
         return parent::convert($asset, $basePath);
     }
     $assetFilemtime = @filemtime("{$basePath}/{$asset}");
     $result = $this->buildResult($asset, $dotPos, $this->cacheSuffix === true ? $assetFilemtime : null);
     $resultFilemtime = @filemtime("{$basePath}/{$result}");
     if ($resultFilemtime < $assetFilemtime) {
         $this->parseLess($basePath, $asset, $result);
     }
     return $result;
 }
 /**
  * Converts a given LESS assets file into a CSS
  *
  * @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)
 {
     if (($dotPos = strrpos($asset, '.')) === false) {
         return $asset;
     }
     //daca nu e less, poti sa returnezi fisierul ca nu trebuie convertit
     if (($ext = substr($asset, $dotPos + 1)) !== self::INPUT_EXT) {
         return parent::convert($asset, $basePath);
     }
     $this->setPrivates($basePath, $asset, $dotPos);
     if ($this->force) {
         $this->parseLess($basePath, $asset, $this->result);
     } elseif ($this->isChanged()) {
         $this->parseLess($basePath, $asset, $this->result);
     }
     return $this->result;
 }
Beispiel #5
0
    public function testConvert()
    {
        $tmpPath = \Yii::$app->runtimePath . '/assetConverterTest';
        if (!is_dir($tmpPath)) {
            mkdir($tmpPath, 0777, true);
        }
        file_put_contents($tmpPath . '/test.php', <<<EOF
<?php

echo "Hello World!
";
echo "Hello Yii!";
EOF
);
        $converter = new AssetConverter();
        $converter->commands['php'] = ['txt', 'php {from} > {to}'];
        $this->assertEquals('test.txt', $converter->convert('test.php', $tmpPath));
        $this->assertTrue(file_exists($tmpPath . '/test.txt'), 'Failed asserting that asset output file exists.');
        $this->assertEquals("Hello World!\nHello Yii!", file_get_contents($tmpPath . '/test.txt'));
    }
Beispiel #6
0
 public function convert($asset, $basePath)
 {
     $this->parsers = ArrayHelper::merge($this->defaultParsersOptions, $this->parsers);
     $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);
     }
     $resultFile = FileHelper::normalizePath(DIRECTORY_SEPARATOR . substr($asset, 0, $pos + 1) . $this->parsers[$ext]['output']);
     $from = $basePath . DIRECTORY_SEPARATOR . $asset;
     $to = $basePath . $resultFile;
     if (!$this->needRecompile($from, $to)) {
         return trim($resultFile, DIRECTORY_SEPARATOR);
     }
     $this->checkDestinationDir($basePath, $resultFile);
     $asConsoleCommand = isset($this->parsers[$ext]['asConsoleCommand']) && $this->parsers[$ext]['asConsoleCommand'];
     if ($asConsoleCommand) {
         if (isset($this->commands[$ext])) {
             list($distExt, $command) = $this->commands[$ext];
             $this->runCommand($command, $basePath, $asset, $resultFile);
         }
     } else {
         $class = $this->parsers[$ext]['class'];
         $parser = new $class($this->parsers[$ext]['options']);
         $parserOptions = isset($this->parsers[$ext]['options']) ? $this->parsers[$ext]['options'] : array();
         $parser->parse($from, $to, $parserOptions);
     }
     if (YII_DEBUG) {
         Yii::info("Converted {$asset} into {$resultFile} ", __CLASS__);
     }
     //$resultFile=str_replace(Yii::getAlias('@webroot'), '', $to);
     $resultFile = trim(FileHelper::normalizePath($resultFile, '/'), '/');
     return $resultFile;
 }
 /**
  * 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 = $this->destinationDir . '/' . ltrim(substr($asset, 0, $pos + 1), '/') . $parserConfig['output'];
     $from = $basePath . '/' . ltrim($asset, '/');
     $to = $basePath . '/' . $resultFile;
     if (!$this->needRecompile($from, $to)) {
         return $resultFile;
     }
     $this->checkDestinationDir($basePath, $resultFile);
     $asConsoleCommand = isset($parserConfig['asConsoleCommand']) && $parserConfig['asConsoleCommand'];
     if ($asConsoleCommand) {
         //can't use parent function because it not support destination directory
         if (isset($this->commands[$ext])) {
             list($distExt, $command) = $this->commands[$ext];
             $this->runCommand($command, $basePath, $asset, $resultFile);
         }
     } else {
         $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;
 }