diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php index 5bdc35e140a83e6e27c024b928d8740eeb4d41a2..3f13fb3320c605df1cb54811b237ef15776f1b35 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 c0de857afd9b280181602dae31f0302725c5f9c8..497ad870999219dce26dfd744d77414f344e9cd8 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 92cd27add9a5dea056b89e6aef10b78aaf6fb986..9f667ec662083240c252de8b9cb12435e2fd22a9 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()