1 | <?php declare(strict_types=1); |
||
22 | class BuildAsset extends Robo\Task\BaseTask implements Robo\Contract\BuilderAwareInterface |
||
23 | { |
||
24 | use Robo\Common\BuilderAwareTrait; |
||
25 | |||
26 | /** |
||
27 | * This should be set the final location of the asset. |
||
28 | * Eg: ```./assets/script.min.js``` |
||
29 | * |
||
30 | * @var SplFileInfo |
||
31 | */ |
||
32 | protected $destination; |
||
33 | |||
34 | /** |
||
35 | * An array of source file / folder paths, that will be |
||
36 | * concatenated together to build the final asset. |
||
37 | * |
||
38 | * @var string[] |
||
39 | */ |
||
40 | protected $source; |
||
41 | |||
42 | /** |
||
43 | * If this is set to true, we will not minify the asset. |
||
44 | * Thus allowing for easier debugging during in development. |
||
45 | * |
||
46 | * **Defaults to:** ```false``` |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $debug = false; |
||
51 | |||
52 | /** |
||
53 | * If set to true and debug is also set to false we will rename the final |
||
54 | * asset to include an md5 hash of it's contents. You may also use this in |
||
55 | * conjuction with the template option. |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $cachebust = false; |
||
60 | |||
61 | /** |
||
62 | * If cachebust is set to true, the assets will be renamed. |
||
63 | * Files in this array will be opened and searched for the old filenames, |
||
64 | * replacing with the new cache busted filenames. |
||
65 | * |
||
66 | * @var string[] |
||
67 | */ |
||
68 | protected $template = []; |
||
69 | |||
70 | /** |
||
71 | * If set to true we will also output a gzipped version of the asset so that |
||
72 | * you can setup your webserver to serve the pre gzipped version of the |
||
73 | * asset instead of doing it on the fly. |
||
74 | * |
||
75 | * **Defaults to:** ```false``` |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $gz = false; |
||
80 | |||
81 | /** |
||
82 | * Only applies when building css assets. If set to true the default we will |
||
83 | * automatically prefix the built css using ```vladkens/autoprefixer-php```. |
||
84 | * |
||
85 | * If set to false we will not do any prefixing. |
||
86 | * |
||
87 | * If set to a string or an array, we will perform prefixing and pass the |
||
88 | * value through to the setBrowsers method of the Autoprefixer class, |
||
89 | * allowing you to easily configure the prefixer. |
||
90 | * |
||
91 | * **Defaults to:** ```false``` |
||
92 | * |
||
93 | * @var bool |
||
94 | */ |
||
95 | protected $autoprefix = false; |
||
96 | |||
97 | /** |
||
98 | * BuildAssetTask Constructor. |
||
99 | * |
||
100 | * @param string $destination The path to where we will save the built asset. |
||
101 | */ |
||
102 | public function __construct(string $destination) |
||
106 | |||
107 | /** |
||
108 | * Set the source(s) of the asset. |
||
109 | * |
||
110 | * @param string|string[]|Finder $value Can be a single path to a folder or |
||
111 | * file, an array of files of folders, |
||
112 | * or a Finder instance. |
||
113 | * |
||
114 | * @return BuildAsset Returns our self for method chaining. |
||
115 | */ |
||
116 | public function source($value): self |
||
120 | |||
121 | /** |
||
122 | * Debug setter. |
||
123 | * |
||
124 | * @param bool $value |
||
125 | * @return self |
||
126 | */ |
||
127 | public function debug(bool $value): self |
||
131 | |||
132 | /** |
||
133 | * Template setter. |
||
134 | * |
||
135 | * @param string|string[]|Finder $value Can be a single path to a folder or |
||
136 | * file, an array of files of folders, |
||
137 | * or a Finder instance. |
||
138 | * |
||
139 | * @return self |
||
140 | */ |
||
141 | public function template($value): self |
||
145 | |||
146 | /** |
||
147 | * Gz Setter. |
||
148 | * |
||
149 | * @param bool $value |
||
150 | * @return self |
||
151 | */ |
||
152 | public function gz(bool $value): self |
||
156 | |||
157 | /** |
||
158 | * Autoprefix Setter. |
||
159 | * |
||
160 | * @param bool $value |
||
161 | * @return self |
||
162 | */ |
||
163 | public function autoprefix(bool $value): self |
||
167 | |||
168 | /** |
||
169 | * Cachebust Setter. |
||
170 | * |
||
171 | * @param bool $value |
||
172 | * @return self |
||
173 | */ |
||
174 | public function cachebust(bool $value): self |
||
178 | |||
179 | /** |
||
180 | * The main run method. |
||
181 | * |
||
182 | * ```php |
||
183 | * $this->taskBuildAsset('/path/to/asset.js') |
||
184 | * ->source |
||
185 | * ([ |
||
186 | * '/path/to/asset1.js', |
||
187 | * '/path/to/asset2.js', |
||
188 | * '/path/to/asset3.js', |
||
189 | * '/path/to/assetetc.js' |
||
190 | * ]) |
||
191 | * ->run(); |
||
192 | * ``` |
||
193 | * @return Robo\Result |
||
194 | */ |
||
195 | public function run(): Robo\Result |
||
239 | |||
240 | /** |
||
241 | * Creates a new compiler based on the file extension type. |
||
242 | * |
||
243 | * @param SplFileInfo $file |
||
244 | * @return Compiler |
||
245 | */ |
||
246 | protected function getCompiler(SplFileInfo $file): Compiler |
||
273 | |||
274 | /** |
||
275 | * Determins the type of source we are dealing file. |
||
276 | * |
||
277 | * Normally this is as simple as looking at the file extension, |
||
278 | * however a folder doesn't have one of those so we mimic it here. |
||
279 | * |
||
280 | * @param SplFileInfo $file |
||
281 | * @return string |
||
282 | */ |
||
283 | protected function getSourceType(SplFileInfo $file): string |
||
288 | |||
289 | /** |
||
290 | * So that we can bust the client cache in browser, we will rename the |
||
291 | * asset filename, using a timestamp. But we also need to update the |
||
292 | * HTML that includes the asset into the web page. |
||
293 | * This method does all that for us. |
||
294 | * |
||
295 | * @param string $asset_contents |
||
296 | * |
||
297 | * @return void |
||
298 | */ |
||
299 | protected function bustCacheBalls(string $asset_contents) |
||
350 | |||
351 | /** |
||
352 | * The business end, finally lets actually save the |
||
353 | * compiled / minified asset. |
||
354 | * |
||
355 | * @param string $asset_contents |
||
356 | */ |
||
357 | protected function writeAsset(string $asset_contents) |
||
391 | |||
392 | /** |
||
393 | * Helper method to convert several possible inputs |
||
394 | * to a simple array of file paths. |
||
395 | * |
||
396 | * @param string|string[]|Finder $input Can be a single path to a folder or |
||
397 | * file, an array of files of folders, |
||
398 | * or a Finder instance. |
||
399 | * |
||
400 | * @return string[] |
||
401 | */ |
||
402 | protected function normaliseSrcInput($input): array |
||
426 | } |
||
427 |