示例#1
0
    [updated_at] TIMESTAMP
  )', 'CREATE UNIQUE INDEX [filename_idx] ON [less] ([filename])'];
foreach ($statements as $statement) {
    if (!$pdo->query($statement)) {
        $error = $pdo->errorInfo();
        throw new Exception($error[2], $error[1]);
    }
}
$stmt = $pdo->prepare('INSERT INTO less(filename, data, updated_at) VALUES(?, ?, ?)');
foreach ([['foo.less', 'body { background: @color; }', time()], ['mixins.less', '.mixin(@a) { background: @a; }', time()]] as $line) {
    $result = $stmt->execute([$line[0], $line[1], $line[2]]);
}
try {
    $cacheDir = dirname(__FILE__) . '/cache';
    $parser = new Parser();
    $parser->getImporter()->registerImporter(new DatabaseImporter($pdo, ['table_name' => 'less', 'filename_column' => 'filename', 'data_column' => 'data', 'updated_at_column' => 'data']));
    $parser->parseString('

  @color: red;

  @import url("foo.less");
  @import (reference) url("mixins.less");

  #head {
    color: @color + #fff;
    .mixin(yellow);
  }

  ');
    $cssContent = $parser->getCSS();
    file_put_contents($cacheDir . '/database.css', $cssContent);
示例#2
0
<?php

use ILess\Parser;
use ILess\Cache\FileSystemCache;
require_once '_bootstrap.php';
// create the parser
$parser = new Parser(array(), new FileSystemCache(array('cache_dir' => dirname(__FILE__) . '/cache')));
$file = dirname(__FILE__) . '/less/test.less';
// create your cache key
$cacheKey = md5($file);
$importer = $parser->getImporter();
$cache = $parser->getCache();
$rebuild = true;
$cssLastModified = -1;
if ($cache->has($cacheKey)) {
    $rebuild = false;
    list($css, $importedFiles) = $cache->get($cacheKey);
    // we need to check if the file has been modified
    foreach ($importedFiles as $importedFileArray) {
        list($lastModifiedBefore, $path, $currentFileInfo) = $importedFileArray;
        $lastModified = $importer->getLastModified($path, $currentFileInfo);
        $cssLastModified = max($lastModified, $cssLastModified);
        if ($lastModifiedBefore != $lastModified) {
            $rebuild = true;
            // no need to continue, we will rebuild the CSS
            break;
        }
    }
}
if ($rebuild) {
    $parser->parseFile($file);
 /**
  * @inheritdoc
  */
 public function install(Parser $parser)
 {
     $parser->getImporter()->registerImporter($this->importer);
 }