From 829b8c456c000765a66f3a920dac8bc47a7c9358 Mon Sep 17 00:00:00 2001 From: Steve Guns Date: Fri, 3 Apr 2015 07:46:03 +0200 Subject: [PATCH 1/3] Less: - Added support for external variables that can be used in the less files, can be configured via the configuration - Automatically register "published-url" (full css path) and "published-base-url" (current asset bundle published folder) - Send out an event right before a compile so that other things can be accomplished - Makes sure to obey the users' asset configuration regarding fileMode Converter: - Create the compiling directory with the correct permissions --- Converter.php | 11 +++++--- Less.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/Converter.php b/Converter.php index b962dc6..41e8e25 100644 --- a/Converter.php +++ b/Converter.php @@ -77,7 +77,7 @@ public function convert($asset, $basePath) $parserConfig = ArrayHelper::merge($this->defaultParsersOptions[$ext], $this->parsers[$ext]); if ($this->destinationDir) { $this->destinationDir .= '/'; - } + } $resultFile = $this->destinationDir . substr($asset, 0, $pos + 1) . $parserConfig['output']; $from = $basePath . '/' . $asset; @@ -115,9 +115,12 @@ public function needRecompile($from, $to) public function checkDestinationDir($basePath, $file) { - $distDir = dirname($basePath . '/' . $file); - if (!is_dir($distDir)) { - mkdir($distDir, $this->destinationDirPerms, true); + $dstDir = dirname($basePath . '/' . $file); + if (!is_dir($dstDir)) { + mkdir($dstDir, $this->destinationDirPerms, true); + $assetManager = \Yii::$app->assetManager; + if ($assetManager->dirMode) + @chmod($dstDir, $assetManager->dirMode); } } } diff --git a/Less.php b/Less.php index 62326fa..a7c4264 100644 --- a/Less.php +++ b/Less.php @@ -3,10 +3,40 @@ use Yii; use yii\caching\FileCache; +use yii\base\Event; class Less extends Parser { - public $auto = false; + /** Name of the event that will be sent right before a compile. It will include filenames and the compiler instance */ + const EVENT_BEFORE_COMPILE = 'beforeCompile'; + + /** @var bool true to automatically try to determine what to compile using the file cache manager */ + public $auto = false; + + /** @var bool true to compress the output */ + public $compressed = false; + + /** @var bool If true, adjust urls to be relative, false means don't touch */ + public $relativeUrls = true; + + /** + * Set of variables to register and that will be available in the less files. + * Note: The value should be in less format (strings are double quoted etc) + * eg "variables" => ['some-variable' => '"variable quoted content"'] + * + * On top of these 2 others will be added: + * - published-url: Full url of the file currently being compiled + * - published-base-url: Base Url of the assets folder + * + * @var array + */ + public $variables = []; + + /** + * Temporary runtime data so the event handlers can access it + * @var array + */ + public $runtime = null; /** * Parse a Less file to CSS @@ -14,6 +44,38 @@ class Less extends Parser public function parse($src, $dst, $options) { $this->auto = isset($options['auto']) ? $options['auto'] : $this->auto; + + $variables = $this->variables; + $assetManager = Yii::$app->assetManager; + + // Final url of the file being compiled + $assetUrl = substr($dst, strpos($assetManager->basePath, $assetManager->baseUrl)); + $variables['published-url'] = '"' . $assetUrl . '"'; + + // Root for the published folder + $variables['published-base-url'] = '"/' . implode('/', array_slice(explode('/', ltrim($assetUrl, '/')), 0, 2)) . '"'; + + $less = new \lessc(); + $less->setVariables($variables); + + // Compressed setting + if ($this->compressed) { + $less->setFormatter('compressed'); + } + \Less_Parser::$default_options['compress'] = $this->compressed; + \Less_Parser::$default_options['relativeUrls'] = $this->relativeUrls; + + // Send out pre-compile event + $event = new \yii\base\Event(); + $this->runtime = [ + 'sourceFile' => $src, + 'destinationFile' => $dst, + 'compiler' => $less, + ]; + $event->sender = $this; + + Event::trigger($this, self::EVENT_BEFORE_COMPILE, $event); + try { if ($this->auto) { /* @var FileCache $cacheMgr */ @@ -35,8 +97,14 @@ public function parse($src, $dst, $options) $less = new \lessc(); $less->compileFile($src, $dst); } - } catch (Exception $e) { - throw new Exception(__CLASS__ . ': Failed to compile less file : ' . $e->getMessage() . '.'); + + // If needed, respect the users configuration + if ($assetManager->fileMode !== null) + @chmod($dst, $assetManager->fileMode); + + unset($this->less); + } catch (\Exception $e) { + throw new \Exception(__CLASS__ . ': Failed to compile less file : ' . $e->getMessage() . '.'); } } } \ No newline at end of file From 046bc209f0291cd81cb7c5d7cd0e98742564bcd5 Mon Sep 17 00:00:00 2001 From: Steve Guns Date: Fri, 3 Apr 2015 08:00:53 +0200 Subject: [PATCH 2/3] Formatting --- Less.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Less.php b/Less.php index a7c4264..97db757 100644 --- a/Less.php +++ b/Less.php @@ -30,13 +30,13 @@ class Less extends Parser * * @var array */ - public $variables = []; + public $variables = []; /** * Temporary runtime data so the event handlers can access it * @var array */ - public $runtime = null; + public $runtime = null; /** * Parse a Less file to CSS @@ -67,13 +67,8 @@ public function parse($src, $dst, $options) // Send out pre-compile event $event = new \yii\base\Event(); - $this->runtime = [ - 'sourceFile' => $src, - 'destinationFile' => $dst, - 'compiler' => $less, - ]; + $this->runtime = ['sourceFile' => $src, 'destinationFile' => $dst, 'compiler' => $less]; $event->sender = $this; - Event::trigger($this, self::EVENT_BEFORE_COMPILE, $event); try { From 4d5aab9028e0c26c3ced54c1b117228ebb1e3159 Mon Sep 17 00:00:00 2001 From: Steve Guns Date: Fri, 3 Apr 2015 08:08:30 +0200 Subject: [PATCH 3/3] Forgot to remove the old creation of lessc instances --- Less.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Less.php b/Less.php index 97db757..e248f5f 100644 --- a/Less.php +++ b/Less.php @@ -81,7 +81,6 @@ public function parse($src, $dst, $options) if ($cache === false || (@filemtime($dst) < @filemtime($src))) { $cache = $src; } - $less = new \lessc(); $newCache = $less->cachedCompile($cache); if (!is_array($cache) || ($newCache["updated"] > $cache["updated"])) { @@ -89,7 +88,6 @@ public function parse($src, $dst, $options) file_put_contents($dst, $newCache['compiled']); } } else { - $less = new \lessc(); $less->compileFile($src, $dst); }