/** * Execute lessphp on a .less file or a lessphp cache structure * * The lessphp cache structure contains information about a specific * less file having been parsed. It can be used as a hint for future * calls to determine whether or not a rebuild is required. * * The cache structure contains two important keys that may be used * externally: * * compiled: The final compiled CSS * updated: The time (in seconds) the CSS was last compiled * * The cache structure is a plain-ol' PHP associative array and can * be serialized and unserialized without a hitch. * * @param mixed $in Input * @param bool $force Force rebuild? * @return array lessphp cache structure */ public static function cexecute($in, $force = false) { // assume no root $root = null; if (is_string($in)) { $root = $in; } elseif (is_array($in) and isset($in['root'])) { if ($force or !isset($in['files'])) { // If we are forcing a recompile or if for some reason the // structure does not contain any file information we should // specify the root to trigger a rebuild. $root = $in['root']; } elseif (isset($in['files']) and is_array($in['files'])) { foreach ($in['files'] as $fname => $ftime) { if (!file_exists($fname) or filemtime($fname) > $ftime) { // One of the files we knew about previously has changed // so we should look at our incoming root again. $root = $in['root']; break; } } } } else { // TODO: Throw an exception? We got neither a string nor something // that looks like a compatible lessphp cache structure. return null; } if ($root !== null) { // If we have a root value which means we should rebuild. $less = new lessc($root); $out = array(); $out['root'] = $root; $out['compiled'] = $less->parse(); $out['files'] = $less->allParsedFiles(); $out['updated'] = time(); return $out; } else { // No changes, pass back the structure // we were given initially. return $in; } }
/** * Gets the contents of an asset and if it's a stylesheet it is run through the * URI rewriter to correct any ill-formed directories. * * @param array $symlinks * @param string $document_root * @return string */ public function get($lessphp, $symlinks = array(), $document_root = '') { $failed = PHP_EOL . '/* Basset could not find asset [' . $this->directory . DS . $this->file . '] */' . PHP_EOL; $contents = @file_get_contents($this->directory . DS . $this->file); if (empty($contents)) { return $failed; } if ($this->is('styles')) { $contents = Vendor\URIRewriter::rewrite($contents, dirname($this->directory . DS . $this->file), $document_root, $symlinks); } if ($this->is('less') && $lessphp) { $less = new Vendor\lessc(); $less->importDir = $this->directory; $contents = $less->parse($contents); } return $contents . PHP_EOL; }