/** * Converts the ruleset to CSS. Applies the output filters to the output. * * @param ILess_Node_Ruleset $ruleset * @param array $variables * @return string The generated CSS code */ protected function toCSS(ILess_Node_Ruleset $ruleset, array $variables) { // the cache key consists of: // 1) parsed rules // 2) assigned variables via the API // 3) environment options $cacheKey = $this->generateCacheKey(serialize($this->rules) . serialize($variables) . serialize(array($this->env->compress, $this->env->sourceMap, $this->env->sourceMapOptions, $this->env->relativeUrls, $this->env->precision, $this->env->debug, $this->env->dumpLineNumbers, $this->env->canShortenColors, $this->env->ieCompat, $this->env->strictMath, $this->env->strictUnits))); $rebuild = true; if ($this->cache->has($cacheKey)) { $rebuild = false; list($css, $importedFiles) = $this->cache->get($cacheKey); // we need to check if the file has been modified foreach ($importedFiles as $importedFileArray) { list($lastModifiedBefore, $path, $currentFileInfo) = $importedFileArray; $lastModified = $this->importer->getLastModified($path, $currentFileInfo); if ($lastModifiedBefore != $lastModified) { $rebuild = true; // no need to continue, we will rebuild the CSS break; } } } if ($rebuild) { $css = parent::toCSS($ruleset, $variables); // what have been imported? $importedFiles = array(); foreach ($this->importer->getImportedFiles() as $importedFile) { // we need to save original path, last modified timestamp and currentFileInfo object // see ILess_Importer::setImportedFile() $importedFiles[] = array($importedFile[0]->getLastModified(), $importedFile[1], $importedFile[2]); } $this->cache->set($cacheKey, array($css, $importedFiles)); } return $this->filter($css); }
/** * Imports the file * * @param string $path The path to import. Path will be searched by the importers * @param ILess_FileInfo $currentFileInfo Current file information * @param array $importOptions Import options * @param integer $index Current index * @return array * @throws ILess_Exception_Import If the $path could not be imported */ public function import($path, ILess_FileInfo $currentFileInfo, array $importOptions = array(), $index = 0) { $cacheKey = $this->generateCacheKey($currentFileInfo->currentDirectory . $path); // do we have a file in the cache? if ($this->cache->has($cacheKey)) { // check the modified timestamp $file = $this->cache->get($cacheKey); // search foreach ($this->importers as $importer) { /* @var $file ILess_ImportedFile */ $lastModified = $importer->getLastModified($path, $currentFileInfo); if ($lastModified !== false && $lastModified == $file->getLastModified()) { // the modification time is the same, take the one from cache return $this->doImport($file, $path, $currentFileInfo, $importOptions, true); } } } foreach ($this->importers as $importer) { /* @var $importer ILess_ImporterInterface */ $file = $importer->import($path, $currentFileInfo); // import is handled by the importer if ($file instanceof ILess_ImportedFile) { $result = $this->doImport($file, $path, $currentFileInfo, $importOptions); /* @var $file ILess_ImportedFile */ list(, $file) = $result; // save the cache $this->cache->set($cacheKey, $file); return $result; } } throw new ILess_Exception_Import(sprintf("'%s' wasn't found.", $path), $index, $currentFileInfo); }