Exemple #1
0
 /**
  * @covers getImporters
  */
 public function testGetImporters()
 {
     $env = new Context();
     $importer = new FileSystemImporter();
     $i = new Importer($env, ['disc' => $importer], new NoCache());
     $this->assertEquals(['disc' => $importer], $i->getImporters());
 }
Exemple #2
0
 private function processImportNode(ImportNode $node, Context $context, $importParent)
 {
     $e = null;
     try {
         $compiledNode = $node->compileForImport($context);
     } catch (Exception $e) {
         $compiledNode = false;
         if (!$e->getCurrentFile()) {
             if ($node->currentFileInfo) {
                 $e->setCurrentFile($node->currentFileInfo, $node->index);
             } else {
                 $e->setIndex($node->index);
             }
         }
         $node->css = true;
         $node->error = $e;
     }
     $inlineCSS = $node->getOption('inline');
     $isPlugin = $node->getOption('plugin');
     if ($compiledNode && (!$compiledNode->css || $inlineCSS)) {
         if ($node->getOption('multiple')) {
             $context->importMultiple = true;
         }
         for ($i = 0; $i < count($importParent->rules); ++$i) {
             if ($importParent->rules[$i] === $node) {
                 $importParent->rules[$i] = $compiledNode;
                 break;
             }
         }
         $tryAppendLessExtension = !$compiledNode->css;
         try {
             // import the file
             list($alreadyImported, $file) = $this->importer->import($compiledNode->getPath(), $tryAppendLessExtension, $compiledNode->currentFileInfo, $compiledNode->options, $compiledNode->index);
             /* @var $file ImportedFile */
             if (!$context->importMultiple) {
                 if ($alreadyImported) {
                     $compiledNode->skip = true;
                 }
             }
             if ($root = $file->getRuleset()) {
                 /* @var $root RulesetNode */
                 $compiledNode->root = $root;
                 $compiledNode->importedFilename = $file->getPath();
                 if (!$inlineCSS && !$isPlugin && ($context->importMultiple || !$alreadyImported)) {
                     $oldEnv = $this->context;
                     $this->context = $context;
                     try {
                         $this->visit($root);
                     } catch (Exception $e) {
                         $this->error = $e;
                     }
                     $this->end = $oldEnv;
                 }
             }
         } catch (ImportException $e) {
             // optional import
             if (isset($compiledNode->options['optional']) && $compiledNode->options['optional']) {
                 // optional import
             } else {
                 $this->error = $e;
             }
         } catch (Exception $e) {
             $this->error = $e;
         }
     }
     --$this->importCount;
     if ($this->isFinished) {
         $this->sequencer->tryRun();
     }
 }
Exemple #3
0
 /**
  * Parses a string.
  *
  * @param string $string The string to parse
  * @param string $filename The filename for reference (will be visible in the source map) or path to a fake file which directory will be used for imports
  * @param bool $returnRuleset Return the ruleset?
  *
  * @return $this
  */
 public function parseString($string, $filename = '__string_to_parse__', $returnRuleset = false)
 {
     $string = Util::normalizeString((string) $string);
     // we need unique key
     $key = sprintf('%s[__%s__]', $filename, md5($string));
     // create a dummy information, since we are not parsing a real file,
     // but a string coming from outside
     $this->context->setCurrentFile($filename);
     $importedFile = new ImportedFile($key, $string, time());
     // save information, so the exceptions can handle errors in the string
     // and source map is generated for the string
     $this->context->currentFileInfo->importedFile = $importedFile;
     $this->importer->setImportedFile($key, $importedFile, $key, $this->context->currentFileInfo);
     if ($this->context->sourceMap) {
         $this->context->setFileContent($key, $string);
     }
     $importedFile->setRuleset($ruleset = new RulesetNode([], $this->parse($string)));
     if ($returnRuleset) {
         return $ruleset;
     }
     $this->rules = array_merge($this->rules, $ruleset->rules);
     return $this;
 }