/** * Compile less to css. Creates a cache-file of the last compiled less-file. * * This code is originally from the manual of lessphp. * * @param @less_fname string the filename of the less-file. * @param @css_fname string the filename of the css-file. * @param @cache_ext string the file-extension of the cache-file, added to the less filename. Default is '.cache'. * @returns boolean true if the css-file was changed, else returns false. */ function auto_compile_less($less_fname, $css_fname, $cache_ext = '.cache') { $cache_fname = $less_fname . $cache_ext; if (file_exists($cache_fname)) { $cache = unserialize(file_get_contents($cache_fname)); } else { $cache = $less_fname; } $new_cache = lessc::cexecute($cache); if (!is_array($cache) || $new_cache['updated'] > $cache['updated']) { file_put_contents($cache_fname, serialize($new_cache)); file_put_contents($css_fname, $new_cache['compiled']); return true; } return false; }
if (File::exists($path) || File::exists($path .= '.less')) { $paths[] = $path; $paths = array_merge($paths, $imports($path)); } } return $paths; }; $compile = function ($input_file, $output_file) use($imports) { try { $latest = File::modified($input_file); foreach ($imports($input_file) as $import) { $import_modified = File::modified($import); $latest = $import_modified > $latest ? $import_modified : $latest; } if (!File::exists($output_file) || $latest > File::modified($output_file)) { $cache = lessc::cexecute($input_file); File::put($output_file, $cache['compiled']); } } catch (Exception $ex) { exit('lessc fatal error:<br />' . $ex->getMessage()); } }; if (isset($config['directories'])) { foreach ($config['directories'] as $less_dir => $css_dir) { $less_dir = rtrim($less_dir, '/') . '/'; foreach (glob($less_dir . '*.[Ll][Ee][Ss][Ss]') as $less) { $css = rtrim($css_dir, '/') . '/' . basename($less, '.less') . '.css'; $compile($less, $css); } } }
public function beforeLayoutRender($observer) { if (!$this->_getConfigHelper()->isEnabled()) { return; } $layout = Mage::getSingleton('core/layout'); if (($head = $layout->getBlock('head')) && $head instanceof Mage_Page_Block_Html_Head) { $baseJsDir = Mage::getBaseDir() . DS . 'js' . DS; $designPackage = Mage::getDesign(); $newItems = $head->getData('items'); $globalVars = $this->_getConfigHelper()->getGlobalVariables(); // Cache by file path $filesCollection = Mage::getModel('less/file')->getCollection()->load(); $filesIds = array_flip($filesCollection->toOptionHash()); foreach ($newItems as $key => $item) { if (in_array($item['type'], array('js_css', 'skin_css'))) { // CSS file if (substr($item['name'], -5) == '.less') { // LESS file if ($item['type'] == 'js_css') { $lessFile = $baseJsDir . $item['name']; } else { $lessFile = $designPackage->getFilename($item['name'], array('_type' => 'skin')); } $baseFile = ltrim(str_replace(Mage::getBaseDir(), '', $lessFile), DS); $cssFile = substr($lessFile, 0, -5) . '.css'; try { // Init file config if (isset($filesIds[$baseFile])) { $isNewModel = false; $model = $filesCollection->getItemById($filesIds[$baseFile]); $oldCache = $model->getCache(); $forceRebuild = (bool) $model->getForceRebuild(); $customVars = $model->getCustomVariables(); $useGlobalVars = (bool) $model->getUseGlobalVariables(); $forceGlobalVars = (bool) $model->getForceGlobalVariables(); } else { $isNewModel = true; $model = null; $oldCache = null; $forceRebuild = false; $customVars = array(); $useGlobalVars = true; $forceGlobalVars = false; } // Get all needed variables for current file if (is_array($customVars)) { $oldVars = $customVars; $customVars = array(); foreach ($oldVars as $oldVar) { $customVars[$oldVar['code']] = $oldVar['value']; } } else { $customVars = array(); } if ($useGlobalVars) { $variables = array_merge($forceGlobalVars ? $customVars : $globalVars, $forceGlobalVars ? $globalVars : $customVars); } else { $variables = $customVars; } $variables = array_merge($variables, $this->_getLessVariables($item['name'])); // Compile if needed (depends on cache and rebuild flag) $oldCache = is_array($oldCache) ? $oldCache : $lessFile; try { $newCache = lessc::cexecute($oldCache, $forceRebuild, $variables, $this->_getLessFunctions($item['name'])); } catch (Exception $e) { if ($this->_getConfigHelper()->getShowErrors()) { if (!is_string($result = $this->_checkWritableFile($cssFile))) { file_put_contents($cssFile, "\n/* " . $e->getMessage() . " */\n", FILE_APPEND); } } throw $e; } if (!is_array($oldCache) || $newCache['updated'] > $oldCache['updated']) { if (!is_string($result = $this->_checkWritableFile($cssFile))) { file_put_contents($cssFile, $newCache['compiled']); } else { Mage::throwException($result); } if ($isNewModel) { $model = Mage::getModel('less/file')->setPath($baseFile); } // Won't be further needed and takes most of the place unset($newCache['compiled']); $model->setCache($newCache)->save(); } } catch (Exception $e) { Mage::logException($e); } // Force adding the CSS file instead of Less one $newItems[$key]['name'] = substr($item['name'], 0, -5) . '.css'; } } } // Replace old items with parsed ones $head->setData('items', $newItems); } }