Skip to content
Snippets Groups Projects
Commit 3cdfd824 authored by Demian Katz's avatar Demian Katz
Browse files

Smarter LESS compiler cache behavior.

parent 03302459
No related merge requests found
...@@ -468,6 +468,9 @@ class UtilController extends AbstractBase ...@@ -468,6 +468,9 @@ class UtilController extends AbstractBase
{ {
$argv = $this->consoleOpts->getRemainingArgs(); $argv = $this->consoleOpts->getRemainingArgs();
$compiler = new \VuFindTheme\LessCompiler(true); $compiler = new \VuFindTheme\LessCompiler(true);
$cacheManager = $this->getServiceLocator()->get('VuFind\CacheManager');
$cacheDir = $cacheManager->getCacheDir() . 'less/';
$compiler->setTempPath($cacheDir);
$compiler->compile($argv); $compiler->compile($argv);
return $this->getSuccessResponse(); return $this->getSuccessResponse();
} }
......
...@@ -46,6 +46,13 @@ class LessCompiler ...@@ -46,6 +46,13 @@ class LessCompiler
*/ */
protected $basePath; protected $basePath;
/**
* Temporary directory for cached files.
*
* @var string
*/
protected $tempPath;
/** /**
* Fake base path used for generating absolute paths in CSS. * Fake base path used for generating absolute paths in CSS.
* *
...@@ -68,6 +75,7 @@ class LessCompiler ...@@ -68,6 +75,7 @@ class LessCompiler
public function __construct($verbose = false) public function __construct($verbose = false)
{ {
$this->basePath = realpath(__DIR__ . '/../../../../'); $this->basePath = realpath(__DIR__ . '/../../../../');
$this->tempPath = sys_get_temp_dir();
$this->verbose = $verbose; $this->verbose = $verbose;
} }
...@@ -83,6 +91,18 @@ class LessCompiler ...@@ -83,6 +91,18 @@ class LessCompiler
$this->basePath = $path; $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. * Compile the scripts.
* *
...@@ -178,17 +198,16 @@ class LessCompiler ...@@ -178,17 +198,16 @@ class LessCompiler
); );
return; return;
} }
$outDir = sys_get_temp_dir();
$outFile = \Less_Cache::Regen( $outFile = \Less_Cache::Regen(
array($lessDir . $less => $this->fakePath . "themes/$theme/css/less"), array($lessDir . $less => $this->fakePath . "themes/$theme/css/less"),
array( array(
'cache_dir' => $outDir, 'cache_dir' => $this->tempPath,
'cache_method' => false, 'cache_method' => false,
'compress' => true, 'compress' => true,
'import_dirs' => $directories 'import_dirs' => $directories
) )
); );
$css = file_get_contents($outDir . '/' . $outFile); $css = file_get_contents($this->tempPath . '/' . $outFile);
if (!is_dir(dirname($finalFile))) { if (!is_dir(dirname($finalFile))) {
mkdir(dirname($finalFile)); mkdir(dirname($finalFile));
} }
......
...@@ -102,6 +102,7 @@ class LessCompilerTest extends Unit\TestCase ...@@ -102,6 +102,7 @@ class LessCompilerTest extends Unit\TestCase
} }
$this->compiler = new LessCompiler(); $this->compiler = new LessCompiler();
$this->compiler->setBasePath($temp . '/vufind_less_comp_test'); $this->compiler->setBasePath($temp . '/vufind_less_comp_test');
$this->compiler->setTempPath($temp . '/vufind_less_comp_test/cache');
} }
public static function tearDownAfterClass() public static function tearDownAfterClass()
...@@ -109,24 +110,18 @@ class LessCompilerTest extends Unit\TestCase ...@@ -109,24 +110,18 @@ class LessCompilerTest extends Unit\TestCase
$temp = sys_get_temp_dir(); $temp = sys_get_temp_dir();
$testDest = $temp . '/vufind_less_comp_test/'; $testDest = $temp . '/vufind_less_comp_test/';
// Delete directory structure // Delete directory structure
unlink($testDest . 'themes/child/less/compiled.less'); self::delTree($testDest);
unlink($testDest . 'themes/child/theme.config.php'); }
unlink($testDest . 'themes/empty/theme.config.php');
unlink($testDest . 'themes/parent/less/relative/relative.less'); // adapted from http://php.net/manual/en/function.rmdir.php
unlink($testDest . 'themes/parent/less/compiled.less'); protected static function delTree($dir)
unlink($testDest . 'themes/parent/less/parent.less'); {
unlink($testDest . 'themes/parent/theme.config.php'); $files = array_diff(scandir($dir), array('.', '..'));
rmdir($testDest . 'themes/child/css'); foreach ($files as $file) {
rmdir($testDest . 'themes/child/less'); is_dir("$dir/$file")
rmdir($testDest . 'themes/child'); ? self::delTree("$dir/$file") : unlink("$dir/$file");
rmdir($testDest . 'themes/empty'); }
rmdir($testDest . 'themes/parent/less/relative'); rmdir($dir);
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);
} }
public function testThemeCompile() public function testThemeCompile()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment