Esempio n. 1
0
 /**
  * @return \Less_Parser
  */
 protected function _initCompiler()
 {
     if ($this->_compiler) {
         return $this->_compiler;
     }
     $options = array('compress' => false, 'strictUnits' => false, 'strictMath' => false, 'relativeUrls' => true, 'cache_method' => false, 'sourceMap' => false, 'indentation' => '    ');
     if ($this->_isDebug()) {
         $options['sourceMap'] = true;
         $options['sourceMapRootpath'] = $this->_options->get('root_path');
         $options['sourceMapBasepath'] = $this->_options->get('root_path');
     }
     // Create compilier
     $this->_compiler = new \Less_Parser($options);
     $this->_compiler->Reset();
     // Global depends
     $mixins = $this->_options->get('autoload');
     foreach ($mixins as $mixin) {
         $this->_compiler->parseFile($mixin);
     }
     // Add custom vars
     $this->_compiler->ModifyVars((array) $this->_options->get('global_vars', []));
     // Set paths
     $importPaths = (array) $this->_options->get('import_paths', []);
     foreach ($importPaths as $fullPath => $relPath) {
         $this->setImportPath($fullPath, $relPath);
     }
     // Set cutsom functions
     $functions = (array) $this->_options->get('functions', [], 'arr');
     foreach ($functions as $name => $function) {
         $this->_compiler->registerFunction($name, $function);
     }
     return $this->_compiler;
 }
Esempio n. 2
0
 public function compileFile($fname, $outFname = null)
 {
     if (!is_readable($fname)) {
         throw new Exception('load error: failed to find ' . $fname);
     }
     $pi = pathinfo($fname);
     $oldImport = $this->importDir;
     $this->importDir = (array) $this->importDir;
     $this->importDir[] = realpath($pi['dirname']) . '/';
     $this->allParsedFiles = array();
     $this->addParsedFile($fname);
     $parser = new Less_Parser(array('sourceMap' => $this->sourceMap));
     $parser->SetImportDirs($this->getImportDirs());
     if (count($this->registeredVars)) {
         $parser->ModifyVars($this->registeredVars);
     }
     foreach ($this->libFunctions as $name => $func) {
         $parser->registerFunction($name, $func);
     }
     $parser->parseFile($fname);
     $out = $parser->getCss();
     $parsed = Less_Parser::AllParsedFiles();
     foreach ($parsed as $file) {
         $this->addParsedFile($file);
     }
     $this->importDir = $oldImport;
     if ($outFname !== null) {
         return file_put_contents($outFname, $out);
     }
     return $out;
 }
Esempio n. 3
0
 /**
  * Test
  */
 public function testFunction()
 {
     echo "\nBegin Tests";
     $less_file = $this->fixtures_dir . '/functions/less/f1.less';
     $expected_css = file_get_contents($this->fixtures_dir . '/functions/css/f1.css');
     $parser = new Less_Parser();
     $parser->registerFunction('myfunc-reverse', array(__CLASS__, 'reverse'));
     $parser->parseFile($less_file);
     $generated_css = $parser->getCss();
     $this->assertEquals($expected_css, $generated_css);
 }
Esempio n. 4
0
 /**
  * {@inheritDoc}
  */
 public function filterLoad(AssetInterface $asset)
 {
     $filePath = $asset->getSourceRoot() . '/' . $asset->getSourcePath();
     $parser = new \Less_Parser($this->options);
     foreach ($this->customFunctions as $name => $callable) {
         $parser->registerFunction($name, $callable);
     }
     $baseUri = $this->basePath;
     if ($this->relativeUrlPaths) {
         $baseUri = $baseUri . '/' . $asset->getSourcePath();
     }
     $parser = $parser->parseFile($filePath, $baseUri);
     $parser->ModifyVars($this->lessVariables);
     $asset->setContent($parser->getCss());
 }