moisespontes /
view
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DevPontes\View; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class DevPontes Assets |
||
| 7 | * |
||
| 8 | * @author Moises Pontes <[email protected]> |
||
| 9 | * @package DevPontes\View |
||
| 10 | */ |
||
| 11 | class Assets |
||
| 12 | { |
||
| 13 | private bool $cache; |
||
| 14 | private string $styles; |
||
| 15 | private string $source; |
||
| 16 | private string $scripts; |
||
| 17 | private string $stylePath; |
||
| 18 | private string $scriptPath; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Assets constructor. |
||
| 22 | * |
||
| 23 | * @param string $src path to resources folder |
||
| 24 | * @param boolean $cache [optional] cache on/off, defaults true |
||
| 25 | * @example - new Assets('assets', false); |
||
| 26 | */ |
||
| 27 | public function __construct(string $src, bool $cache = true) |
||
| 28 | { |
||
| 29 | $this->styles = ''; |
||
| 30 | $this->scripts = ''; |
||
| 31 | |||
| 32 | $this->source = $src; |
||
| 33 | $this->cache = $cache; |
||
| 34 | |||
| 35 | $this->stylePath = "{$this->source}/css"; |
||
| 36 | $this->scriptPath = "{$this->source}/js"; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | public function getStyles(): string |
||
| 43 | { |
||
| 44 | return $this->styles; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public function getScripts(): string |
||
| 51 | { |
||
| 52 | return $this->scripts; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Set path for styles |
||
| 57 | * |
||
| 58 | * @param string $stylePath |
||
| 59 | * @return Assets |
||
| 60 | */ |
||
| 61 | public function setStylePath(string $stylePath): Assets |
||
| 62 | { |
||
| 63 | $this->stylePath = $this->source . '/' . ltrim($stylePath, '/'); |
||
| 64 | return $this; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Set path for scripts |
||
| 69 | * |
||
| 70 | * @param string $scriptPath |
||
| 71 | * @return Assets |
||
| 72 | */ |
||
| 73 | public function setScriptPath(string $scriptPath): Assets |
||
| 74 | { |
||
| 75 | $this->scriptPath = $this->source . '/' . ltrim($scriptPath, '/'); |
||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Make CSS styles |
||
| 81 | * |
||
| 82 | * @param array $css array of styles |
||
| 83 | * @return Assets |
||
| 84 | * @example - $a->makeStyle($css['global', ...]); |
||
| 85 | */ |
||
| 86 | public function makeStyle(array $css): Assets |
||
| 87 | { |
||
| 88 | $this->styles = $this->build('link', $css, $this->stylePath); |
||
| 89 | return $this; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Make JS scripts |
||
| 94 | * |
||
| 95 | * @param array $js array of scripts |
||
| 96 | * @return Assets |
||
| 97 | * @example - $a->makeScript($js['global', ...]); |
||
| 98 | */ |
||
| 99 | public function makeScript(array $js): Assets |
||
| 100 | { |
||
| 101 | $this->scripts = $this->build('script', $js, $this->scriptPath); |
||
| 102 | return $this; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Build styles and scripts |
||
| 107 | * |
||
| 108 | * @param string $tag |
||
| 109 | * @param array $files |
||
| 110 | * @param string $path |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | private function build(string $tag, array $files, string $path): string |
||
| 114 | { |
||
| 115 | $version = $this->cache ? "" : "?v=" . time(); |
||
| 116 | |||
| 117 | if (!$files) { |
||
|
0 ignored issues
–
show
|
|||
| 118 | return ''; |
||
| 119 | } |
||
| 120 | |||
| 121 | $tags = array_map(fn($file) => match ($tag) { |
||
| 122 | 'script' => "<script src='{$path}/{$file}.js{$version}'></script>", |
||
| 123 | 'link' => "<link rel='stylesheet' href='{$path}/{$file}.css{$version}'>", |
||
| 124 | }, $files); |
||
| 125 | |||
| 126 | return implode("\n ", $tags) . "\n "; |
||
| 127 | } |
||
| 128 | } |
||
| 129 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.