Hawk - PHP documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • Hawk
    • View
      • Plugins

Classes

  • Hawk\App
  • Hawk\ButtonInput
  • Hawk\Cache
  • Hawk\CheckboxInput
  • Hawk\ColorInput
  • Hawk\Conf
  • Hawk\Controller
  • Hawk\Crypto
  • Hawk\DatabaseSessionHandler
  • Hawk\DatetimeInput
  • Hawk\DB
  • Hawk\DBExample
  • Hawk\DeleteInput
  • Hawk\Dialogbox
  • Hawk\EmailInput
  • Hawk\ErrorHandler
  • Hawk\Event
  • Hawk\FileInput
  • Hawk\FileSystem
  • Hawk\FloatInput
  • Hawk\Form
  • Hawk\FormFieldset
  • Hawk\FormInput
  • Hawk\GenericModel
  • Hawk\GifImage
  • Hawk\HawkApi
  • Hawk\HawkUpdater
  • Hawk\HiddenInput
  • Hawk\HtmlInput
  • Hawk\HTTPRequest
  • Hawk\Icon
  • Hawk\Image
  • Hawk\IntegerInput
  • Hawk\ItemList
  • Hawk\ItemListField
  • Hawk\JpegImage
  • Hawk\Lang
  • Hawk\Language
  • Hawk\LeftSidebarTab
  • Hawk\Less
  • Hawk\Logger
  • Hawk\Mail
  • Hawk\MenuItem
  • Hawk\Model
  • Hawk\NoSidebarTab
  • Hawk\NumberInput
  • Hawk\ObjectInput
  • Hawk\Option
  • Hawk\Panel
  • Hawk\PasswordInput
  • Hawk\Permission
  • Hawk\Plugin
  • Hawk\PluginInstaller
  • Hawk\PngImage
  • Hawk\ProfileQuestion
  • Hawk\ProfileQuestionValue
  • Hawk\RadioInput
  • Hawk\Request
  • Hawk\Response
  • Hawk\RightSidebarTab
  • Hawk\Role
  • Hawk\RolePermission
  • Hawk\Route
  • Hawk\Router
  • Hawk\SelectInput
  • Hawk\Session
  • Hawk\Singleton
  • Hawk\SubmitInput
  • Hawk\Tabs
  • Hawk\TextareaInput
  • Hawk\TextInput
  • Hawk\Theme
  • Hawk\TimeInput
  • Hawk\Upload
  • Hawk\User
  • Hawk\View
  • Hawk\View\Plugins\Accordion
  • Hawk\View\Plugins\Button
  • Hawk\View\Plugins\Form
  • Hawk\View\Plugins\Icon
  • Hawk\View\Plugins\Import
  • Hawk\View\Plugins\Panel
  • Hawk\View\Plugins\Tabs
  • Hawk\View\Plugins\Text
  • Hawk\View\Plugins\Uri
  • Hawk\View\Plugins\Widget
  • Hawk\ViewPlugin
  • Hawk\Widget
  • Hawk\WysiwygInput

Traits

  • Hawk\Utils

Exceptions

  • Hawk\AppStopException
  • Hawk\DBExampleException
  • Hawk\DBException
  • Hawk\FileSystemException
  • Hawk\HawkApiException
  • Hawk\ImageException
  • Hawk\MailException
  • Hawk\UploadException
  • Hawk\ViewException
  1 <?php
  2 /**
  3  * Less.php
  4  *
  5  * @author  Elvyrra
  6  * @license http://rem.mit-license.org/ MIT
  7  */
  8 
  9 namespace Hawk;
 10 
 11 /**
 12  * This class use the library lessc to compile less files into CSS, using cache system to build only if necessary
 13  *
 14  * @package Utils
 15  */
 16 class Less{
 17     const CACHE_DIR = 'less/';
 18 
 19     /**
 20      * The source file
 21      */
 22     private $source;
 23 
 24     /**
 25      * Constructor
 26      *
 27      * @param string $source The Less source filename
 28      */
 29     public function __construct($source){
 30         if(!is_file($source)) {
 31             throw new \Exception('Impossible to compile the file ' . $source . ' : No such file');
 32         }
 33 
 34         $this->source = $source;
 35     }
 36 
 37     /**
 38      * Get the filename containing the information about the last compilation
 39      *
 40      * @return The filename path
 41      */
 42     private function getLastCompilationInfoFilename(){
 43         return 'less/' . str_replace(array(ROOT_DIR, '/'), array('', '-'), realpath($this->source));
 44     }
 45 
 46     /**
 47      * Save the result of the last compilation
 48      *
 49      * @param array $info The information array
 50      */
 51     private function saveLastCompilationInfo($info){
 52         App::cache()->save($this->getLastCompilationInfoFilename(), '<?php return ' . var_export($info, true) . ';');
 53     }
 54 
 55     /**
 56      * Build the Less file
 57      *
 58      * @param string $dest      The destination CSS file
 59      * @param bool   $force     If set to true, will build whereas the cache status
 60      * @param array  $variables Less variables to set before compiling the Less file
 61      */
 62     public function build($dest, $force = false, $variables = array()){
 63         if(!is_dir(dirname($dest))) {
 64             mkdir(dirname($dest), 0755, true);
 65         }
 66 
 67         $compiler = new \lessc;
 68 
 69 
 70         $lastCompilationFile = App::cache()->getCacheFilePath($this->getLastCompilationInfoFilename());
 71         if(!$force && is_file($lastCompilationFile)) {
 72             $cache = include $lastCompilationFile;
 73         }
 74         else{
 75             $cache = $this->source;
 76         }
 77 
 78         $compiler->setFormatter('compressed');
 79         $compiler->setPreserveComments(false);
 80         $compilation = $compiler->cachedCompile($cache, $force);
 81 
 82         if(!is_array($cache) || $compilation['updated'] > $cache['updated']) {
 83             file_put_contents($dest, '/*** ' . date('Y-m-d H:i:s') . ' ***/' . PHP_EOL . $compilation['compiled']);
 84 
 85             $event = new Event('built-less', array('source' => $this->source, 'dest' => $dest));
 86             $event->trigger();
 87 
 88             // Save the compilation information
 89             unset($compilation['compiled']);
 90             $this->saveLastCompilationInfo($compilation);
 91         }
 92     }
 93 
 94 
 95     /**
 96      * Build a source Less file to a Css destination file
 97      *
 98      * @param string $source    The Less source filename
 99      * @param string $dest      The destination CSS file
100      * @param bool   $force     If set to true, will build whereas the cache status
101      * @param array  $variables Less variables to set before compiling the Less file
102      */
103     public static function compile($source, $dest, $force = false, $variables = array()){
104         $less = new self($source);
105 
106         $less->build($dest, $force, $variables);
107     }
108 }
Hawk - PHP documentation API documentation generated by ApiGen