Total Complexity | 102 |
Total Lines | 680 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 5 | Features | 0 |
Complex classes like Asset often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Asset, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Asset implements \ArrayAccess |
||
24 | { |
||
25 | /** @var Builder */ |
||
26 | protected $builder; |
||
27 | |||
28 | /** @var Config */ |
||
29 | protected $config; |
||
30 | |||
31 | /** @var array */ |
||
32 | protected $data = []; |
||
33 | |||
34 | /** @var bool */ |
||
35 | protected $optimized = false; |
||
36 | |||
37 | /** @var bool */ |
||
38 | protected $fingerprinted = false; |
||
39 | |||
40 | /** @var bool */ |
||
41 | protected $compiled = false; |
||
42 | |||
43 | /** @var bool */ |
||
44 | protected $minified = false; |
||
45 | |||
46 | /** @var bool */ |
||
47 | protected $ignore_missing = false; |
||
48 | |||
49 | /** |
||
50 | * Creates an Asset from file(s) path. |
||
51 | * |
||
52 | * $options[ |
||
53 | * 'fingerprint' => true, |
||
54 | * 'minify' => true, |
||
55 | * 'filename' => '', |
||
56 | * 'ignore_missing' => false, |
||
57 | * ]; |
||
58 | * |
||
59 | * @param Builder $builder |
||
60 | * @param string|array $paths |
||
61 | * @param array|null $options |
||
62 | * |
||
63 | * @throws RuntimeException |
||
64 | */ |
||
65 | public function __construct(Builder $builder, $paths, array $options = null) |
||
66 | { |
||
67 | $this->builder = $builder; |
||
68 | $this->config = $builder->getConfig(); |
||
69 | $paths = is_array($paths) ? $paths : [$paths]; |
||
70 | array_walk($paths, function ($path) { |
||
71 | if (empty($path)) { |
||
72 | throw new RuntimeException('The path parameter of "asset() can\'t be empty."'); |
||
73 | } |
||
74 | }); |
||
75 | $this->data = [ |
||
76 | 'file' => '', |
||
77 | 'filename' => '', |
||
78 | 'path_source' => '', |
||
79 | 'path' => '', |
||
80 | 'ext' => '', |
||
81 | 'type' => '', |
||
82 | 'subtype' => '', |
||
83 | 'size' => 0, |
||
84 | 'content_source' => '', |
||
85 | 'content' => '', |
||
86 | ]; |
||
87 | |||
88 | // handles options |
||
89 | $optimize = (bool) $this->config->get('assets.images.optimize.enabled'); |
||
90 | $fingerprint = (bool) $this->config->get('assets.fingerprint.enabled'); |
||
91 | $minify = (bool) $this->config->get('assets.minify.enabled'); |
||
92 | $filename = ''; |
||
93 | $ignore_missing = false; |
||
94 | $force_slash = true; |
||
95 | extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
||
96 | $this->ignore_missing = $ignore_missing; |
||
97 | |||
98 | // fill data array with file(s) informations |
||
99 | $cache = new Cache($this->builder, 'assets'); |
||
100 | $cacheKey = sprintf('%s.ser', implode('_', $paths)); |
||
101 | if (!$cache->has($cacheKey)) { |
||
102 | $pathsCount = count($paths); |
||
103 | $file = []; |
||
104 | for ($i = 0; $i < $pathsCount; $i++) { |
||
105 | // loads file(s) |
||
106 | $file[$i] = $this->loadFile($paths[$i], $ignore_missing, $force_slash); |
||
107 | // bundle: same type/ext only |
||
108 | if ($i > 0) { |
||
109 | if ($file[$i]['type'] != $file[$i - 1]['type']) { |
||
110 | throw new RuntimeException(\sprintf('Asset bundle type error (%s != %s).', $file[$i]['type'], $file[$i - 1]['type'])); |
||
111 | } |
||
112 | if ($file[$i]['ext'] != $file[$i - 1]['ext']) { |
||
113 | throw new RuntimeException(\sprintf('Asset bundle extension error (%s != %s).', $file[$i]['ext'], $file[$i - 1]['ext'])); |
||
114 | } |
||
115 | } |
||
116 | // missing allowed = empty path |
||
117 | if ($file[$i]['missing']) { |
||
118 | $this->data['path'] = ''; |
||
119 | |||
120 | continue; |
||
121 | } |
||
122 | // set data |
||
123 | if ($i == 0) { |
||
124 | $this->data['file'] = $file[$i]['filepath']; // should be an array of files in case of bundle? |
||
125 | $this->data['filename'] = $file[$i]['path']; |
||
126 | $this->data['path_source'] = $file[$i]['path']; |
||
127 | $this->data['path'] = $file[$i]['path']; |
||
128 | if (!empty($filename)) { |
||
129 | $this->data['path'] = '/'.ltrim($filename, '/'); |
||
130 | } |
||
131 | $this->data['ext'] = $file[$i]['ext']; |
||
132 | $this->data['type'] = $file[$i]['type']; |
||
133 | $this->data['subtype'] = $file[$i]['subtype']; |
||
134 | } |
||
135 | $this->data['size'] += $file[$i]['size']; |
||
136 | $this->data['content_source'] .= $file[$i]['content']; |
||
137 | $this->data['content'] .= $file[$i]['content']; |
||
138 | } |
||
139 | // bundle: define path |
||
140 | if ($pathsCount > 1) { |
||
141 | if (empty($filename)) { |
||
142 | switch ($this->data['ext']) { |
||
143 | case 'scss': |
||
144 | case 'css': |
||
145 | $this->data['path'] = '/styles.'.$file[0]['ext']; |
||
146 | break; |
||
147 | case 'js': |
||
148 | $this->data['path'] = '/scripts.'.$file[0]['ext']; |
||
149 | break; |
||
150 | default: |
||
151 | throw new RuntimeException(\sprintf('Asset bundle supports "%s" files only.', 'scss, css and js')); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | $cache->set($cacheKey, $this->data); |
||
156 | } |
||
157 | $this->data = $cache->get($cacheKey); |
||
158 | |||
159 | // optimizing |
||
160 | if ($optimize) { |
||
161 | $this->optimize(); |
||
162 | } |
||
163 | // fingerprinting |
||
164 | if ($fingerprint) { |
||
165 | $this->fingerprint(); |
||
166 | } |
||
167 | // compiling |
||
168 | if ((bool) $this->config->get('assets.compile.enabled')) { |
||
169 | $this->compile(); |
||
170 | } |
||
171 | // minifying |
||
172 | if ($minify) { |
||
173 | $this->minify(); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Returns path. |
||
179 | * |
||
180 | * @throws RuntimeException |
||
181 | */ |
||
182 | public function __toString(): string |
||
183 | { |
||
184 | try { |
||
185 | $this->save(); |
||
186 | } catch (\Exception $e) { |
||
187 | $this->builder->getLogger()->error($e->getMessage()); |
||
188 | } |
||
189 | |||
190 | return $this->data['path']; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Fingerprints a file. |
||
195 | */ |
||
196 | public function fingerprint(): self |
||
197 | { |
||
198 | if ($this->fingerprinted) { |
||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | $fingerprint = hash('md5', $this->data['content_source']); |
||
203 | $this->data['path'] = preg_replace( |
||
204 | '/\.'.$this->data['ext'].'$/m', |
||
205 | ".$fingerprint.".$this->data['ext'], |
||
206 | $this->data['path'] |
||
207 | ); |
||
208 | |||
209 | $this->fingerprinted = true; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Compiles a SCSS. |
||
216 | * |
||
217 | * @throws RuntimeException |
||
218 | */ |
||
219 | public function compile(): self |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Minifying a CSS or a JS. |
||
293 | * |
||
294 | * @throws RuntimeException |
||
295 | */ |
||
296 | public function minify(): self |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Optimizing an image. |
||
350 | */ |
||
351 | public function optimize(): self |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Resizes an image. |
||
393 | * |
||
394 | * @throws RuntimeException |
||
395 | */ |
||
396 | public function resize(int $size): self |
||
397 | { |
||
398 | if ($size >= $this->getWidth()) { |
||
399 | return $this; |
||
400 | } |
||
401 | |||
402 | $cache = new Cache($this->builder, 'assets'); |
||
403 | $cacheKey = $cache->createKeyFromAsset($this, "{$size}x"); |
||
404 | if (!$cache->has($cacheKey)) { |
||
405 | if ($this->data['type'] !== 'image') { |
||
406 | throw new RuntimeException(\sprintf('Not able to resize "%s"', $this->data['path'])); |
||
407 | } |
||
408 | if (!extension_loaded('gd')) { |
||
409 | throw new RuntimeException('GD extension is required to use images resize.'); |
||
410 | } |
||
411 | |||
412 | try { |
||
413 | $img = ImageManager::make($this->data['content_source']); |
||
414 | $img->resize($size, null, function (\Intervention\Image\Constraint $constraint) { |
||
415 | $constraint->aspectRatio(); |
||
416 | $constraint->upsize(); |
||
417 | }); |
||
418 | } catch (\Exception $e) { |
||
419 | throw new RuntimeException(\sprintf('Not able to resize image "%s": %s', $this->data['path'], $e->getMessage())); |
||
420 | } |
||
421 | $this->data['path'] = '/'.Util::joinPath((string) $this->config->get('assets.target'), 'thumbnails', (string) $size, $this->data['path']); |
||
422 | |||
423 | try { |
||
424 | $this->data['content'] = (string) $img->encode($this->data['ext'], $this->config->get('assets.images.quality')); |
||
425 | } catch (\Exception $e) { |
||
426 | throw new RuntimeException(\sprintf('Not able to encode image "%s": %s', $this->data['path'], $e->getMessage())); |
||
427 | } |
||
428 | |||
429 | $cache->set($cacheKey, $this->data); |
||
430 | } |
||
431 | $this->data = $cache->get($cacheKey); |
||
432 | |||
433 | return $this; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Returns the data URL of an image. |
||
438 | * |
||
439 | * @throws RuntimeException |
||
440 | */ |
||
441 | public function dataurl(): string |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Implements \ArrayAccess. |
||
452 | */ |
||
453 | #[\ReturnTypeWillChange] |
||
454 | public function offsetSet($offset, $value) |
||
455 | { |
||
456 | if (!is_null($offset)) { |
||
457 | $this->data[$offset] = $value; |
||
458 | } |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Implements \ArrayAccess. |
||
463 | */ |
||
464 | #[\ReturnTypeWillChange] |
||
465 | public function offsetExists($offset) |
||
466 | { |
||
467 | return isset($this->data[$offset]); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Implements \ArrayAccess. |
||
472 | */ |
||
473 | #[\ReturnTypeWillChange] |
||
474 | public function offsetUnset($offset) |
||
475 | { |
||
476 | unset($this->data[$offset]); |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Implements \ArrayAccess. |
||
481 | */ |
||
482 | #[\ReturnTypeWillChange] |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * Hashing content of an asset with the specified algo, sha384 by default. |
||
490 | * Used for SRI (Subresource Integrity). |
||
491 | * |
||
492 | * @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity |
||
493 | */ |
||
494 | public function getIntegrity(string $algo = 'sha384'): string |
||
495 | { |
||
496 | return \sprintf('%s-%s', $algo, base64_encode(hash($algo, $this->data['content'], true))); |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Returns the width of an image. |
||
501 | * |
||
502 | * @return false|int |
||
503 | */ |
||
504 | public function getWidth() |
||
505 | { |
||
506 | if (false === $size = $this->getImageSize()) { |
||
507 | return false; |
||
508 | } |
||
509 | |||
510 | return $size[0]; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Returns the height of an image. |
||
515 | * |
||
516 | * @return false|int |
||
517 | */ |
||
518 | public function getHeight() |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Returns MP3 file infos. |
||
529 | * |
||
530 | * @see https://github.com/wapmorgan/Mp3Info |
||
531 | */ |
||
532 | public function getAudio(): Mp3Info |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Saves file. |
||
539 | * Note: a file from `static/` with the same name will NOT be overridden. |
||
540 | * |
||
541 | * @throws RuntimeException |
||
542 | */ |
||
543 | public function save(): void |
||
553 | } |
||
554 | } |
||
555 | } |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * Load file data. |
||
560 | * |
||
561 | * @throws RuntimeException |
||
562 | */ |
||
563 | private function loadFile(string $path, bool $ignore_missing = false, bool $force_slash = true): array |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * Try to find the file: |
||
614 | * 1. remote (if $path is a valid URL) |
||
615 | * 2. in static/ |
||
616 | * 3. in themes/<theme>/static/ |
||
617 | * Returns local file path or false if file don't exists. |
||
618 | * |
||
619 | * @throws RuntimeException |
||
620 | * |
||
621 | * @return string|false |
||
622 | */ |
||
623 | private function findFile(string $path) |
||
624 | { |
||
625 | // in case of remote file: save it and returns cached file path |
||
626 | if (Util\Url::isUrl($path)) { |
||
627 | $url = $path; |
||
628 | $relativePath = Page::slugify(\sprintf('%s%s-%s', parse_url($url, PHP_URL_HOST), parse_url($url, PHP_URL_PATH), parse_url($url, PHP_URL_QUERY))); |
||
629 | $filePath = Util::joinFile($this->config->getCacheAssetsPath(), $relativePath); |
||
630 | if (!file_exists($filePath)) { |
||
631 | if (!Util\Url::isRemoteFileExists($url)) { |
||
632 | return false; |
||
633 | } |
||
634 | if (false === $content = Util\File::fileGetContents($url, true)) { |
||
635 | return false; |
||
636 | } |
||
637 | if (strlen($content) <= 1) { |
||
638 | throw new RuntimeException(\sprintf('Asset at "%s" is empty.', $url)); |
||
639 | } |
||
640 | Util\File::getFS()->dumpFile($filePath, $content); |
||
641 | } |
||
642 | |||
643 | return $filePath; |
||
644 | } |
||
645 | |||
646 | // checks in assets/ |
||
647 | $filePath = Util::joinFile($this->config->getAssetsPath(), $path); |
||
648 | if (Util\File::getFS()->exists($filePath)) { |
||
649 | return $filePath; |
||
650 | } |
||
651 | |||
652 | // checks in each themes/<theme>/assets/ |
||
653 | foreach ($this->config->getTheme() as $theme) { |
||
654 | $filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'assets'), $path); |
||
655 | if (Util\File::getFS()->exists($filePath)) { |
||
656 | return $filePath; |
||
657 | } |
||
658 | } |
||
659 | |||
660 | // checks in static/ |
||
661 | $filePath = Util::joinFile($this->config->getStaticTargetPath(), $path); |
||
662 | if (Util\File::getFS()->exists($filePath)) { |
||
663 | return $filePath; |
||
664 | } |
||
665 | |||
666 | // checks in each themes/<theme>/static/ |
||
667 | foreach ($this->config->getTheme() as $theme) { |
||
668 | $filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path); |
||
669 | if (Util\File::getFS()->exists($filePath)) { |
||
670 | return $filePath; |
||
671 | } |
||
672 | } |
||
673 | |||
674 | return false; |
||
675 | } |
||
676 | |||
677 | /** |
||
678 | * Returns image size informations. |
||
679 | * |
||
680 | * @see https://www.php.net/manual/function.getimagesize.php |
||
681 | * |
||
682 | * @return false|array |
||
683 | */ |
||
684 | private function getImageSize() |
||
685 | { |
||
686 | if (!$this->data['type'] == 'image') { |
||
687 | return false; |
||
688 | } |
||
689 | |||
690 | if (false === $size = getimagesizefromstring($this->data['content'])) { |
||
691 | return false; |
||
692 | } |
||
693 | |||
694 | return $size; |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * Replaces some characters by '_'. |
||
699 | */ |
||
700 | private function sanitize(string $string): string |
||
703 | } |
||
704 | } |
||
705 |