4cfa097802c68fb2f0453cb4050312b209440293
[yaffs-website] / vendor / consolidation / robo / src / Task / Assets / Less.php
1 <?php
2 namespace Robo\Task\Assets;
3
4 use Robo\Result;
5
6 /**
7  * Compiles less files.
8  *
9  * ```php
10  * <?php
11  * $this->taskLess([
12  *     'less/default.less' => 'css/default.css'
13  * ])
14  * ->run();
15  * ?>
16  * ```
17  *
18  * Use one of both less compilers in your project:
19  *
20  * ```
21  * "leafo/lessphp": "~0.5",
22  * "oyejorge/less.php": "~1.5"
23  * ```
24  *
25  * Specify directory (string or array) for less imports lookup:
26  *
27  * ```php
28  * <?php
29  * $this->taskLess([
30  *     'less/default.less' => 'css/default.css'
31  * ])
32  * ->importDir('less')
33  * ->compiler('lessphp')
34  * ->run();
35  * ?>
36  * ```
37  *
38  * You can implement additional compilers by extending this task and adding a
39  * method named after them and overloading the lessCompilers() method to
40  * inject the name there.
41  */
42 class Less extends CssPreprocessor
43 {
44     const FORMAT_NAME = 'less';
45
46     /**
47      * @var string[]
48      */
49     protected $compilers = [
50         'less', // https://github.com/oyejorge/less.php
51         'lessphp', //https://github.com/leafo/lessphp
52     ];
53
54     /**
55      * lessphp compiler
56      * @link https://github.com/leafo/lessphp
57      *
58      * @param string $file
59      *
60      * @return string
61      */
62     protected function lessphp($file)
63     {
64         if (!class_exists('\lessc')) {
65             return Result::errorMissingPackage($this, 'lessc', 'leafo/lessphp');
66         }
67
68         $lessCode = file_get_contents($file);
69
70         $less = new \lessc();
71         if (isset($this->compilerOptions['importDirs'])) {
72             $less->setImportDir($this->compilerOptions['importDirs']);
73         }
74
75         return $less->compile($lessCode);
76     }
77
78     /**
79      * less compiler
80      * @link https://github.com/oyejorge/less.php
81      *
82      * @param string $file
83      *
84      * @return string
85      */
86     protected function less($file)
87     {
88         if (!class_exists('\Less_Parser')) {
89             return Result::errorMissingPackage($this, 'Less_Parser', 'oyejorge/less.php');
90         }
91
92         $lessCode = file_get_contents($file);
93
94         $parser = new \Less_Parser();
95         $parser->SetOptions($this->compilerOptions);
96         if (isset($this->compilerOptions['importDirs'])) {
97             $importDirs = [];
98             foreach ($this->compilerOptions['importDirs'] as $dir) {
99                 $importDirs[$dir] = $dir;
100             }
101             $parser->SetImportDirs($importDirs);
102         }
103
104         $parser->parse($lessCode);
105
106         return $parser->getCss();
107     }
108 }