Exemplo n.º 1
0
 /**
  * Adds a chunk to the stack
  *
  * @param string $chunk
  * @param string $fileInfo
  * @param integer $index
  * @param mixed $mapLines
  * @return StandardOutput
  */
 public function add($chunk, FileInfo $fileInfo = null, $index = 0, $mapLines = null)
 {
     // nothing to do
     if ($chunk == '') {
         return $this;
     }
     $lines = explode("\n", $chunk);
     $columns = end($lines);
     if ($fileInfo) {
         $inputSource = substr($this->contentsMap[$fileInfo->importedFile->getPath()], 0, $index);
         $sourceLines = explode("\n", $inputSource);
         $sourceColumns = end($sourceLines);
         $sourceLinesCount = count($sourceLines);
         $sourceColumnsLength = strlen($sourceColumns);
         if (!$mapLines) {
             $this->generator->addMapping($this->lineNumber + 1, $this->column, $sourceLinesCount, $sourceColumnsLength, $fileInfo->filename);
         } else {
             for ($i = 0, $count = count($lines); $i < $count; $i++) {
                 $this->generator->addMapping($this->lineNumber + $i + 1, $i === 0 ? $this->column : 0, $sourceLinesCount + $i, $i === 0 ? $sourceColumnsLength : 0, $fileInfo->filename);
             }
         }
     }
     if (count($lines) === 1) {
         $this->column += strlen($columns);
     } else {
         $this->lineNumber += count($lines) - 1;
         $this->column = strlen($columns);
     }
     // add only chunk
     return parent::add($chunk);
 }
 private function fixSourceMap(Generator $sourceMap, $outputFile)
 {
     $writeTo = $sourceMap->getOption('write_to');
     if (!$writeTo) {
         // this is inline map
         return;
     }
     $map = $outputFile . '.map';
     // overwrite the map
     if (is_readable($map)) {
         // autoprefixer puts unreferenced source, so we will "remove" it
         $mapContent = file_get_contents($map);
         $mapContent = json_decode($mapContent, true);
         $file = basename($outputFile);
         foreach ($mapContent['sources'] as $index => $source) {
             if ($source == $file) {
                 $mapContent['sources'][$index] = 'zzzzzoo_ignore_this';
                 $mapContent['sourcesContent'][$index] = '/*autoprefixer adds unreferenced source, just try to ignore this :)*/';
                 break;
             }
         }
         $mapContent = json_encode($mapContent);
         file_put_contents($writeTo, $mapContent);
         unlink($map);
     }
 }
Exemplo n.º 3
0
 public function generateJson()
 {
     return parent::generateJson();
 }
Exemplo n.º 4
0
 /**
  * Converts the ruleset to CSS.
  *
  * @param RulesetNode $ruleset
  * @param array $variables
  *
  * @return string The generated CSS code
  *
  * @throws
  */
 protected function toCSS(RulesetNode $ruleset, array $variables)
 {
     $precision = ini_set('precision', 16);
     $locale = setlocale(LC_NUMERIC, 0);
     setlocale(LC_NUMERIC, 'C');
     if (extension_loaded('xdebug')) {
         $level = ini_set('xdebug.max_nesting_level', PHP_INT_MAX);
     }
     $e = $css = null;
     try {
         $this->prepareVariables($this->context, $variables);
         // pre compilation visitors
         foreach ($this->getPreCompileVisitors() as $visitor) {
             /* @var $visitor Visitor */
             $visitor->run($ruleset);
         }
         // compile the ruleset
         $compiled = $ruleset->compile($this->context);
         // post compilation visitors
         foreach ($this->getPostCompileVisitors() as $visitor) {
             /* @var $visitor Visitor */
             $visitor->run($compiled);
         }
         $context = $this->getContext();
         $context->numPrecision = 8;
         // less.js compatibility
         if ($context->sourceMap) {
             $generator = new Generator($compiled, $this->context->getContentsMap(), $this->context->sourceMapOptions);
             // will also save file
             $css = $generator->generateCSS($this->context);
         } else {
             $generator = null;
             $css = $compiled->toCSS($this->context);
         }
         if ($this->pluginManager) {
             // post process
             $postProcessors = $this->pluginManager->getPostProcessors();
             foreach ($postProcessors as $postProcessor) {
                 /* @var $postProcessor PostProcessorInterface */
                 $css = $postProcessor->process($css, ['context' => $this->context, 'source_map' => $generator, 'importer' => $this->importer]);
             }
         }
         if ($this->context->compress) {
             $css = preg_replace('/(^(\\s)+)|((\\s)+$)/', '', $css);
         }
     } catch (\Exception $e) {
     }
     // restore
     setlocale(LC_NUMERIC, $locale);
     ini_set('precision', $precision);
     if (extension_loaded('xdebug')) {
         ini_set('xdebug.max_nesting_level', $level);
     }
     if ($e) {
         throw $e;
     }
     return $css;
 }