|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This source file is subject to the Academic Free License (AFL 3.0) |
| 4 | + * that is bundled with this package in the file LICENSE_AFL.txt. |
| 5 | + * It is also available through the world-wide-web at this URL: |
| 6 | + * http://opensource.org/licenses/afl-3.0.php |
| 7 | + * |
| 8 | + * @category Magemaven |
| 9 | + * @package Magemaven_Lesscss |
| 10 | + * @copyright Copyright (c) 2012 Sergey Storchay <[email protected]> |
| 11 | + * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) |
| 12 | + */ |
| 13 | +require_once(Mage::getBaseDir('lib') . DS . 'lessphp' . DS .'lessc.inc.php'); |
| 14 | + |
| 15 | +class Magemaven_Lesscss_Helper_Data extends Mage_Core_Helper_Abstract |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Get file extension in lower case |
| 19 | + * |
| 20 | + * @param $file |
| 21 | + * @return string |
| 22 | + */ |
| 23 | + public function getFileExtension($file) |
| 24 | + { |
| 25 | + return strtolower(pathinfo($file, PATHINFO_EXTENSION)); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Compile less file and return full path to created css |
| 30 | + * |
| 31 | + * @param $file |
| 32 | + * @return string |
| 33 | + */ |
| 34 | + public function compile($file) |
| 35 | + { |
| 36 | + if (!$file) { |
| 37 | + return ''; |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + $targetFilename = Mage::getBaseDir('media') |
| 42 | + . DS . 'lesscss' . DS . md5($file) . '.css'; |
| 43 | + $cacheKey = 'less_' . $file; |
| 44 | + |
| 45 | + /** @var $cacheModel Mage_Core_Model_Cache */ |
| 46 | + $cacheModel = $cache = Mage::getSingleton('core/cache'); |
| 47 | + $cache = $cacheModel->load($cacheKey); |
| 48 | + if ($cache) { |
| 49 | + $cache = @unserialize($cache); |
| 50 | + } |
| 51 | + |
| 52 | + if (!file_exists($targetFilename)) { |
| 53 | + $cache = false; |
| 54 | + } |
| 55 | + |
| 56 | + $lastUpdated = (isset($cache['updated'])) ? $cache['updated'] : 0; |
| 57 | + $cache = lessc::cexecute(($cache) ? $cache : $file); |
| 58 | + |
| 59 | + if ($cache['updated'] > $lastUpdated) { |
| 60 | + if (!file_exists(dirname($targetFilename))) { |
| 61 | + mkdir(dirname($targetFilename), 0777, true); |
| 62 | + } |
| 63 | + |
| 64 | + file_put_contents($targetFilename, $cache['compiled']); |
| 65 | + $cacheModel->save(serialize($cache), $cacheKey); |
| 66 | + } |
| 67 | + |
| 68 | + } catch (Exception $e) { |
| 69 | + Mage::logException($e); |
| 70 | + $targetFilename = ''; |
| 71 | + } |
| 72 | + |
| 73 | + return $targetFilename; |
| 74 | + } |
| 75 | +} |
0 commit comments