diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php index f24357777df2328bfc24e55daf19cba5db39e80d..5bdc35e140a83e6e27c024b928d8740eeb4d41a2 100644 --- a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php +++ b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php @@ -467,7 +467,7 @@ class UtilController extends AbstractBase public function cssbuilderAction() { $argv = $this->consoleOpts->getRemainingArgs(); - $compiler = new \VuFindTheme\LessCompiler(); + $compiler = new \VuFindTheme\LessCompiler(true); $compiler->compile($argv); return $this->getSuccessResponse(); } diff --git a/module/VuFindTheme/src/VuFindTheme/LessCompiler.php b/module/VuFindTheme/src/VuFindTheme/LessCompiler.php index ffd18da152f7194ee0a47dfaf91c87b780977b72..65a0291a456692abdd161cc69e54fef43ddeb9fc 100644 --- a/module/VuFindTheme/src/VuFindTheme/LessCompiler.php +++ b/module/VuFindTheme/src/VuFindTheme/LessCompiler.php @@ -53,12 +53,28 @@ class LessCompiler */ protected $fakePath = '/zzzz_basepath_zzzz/'; + /** + * Console log? + * + * @var bool + */ + protected $verbose = '/zzzz_basepath_zzzz/'; + /** * Constructor */ - public function __construct() + public function __construct($verbose = false) { $this->basePath = realpath(__DIR__ . '/../../../../'); + $this->verbose = $verbose; + } + + /** + * Set base path + */ + public function setBasePath($path) + { + $this->basePath = $path; } /** @@ -90,10 +106,14 @@ class LessCompiler { $lessFiles = $this->getAllLessFiles($theme); if (empty($lessFiles)) { - Console::writeLine("No LESS in " . $theme); + if ($this->verbose) { + Console::writeLine("No LESS in " . $theme); + } return; } - Console::writeLine("Processing " . $theme); + if ($this->verbose) { + Console::writeLine("Processing " . $theme); + } foreach ($lessFiles as $less) { if (is_string($less)) { $this->compileFile($theme, $less); @@ -139,7 +159,9 @@ class LessCompiler list($fileName, ) = explode('.', $less); $finalFile = $finalOutDir . $fileName . '.css'; - Console::writeLine("\tcompiling '" . $less . "' into '" . $finalFile . "'"); + if ($this->verbose) { + Console::writeLine("\tcompiling '" . $less . "' into '" . $finalFile . "'"); + } $start = microtime(true); $directories = array(); @@ -150,9 +172,11 @@ class LessCompiler } $lessDir = $this->basePath . '/themes/' . $theme . '/less/'; if (!file_exists($lessDir . $less)) { - Console::writeLine( - "\t\t" . $lessDir . $less . ' does not exist; skipping.' - ); + if ($this->verbose) { + Console::writeLine( + "\t\t" . $lessDir . $less . ' does not exist; skipping.' + ); + } return; } $outDir = sys_get_temp_dir(); @@ -171,7 +195,9 @@ class LessCompiler } file_put_contents($finalFile, $this->makeRelative($css, $less)); - Console::writeLine("\t\t" . (microtime(true)-$start) . ' sec'); + if ($this->verbose) { + Console::writeLine("\t\t" . (microtime(true)-$start) . ' sec'); + } } /** diff --git a/module/VuFindTheme/tests/unit-tests/src/VuFindTest/LessCompilerTest.php b/module/VuFindTheme/tests/unit-tests/src/VuFindTest/LessCompilerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a7495a488014dba3bd90a27db5aecab250af4871 --- /dev/null +++ b/module/VuFindTheme/tests/unit-tests/src/VuFindTest/LessCompilerTest.php @@ -0,0 +1,136 @@ +<?php +/** + * LessCompiler Test Class + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/vufind2:unit_tests Wiki + */ +namespace VuFindTest; +use VuFindTheme\LessCompiler; + +/** + * LessCompiler Test Class + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/vufind2:unit_tests Wiki + */ +class LessCompilerTest extends Unit\TestCase +{ + /** + * Our brave test subject + * + * @var VuFindTheme\LessCompiler + */ + protected $testDest; + + /** + * Our brave test subject + * + * @var VuFindTheme\LessCompiler + */ + protected $compiler; + + public static function setUpBeforeClass() + { + $temp = sys_get_temp_dir(); + $testDest = $temp . '/vufind_less_comp_test/'; + + mkdir($testDest . 'themes/parent/css/', 0777, true); + mkdir($testDest . 'themes/parent/less/', 0777, true); + mkdir($testDest . 'themes/child/css/', 0777, true); + mkdir($testDest . 'themes/child/less/', 0777, true); + + file_put_contents( + $testDest . 'themes/parent/theme.config.php', + '<?php return array("extends"=>false, "less"=>array("compiled.less"));' + ); + file_put_contents( + $testDest . 'themes/child/theme.config.php', + '<?php return array("extends"=>"parent", "less"=>array("compiled.less"));' + ); + file_put_contents( + $testDest . 'themes/parent/less/compiled.less', + '@import "parent";' + ); + file_put_contents( + $testDest . 'themes/parent/less/parent.less', + 'body { color:#00D; a { color:#F00; } }' + ); + file_put_contents( + $testDest . 'themes/child/less/compiled.less', + '@import "parent"; @black: #000; div {border:1px solid @black;}' + ); + } + + public function setUp() + { + $temp = sys_get_temp_dir(); + $perms = fileperms($temp); + $this->testDest = $temp . '/vufind_less_comp_test/'; + if (!($perms & 0x0002)) { + $markTestSkipped('No write permissions in system temporary file'); + } + $this->compiler = new LessCompiler(); + $this->compiler->setBasePath($temp . '/vufind_less_comp_test'); + } + + public static function tearDownAfterClass() + { + $temp = sys_get_temp_dir(); + $testDest = $temp . '/vufind_less_comp_test/'; + + unlink($testDest . 'themes/child/less/compiled.less'); + unlink($testDest . 'themes/child/theme.config.php'); + 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/parent/less'); + rmdir($testDest . 'themes/parent/css'); + rmdir($testDest . 'themes/parent'); + rmdir($testDest . 'themes'); + rmdir($testDest); + } + + public function testThemeCompile() + { + $this->compiler->compile(array('child')); + $this->assertTrue(file_exists($this->testDest . 'themes/child/css/compiled.css')); + $this->assertFalse(file_exists($this->testDest . 'themes/parent/css/compiled.css')); + unlink($this->testDest . 'themes/child/css/compiled.css'); + } + + public function testEmptyCompile() + { + $this->compiler->compile(array()); + $this->assertTrue(file_exists($this->testDest . 'themes/child/css/compiled.css')); + $this->assertTrue(file_exists($this->testDest . 'themes/parent/css/compiled.css')); + unlink($this->testDest . 'themes/child/css/compiled.css'); + unlink($this->testDest . 'themes/parent/css/compiled.css'); + } +} \ No newline at end of file