From 3cdfd82460dceb202138891b517f936a41b38e9c Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 29 Oct 2014 14:01:02 -0400
Subject: [PATCH] Smarter LESS compiler cache behavior.

---
 .../Controller/UtilController.php             |  3 ++
 .../src/VuFindTheme/LessCompiler.php          | 25 +++++++++++++--
 .../src/VuFindTest/LessCompilerTest.php       | 31 ++++++++-----------
 3 files changed, 38 insertions(+), 21 deletions(-)

diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
index 5bdc35e140a..3f13fb3320c 100644
--- a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
+++ b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
@@ -468,6 +468,9 @@ class UtilController extends AbstractBase
     {
         $argv = $this->consoleOpts->getRemainingArgs();
         $compiler = new \VuFindTheme\LessCompiler(true);
+        $cacheManager = $this->getServiceLocator()->get('VuFind\CacheManager');
+        $cacheDir = $cacheManager->getCacheDir() . 'less/';
+        $compiler->setTempPath($cacheDir);
         $compiler->compile($argv);
         return $this->getSuccessResponse();
     }
diff --git a/module/VuFindTheme/src/VuFindTheme/LessCompiler.php b/module/VuFindTheme/src/VuFindTheme/LessCompiler.php
index c0de857afd9..497ad870999 100644
--- a/module/VuFindTheme/src/VuFindTheme/LessCompiler.php
+++ b/module/VuFindTheme/src/VuFindTheme/LessCompiler.php
@@ -46,6 +46,13 @@ class LessCompiler
      */
     protected $basePath;
 
+    /**
+     * Temporary directory for cached files.
+     *
+     * @var string
+     */
+    protected $tempPath;
+
     /**
      * Fake base path used for generating absolute paths in CSS.
      *
@@ -68,6 +75,7 @@ class LessCompiler
     public function __construct($verbose = false)
     {
         $this->basePath = realpath(__DIR__ . '/../../../../');
+        $this->tempPath = sys_get_temp_dir();
         $this->verbose = $verbose;
     }
 
@@ -83,6 +91,18 @@ class LessCompiler
         $this->basePath = $path;
     }
 
+    /**
+     * Set temporary directory
+     *
+     * @param string $path Path to set
+     *
+     * @return void
+     */
+    public function setTempPath($path)
+    {
+        $this->tempPath = rtrim($path, '/');
+    }
+
     /**
      * Compile the scripts.
      *
@@ -178,17 +198,16 @@ class LessCompiler
             );
             return;
         }
-        $outDir = sys_get_temp_dir();
         $outFile = \Less_Cache::Regen(
             array($lessDir . $less => $this->fakePath . "themes/$theme/css/less"),
             array(
-                'cache_dir' => $outDir,
+                'cache_dir' => $this->tempPath,
                 'cache_method' => false,
                 'compress' => true,
                 'import_dirs' => $directories
             )
         );
-        $css = file_get_contents($outDir . '/' . $outFile);
+        $css = file_get_contents($this->tempPath . '/' . $outFile);
         if (!is_dir(dirname($finalFile))) {
             mkdir(dirname($finalFile));
         }
diff --git a/module/VuFindTheme/tests/unit-tests/src/VuFindTest/LessCompilerTest.php b/module/VuFindTheme/tests/unit-tests/src/VuFindTest/LessCompilerTest.php
index 92cd27add9a..9f667ec6620 100644
--- a/module/VuFindTheme/tests/unit-tests/src/VuFindTest/LessCompilerTest.php
+++ b/module/VuFindTheme/tests/unit-tests/src/VuFindTest/LessCompilerTest.php
@@ -102,6 +102,7 @@ class LessCompilerTest extends Unit\TestCase
         }
         $this->compiler = new LessCompiler();
         $this->compiler->setBasePath($temp . '/vufind_less_comp_test');
+        $this->compiler->setTempPath($temp . '/vufind_less_comp_test/cache');
     }
 
     public static function tearDownAfterClass()
@@ -109,24 +110,18 @@ class LessCompilerTest extends Unit\TestCase
         $temp = sys_get_temp_dir();
         $testDest = $temp . '/vufind_less_comp_test/';
         // Delete directory structure
-        unlink($testDest . 'themes/child/less/compiled.less');
-        unlink($testDest . 'themes/child/theme.config.php');
-        unlink($testDest . 'themes/empty/theme.config.php');
-        unlink($testDest . 'themes/parent/less/relative/relative.less');
-        unlink($testDest . 'themes/parent/less/compiled.less');
-        unlink($testDest . 'themes/parent/less/parent.less');
-        unlink($testDest . 'themes/parent/theme.config.php');
-        rmdir($testDest . 'themes/child/css');
-        rmdir($testDest . 'themes/child/less');
-        rmdir($testDest . 'themes/child');
-        rmdir($testDest . 'themes/empty');
-        rmdir($testDest . 'themes/parent/less/relative');
-        rmdir($testDest . 'themes/parent/less');
-        rmdir($testDest . 'themes/parent/css/relative');
-        rmdir($testDest . 'themes/parent/css');
-        rmdir($testDest . 'themes/parent');
-        rmdir($testDest . 'themes');
-        rmdir($testDest);
+        self::delTree($testDest);
+    }
+
+    // adapted from http://php.net/manual/en/function.rmdir.php
+    protected static function delTree($dir)
+    {
+        $files = array_diff(scandir($dir), array('.', '..'));
+        foreach ($files as $file) {
+            is_dir("$dir/$file")
+                ? self::delTree("$dir/$file") : unlink("$dir/$file");
+        }
+        rmdir($dir);
     }
 
     public function testThemeCompile()
-- 
GitLab