Theme trouble and fix
Thanks so much for this awesome plugin. I was having a couple issues with it as it was not throwing any errors with my theme's css folder being unwritable. I changed a few things around so I could use the current theme's less files dynamically and use Cake's built in themePath function.
The set_theme function:
private function set_theme() {
$theme_path = App::themePath($this->theme) . 'webroot' . DS;
$this->lessFolder = new Folder($theme_path.'less');
$this->cssFolder = new Folder($theme_path.'css');
}
The first part of the css helper function:
public function css($file) {
if ($this->theme) {
$this->set_theme();
}
...
The helper doesn't need the second parameter now if a theme is being used. Hope this helps!
I wanted to be able to compress the output css, so I put in that functionality. I also noticed that the old Lessify library was being used. So I updated it to the new lessc library.
At the top of the LessHelper.php file a changed lines 25-29 to:
App::import('Vendor', 'Less.Lessc',
array(
'file' => 'lessphp' . DS . 'lessc.inc.php'
)
);
And I refactored the auto_compile_less function to look like:
public function auto_compile_less($lessFilename, $cssFilename, $compressed = 'compressed') {
// Check if cache & output folders are writable and the less file exists.
if (!is_writable(CACHE.'less')) {
trigger_error(__d('cake_dev', '"%s" directory is NOT writable.', CACHE.'less'), E_USER_NOTICE);
return;
}
if (file_exists($lessFilename) == false) {
trigger_error(__d('cake_dev', 'File: "%s" not found.', $lessFilename), E_USER_NOTICE);
return;
}
// Cache location
$cacheFilename = CACHE . 'less' . DS .str_replace('/', '_', str_replace($this->lessFolder->path, '', $lessFilename) . ".cache");
// Load the cache
if (file_exists($cacheFilename)) {
$cache = unserialize(file_get_contents($cacheFilename));
} else {
$cache = $lessFilename;
}
$less = new lessc;
$less->setFormatter($compressed);
$new_cache = $less->cachedCompile($cache);
if (!is_array($cache) || $new_cache['updated'] > $cache['updated'] || file_exists($cssFilename) === false) {
$cssFile = new File($cssFilename, true);
if ($cssFile->write($new_cache['compiled']) === false) {
if (!is_writable(dirname($cssFilename))) {
trigger_error(__d('cake_dev', '"%s" directory is NOT writable.', dirname($cssFilename)), E_USER_NOTICE);
}
trigger_error(__d('cake_dev', 'Failed to write "%s"', $cssFilename), E_USER_NOTICE);
}
$cacheFile = new File($cacheFilename, true);
$cacheFile->write(serialize($new_cache));
}
}
Notice I added the $compressed variable to the function. If you don't want the output to be compressed you can set the variable like this:
echo $this->Less->css(
'lessfile',
null
);
It is possible to use 'lessjs', 'compressed' (default), 'classic', or null. Once again, thanks for building a great plugin.