Esempio n. 1
0
 private function compileLess()
 {
     $inputFile = $this->publicRoot . $this->src;
     $outputFile = $this->publicRoot . $this->generateLessFilename();
     $compiler = new LessCompiler();
     $compiler->compile($inputFile, $outputFile);
 }
Esempio n. 2
0
 public function smash(UrlRewriter $urlRewriter = null, $collection = null)
 {
     if ($collection) {
         $collections = [$this->manager->getCollection($collection)];
     } else {
         $collections = $this->manager->getCollections();
     }
     foreach ($collections as $collection) {
         $compiledStylesFile = $this->outputDir . '/' . $collection->getName() . ".tmp.css";
         $compiledScriptsFile = $this->outputDir . '/' . $collection->getName() . ".tmp.js";
         @unlink($compiledScriptsFile);
         @unlink(@$compiledStylesFile);
         $lessCompiler = new LessCompiler();
         $assetBuckets = $collection->getAssets();
         foreach ($assetBuckets as $bucket => $assets) {
             foreach ($assets as $asset) {
                 if (!$asset->isEmbedded() && !$asset->isRemote()) {
                     $sourcePath = $this->manager->getPublicRoot() . '/' . $asset->getSrc();
                     $appendData = file_get_contents($sourcePath);
                     $appendData .= "\n\n";
                     if ($asset instanceof StyleInterface) {
                         if ($asset->isLess()) {
                             $lessTmpFile = $this->outputDir . '/' . sha1($asset->getSrc() . getmypid()) . '.css';
                             $lessCompiler->compile($sourcePath, $lessTmpFile, true);
                             $appendData = file_get_contents($lessTmpFile) . "\n\n";
                             unlink($lessTmpFile);
                         }
                         file_put_contents($compiledStylesFile, $appendData, FILE_APPEND);
                     } else {
                         file_put_contents($compiledScriptsFile, $appendData, FILE_APPEND);
                     }
                 }
             }
         }
         if (file_exists($compiledStylesFile)) {
             if ($urlRewriter) {
                 $content = file_get_contents($compiledStylesFile);
                 $newContent = $urlRewriter->rewriteCssUrls($content);
                 file_put_contents($compiledStylesFile, $newContent);
             }
             $this->cssMinifier->minify($compiledStylesFile, str_replace('.tmp', '', $compiledStylesFile));
             @unlink($compiledStylesFile);
         }
         if (file_exists($compiledScriptsFile)) {
             $this->javascriptMinifier->minify($compiledScriptsFile, str_replace('.tmp', '', $compiledScriptsFile));
             @unlink($compiledScriptsFile);
         }
     }
 }
Esempio n. 3
0
 /**
  * Initializes the component.
  * @throws CException if initialization fails.
  */
 public function init()
 {
     if (!in_array($this->env, array(self::ENV_DEVELOPMENT, self::ENV_PRODUCTION))) {
         throw new CException('Failed to initialize LESS compiler. Property env must be either "development" or "production".');
     }
     if (isset($this->dumpLineNumbers) && !in_array($this->dumpLineNumbers, array(self::DLN_COMMENTS, self::DLN_MEDIAQUERY, self::DLN_ALL))) {
         throw new CException('Failed to initialize LESS compiler. Property dumpLineNumber must be "comments", "mediaQuery" or "all".');
     }
     parent::init();
 }
 /**
  * Initializes the component.
  * @throws CException if initialization fails.
  */
 public function init()
 {
     parent::init();
     if ($this->compression !== false && !in_array($this->compression, array(self::COMPRESSION_WHITESPACE, self::COMPRESSION_YUI))) {
         throw new CException('Failed to initialize LESS compiler. Property compression must be either "whitespace" or "yui".');
     }
     if ($this->optimizationLevel !== false && !in_array($this->optimizationLevel, array(0, 1, 2))) {
         throw new CException('Failed to initialize LESS compiler. Property optimizationLevel must be 0, 1 or 2.');
     }
 }
 function css($file, $media = null)
 {
     /* Only initiate if webiste is in dev mode or a ?flush is called */
     if (preg_match('/\\.less$/i', $file) || Director::isDev() || isset($_GET['flush'])) {
         /* If file is CSS, check if there is a LESS file */
         if (preg_match('/\\.css$/i', $file)) {
             $less = preg_replace('/\\.css$/i', '.less', $file);
             if (is_file(Director::getAbsFile($less))) {
                 $file = $less;
             }
         }
         /* If less file exists, then check/compile it */
         if (preg_match('/\\.less$/i', $file)) {
             $out = preg_replace('/\\.less$/i', '.css', $file);
             $css_file = Director::getAbsFile($out);
             $options = array();
             /* Automatically compress if in live mode */
             if (Director::isLive()) {
                 $options['compress'] = true;
             }
             try {
                 /* Force recompile & only write to css if updated */
                 if (isset($_GET['flush']) || !Director::isLive()) {
                     /* Force deleting of all cache files on flush */
                     if (file_exists(self::$cacheDir) && isset($_GET['flush']) && !self::$already_flushed) {
                         $paths = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::$cacheDir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
                         foreach ($paths as $path) {
                             $path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
                         }
                         /* make sure we only flush once per request and not for each *.less */
                         self::$already_flushed = true;
                     }
                     /* Set cache directory */
                     $options['cache_dir'] = self::$cacheDir;
                     /* Set cache method */
                     $options['cache_method'] = self::$cacheMethod;
                     /* Calculate the LESS file's parent URL */
                     $css_dir = dirname(Director::baseURL() . $file) . '/';
                     /* Generate and return cached file path */
                     $cached_file = self::$cacheDir . '/' . Less_Cache::Get(array(Director::getAbsFile($file) => $css_dir), $options, self::$variables);
                     /* check cache vs. css and overwrite if necessary */
                     if (!is_file($css_file) || md5_file($css_file) != md5_file($cached_file)) {
                         copy($cached_file, $css_file);
                     }
                 }
             } catch (Exception $ex) {
                 trigger_error('Less.php fatal error: ' . $ex->getMessage(), E_USER_ERROR);
             }
             $file = $out;
         }
     }
     /* Return css file path */
     return parent::css($file, $media);
 }
Esempio n. 6
0
 public static function ipBeforeController()
 {
     $configModel = ConfigModel::instance();
     if ($configModel->isInPreviewState()) {
         ipAddJsVariable('ipRepositoryUrl', ipFileUrl('file/repository/'));
         static::initConfig();
     }
     $lessCompiler = LessCompiler::instance();
     if (ipConfig()->isDevelopmentEnvironment()) {
         if ($lessCompiler->shouldRebuild(ipConfig()->theme())) {
             $lessCompiler->rebuild(ipConfig()->theme());
         }
     }
 }
<?php

Requirements::set_backend(new LessCompiler());
/* Add default ThemeDir variable */
LessCompiler::addVariable('ThemeDir', '"' . Director::baseURL() . SSViewer::get_theme_folder() . '"');
/* Set default cache directory */
LessCompiler::setCacheDir(TEMP_FOLDER . '/less-cache');
/* Set default cache method */
LessCompiler::setCacheMethod('serialize');
Esempio n. 8
0
<?php

Requirements::set_backend(new LessCompiler());
/* Add default ThemeDir variable */
LessCompiler::addVariable('ThemeDir', '"' . Director::baseURL() . SSViewer::get_theme_folder() . '"');
Esempio n. 9
0
 /**
  * Compile the less files
  *
  * @param  string $inputFile
  * @param  string $outputFile
  * @param  string $lessFolder
  *non
  * @return boolean
  */
 protected function _autoCompileLess($inputFile, $outputFile, $lessFolder)
 {
     $cacheFile = str_replace(DS, '_', str_replace(APP, null, $outputFile));
     $cacheFile = $this->_cacheFolder . DS . $cacheFile;
     $cacheFile = substr_replace($cacheFile, 'cache', -3);
     $cache = file_exists($cacheFile) ? unserialize(file_get_contents($cacheFile)) : $inputFile;
     $lessCompiler = new LessCompiler();
     $lessCompiler->setSourceMap($this->settings['sourceMap']);
     $lessCompiler->setFormatter($this->settings['formatter']);
     if (is_bool($this->settings['preserveComments'])) {
         $lessCompiler->setPreserveComments($this->settings['preserveComments']);
     }
     if ($this->settings['variables']) {
         $lessCompiler->setVariables($this->settings['variables']);
     }
     $lessCompiler->registerFunction('arie', function ($var) {
         return $var;
     });
     $newCache = $lessCompiler->cachedCompile($cache, $this->settings['forceCompiling']);
     if (true === $this->settings['forceCompiling'] || !is_array($cache) || $newCache["updated"] > $cache["updated"]) {
         file_put_contents($cacheFile, serialize($newCache));
         file_put_contents($outputFile, $newCache['compiled']);
         return true;
     }
     return false;
 }
Esempio n. 10
0
require_once DIR_ROOT . '/php/Libs/Core/View.php';
use Core\ResourceCompiler;
class LessCompiler extends ResourceCompiler
{
    public static function url($arg)
    {
        return "url('" . URL_ROOT . $arg[1] . "')";
    }
    protected static function render_file($path)
    {
        $cache_path = $path . '-cache.css';
        if (!file_exists($cache_path) || filemtime($cache_path) < filemtime($path)) {
            $out = parent::render_file($path);
            $less = new lessc();
            $less->importDir = dirname($path);
            //$less->registerFunction('url', array('LessCompiler', 'url'));
            $css = $less->parse($out);
            $css = str_replace('url("/', 'url("' . URL_ROOT . '/', $css);
            file_put_contents($cache_path, $css);
        }
        return file_get_contents($cache_path);
    }
}
LessCompiler::handle($_GET['URL'], 'text/css');
/*
try {
	$less = new lessc(DIR_CURRENT . '/' . basename($_GET['URL']));
	print $less->parse();
} catch (exception $ex) {
	exit('lessc fatal error:<br />' . $ex->getMessage());
}*/