1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Robo\Task\Assets; |
4
|
|
|
|
5
|
|
|
use Robo\Result; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Compiles scss files. |
9
|
|
|
* |
10
|
|
|
* ```php |
11
|
|
|
* <?php |
12
|
|
|
* $this->taskScss([ |
13
|
|
|
* 'scss/default.scss' => 'css/default.css' |
14
|
|
|
* ]) |
15
|
|
|
* ->importDir('assets/styles') |
16
|
|
|
* ->run(); |
17
|
|
|
* ?> |
18
|
|
|
* ``` |
19
|
|
|
* |
20
|
|
|
* Use the following scss compiler in your project: |
21
|
|
|
* |
22
|
|
|
* ``` |
23
|
|
|
* "scssphp/scssphp ": "~1.0.0", |
24
|
|
|
* ``` |
25
|
|
|
* |
26
|
|
|
* You can implement additional compilers by extending this task and adding a |
27
|
|
|
* method named after them and overloading the scssCompilers() method to |
28
|
|
|
* inject the name there. |
29
|
|
|
*/ |
30
|
|
|
class Scss extends CssPreprocessor |
31
|
|
|
{ |
32
|
|
|
const FORMAT_NAME = 'scss'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
protected $compilers = [ |
38
|
|
|
'scssphp', // https://github.com/scssphp/scssphp |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* scssphp compiler |
43
|
|
|
* @link https://github.com/scssphp/scssphp |
44
|
|
|
* |
45
|
|
|
* @param string $file |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
protected function scssphp($file) |
50
|
|
|
{ |
51
|
|
|
if (!class_exists('\ScssPhp\ScssPhp\Compiler')) { |
52
|
|
|
return Result::errorMissingPackage($this, 'scssphp', 'scssphp/scssphp'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$scssCode = file_get_contents($file); |
56
|
|
|
$scss = new \ScssPhp\ScssPhp\Compiler(); |
57
|
|
|
|
58
|
|
|
// set options for the scssphp compiler |
59
|
|
|
if (isset($this->compilerOptions['importDirs'])) { |
60
|
|
|
$scss->setImportPaths($this->compilerOptions['importDirs']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (isset($this->compilerOptions['formatter'])) { |
64
|
|
|
$scss->setFormatter($this->compilerOptions['formatter']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $scss->compile($scssCode); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sets the formatter for scssphp |
72
|
|
|
* |
73
|
|
|
* The method setFormatter($formatterName) sets the current formatter to $formatterName, |
74
|
|
|
* the name of a class as a string that implements the formatting interface. See the source |
75
|
|
|
* for ScssPhp\ScssPhp\Formatter\Expanded for an example. |
76
|
|
|
* |
77
|
|
|
* Five formatters are included with scssphp/scssphp: |
78
|
|
|
* - ScssPhp\ScssPhp\Formatter\Expanded |
79
|
|
|
* - ScssPhp\ScssPhp\Formatter\Nested (default) |
80
|
|
|
* - ScssPhp\ScssPhp\Formatter\Compressed |
81
|
|
|
* - ScssPhp\ScssPhp\Formatter\Compact |
82
|
|
|
* - ScssPhp\ScssPhp\Formatter\Crunched |
83
|
|
|
* |
84
|
|
|
* @link https://scssphp.github.io/scssphp/docs/#output-formatting |
85
|
|
|
* |
86
|
|
|
* @param string $formatterName |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setFormatter($formatterName) |
91
|
|
|
{ |
92
|
|
|
return parent::setFormatter($formatterName); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.