1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Robo\Task\Assets; |
4
|
|
|
|
5
|
|
|
use Robo\Result; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Compiles less files. |
9
|
|
|
* |
10
|
|
|
* ```php |
11
|
|
|
* <?php |
12
|
|
|
* $this->taskLess([ |
13
|
|
|
* 'less/default.less' => 'css/default.css' |
14
|
|
|
* ]) |
15
|
|
|
* ->run(); |
16
|
|
|
* ?> |
17
|
|
|
* ``` |
18
|
|
|
* |
19
|
|
|
* Use one of both less compilers in your project: |
20
|
|
|
* |
21
|
|
|
* ``` |
22
|
|
|
* "leafo/lessphp": "~0.5", |
23
|
|
|
* "oyejorge/less.php": "~1.5" |
24
|
|
|
* ``` |
25
|
|
|
* |
26
|
|
|
* Specify directory (string or array) for less imports lookup: |
27
|
|
|
* |
28
|
|
|
* ```php |
29
|
|
|
* <?php |
30
|
|
|
* $this->taskLess([ |
31
|
|
|
* 'less/default.less' => 'css/default.css' |
32
|
|
|
* ]) |
33
|
|
|
* ->importDir('less') |
34
|
|
|
* ->compiler('lessphp') |
35
|
|
|
* ->run(); |
36
|
|
|
* ?> |
37
|
|
|
* ``` |
38
|
|
|
* |
39
|
|
|
* You can implement additional compilers by extending this task and adding a |
40
|
|
|
* method named after them and overloading the lessCompilers() method to |
41
|
|
|
* inject the name there. |
42
|
|
|
*/ |
43
|
|
|
class Less extends CssPreprocessor |
44
|
|
|
{ |
45
|
|
|
const FORMAT_NAME = 'less'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string[] |
49
|
|
|
*/ |
50
|
|
|
protected $compilers = [ |
51
|
|
|
'less', // https://github.com/oyejorge/less.php |
52
|
|
|
'lessphp', //https://github.com/leafo/lessphp |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* lessphp compiler |
57
|
|
|
* @link https://github.com/leafo/lessphp |
58
|
|
|
* |
59
|
|
|
* @param string $file |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
protected function lessphp($file) |
64
|
|
|
{ |
65
|
|
|
if (!class_exists('\lessc')) { |
66
|
|
|
return Result::errorMissingPackage($this, 'lessc', 'leafo/lessphp'); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$lessCode = file_get_contents($file); |
70
|
|
|
|
71
|
|
|
$less = new \lessc(); |
72
|
|
|
if (isset($this->compilerOptions['importDirs'])) { |
73
|
|
|
$less->setImportDir($this->compilerOptions['importDirs']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $less->compile($lessCode); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* less compiler |
81
|
|
|
* @link https://github.com/oyejorge/less.php |
82
|
|
|
* |
83
|
|
|
* @param string $file |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function less($file) |
88
|
|
|
{ |
89
|
|
|
if (!class_exists('\Less_Parser')) { |
90
|
|
|
return Result::errorMissingPackage($this, 'Less_Parser', 'oyejorge/less.php'); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$lessCode = file_get_contents($file); |
94
|
|
|
|
95
|
|
|
$parser = new \Less_Parser(); |
96
|
|
|
$parser->SetOptions($this->compilerOptions); |
97
|
|
|
if (isset($this->compilerOptions['importDirs'])) { |
98
|
|
|
$importDirs = []; |
99
|
|
|
foreach ($this->compilerOptions['importDirs'] as $dir) { |
100
|
|
|
$importDirs[$dir] = $dir; |
101
|
|
|
} |
102
|
|
|
$parser->SetImportDirs($importDirs); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$parser->parse($lessCode); |
106
|
|
|
|
107
|
|
|
return $parser->getCss(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.