|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: Inhere |
| 5 | + * Date: 2017-08-30 |
| 6 | + * Use : ... |
| 7 | + * File: AssetsRendererTrait.php |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Inhere\Web; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class AssetsRendererTrait |
| 14 | + * @package Inhere\Web |
| 15 | + */ |
| 16 | +trait SimpleAssetsLoaderTrait |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + private $pageAssets = []; |
| 22 | + |
| 23 | + /******************************************************************************** |
| 24 | + * css files |
| 25 | + *******************************************************************************/ |
| 26 | + |
| 27 | + /** |
| 28 | + * @param string|array $cssFile |
| 29 | + * @param string $key |
| 30 | + * @return $this |
| 31 | + */ |
| 32 | + public function addTopCssFile($cssFile, string $key = null) |
| 33 | + { |
| 34 | + return $this->addCssFile($cssFile, 'top', $key); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param string|array $cssFile |
| 39 | + * @param string $key |
| 40 | + * @return $this |
| 41 | + */ |
| 42 | + public function addBottomCssFile($cssFile, string $key = null) |
| 43 | + { |
| 44 | + return $this->addCssFile($cssFile, 'bottom', $key); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param string|array $cssFile |
| 49 | + * @param string $position |
| 50 | + * @param string $key |
| 51 | + * @return $this |
| 52 | + */ |
| 53 | + public function addCssFile($cssFile, string $position = 'top', string $key = null) |
| 54 | + { |
| 55 | + if (\is_array($cssFile)) { |
| 56 | + foreach ($cssFile as $k => $code) { |
| 57 | + $this->addCssFile($code, $position, \is_int($k) ? null : $k); |
| 58 | + } |
| 59 | + |
| 60 | + return $this; |
| 61 | + } |
| 62 | + |
| 63 | + if ($key) { |
| 64 | + $this->pageAssets['__cssFiles:' . $position][$key] = $cssFile; |
| 65 | + } else { |
| 66 | + $this->pageAssets['__cssFiles:' . $position][] = $cssFile; |
| 67 | + } |
| 68 | + |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + /******************************************************************************** |
| 73 | + * js files |
| 74 | + *******************************************************************************/ |
| 75 | + |
| 76 | + /** |
| 77 | + * @param string|array $jsFile |
| 78 | + * @param string $key |
| 79 | + * @return $this |
| 80 | + */ |
| 81 | + public function addTopJsFile($jsFile, string $key = null) |
| 82 | + { |
| 83 | + return $this->addJsFile($jsFile, 'top', $key); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param string|array $jsFile |
| 88 | + * @param string $key |
| 89 | + * @return $this |
| 90 | + */ |
| 91 | + public function addBottomJsFile($jsFile, string $key = null) |
| 92 | + { |
| 93 | + return $this->addJsFile($jsFile, 'bottom', $key); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param string|array $jsFile |
| 98 | + * @param string $position |
| 99 | + * @param string $key |
| 100 | + * @return $this |
| 101 | + */ |
| 102 | + public function addJsFile($jsFile, string $position = 'bottom', string $key = null) |
| 103 | + { |
| 104 | + if (\is_array($jsFile)) { |
| 105 | + foreach ($jsFile as $k => $code) { |
| 106 | + $this->addJsFile($code, $position, \is_int($k) ? null : $k); |
| 107 | + } |
| 108 | + |
| 109 | + return $this; |
| 110 | + } |
| 111 | + |
| 112 | + if ($key) { |
| 113 | + $this->pageAssets['__jsFiles:' . $position][$key] = $jsFile; |
| 114 | + } else { |
| 115 | + $this->pageAssets['__jsFiles:' . $position][] = $jsFile; |
| 116 | + } |
| 117 | + |
| 118 | + return $this; |
| 119 | + } |
| 120 | + |
| 121 | + /******************************************************************************** |
| 122 | + * css codes |
| 123 | + *******************************************************************************/ |
| 124 | + |
| 125 | + /** |
| 126 | + * @param string|array $cssCode |
| 127 | + * @param string $key |
| 128 | + * @return $this |
| 129 | + */ |
| 130 | + public function addTopCss($cssCode, string $key = null) |
| 131 | + { |
| 132 | + return $this->addCss($cssCode, 'top', $key); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @param string|array $cssCode |
| 137 | + * @param string $key |
| 138 | + * @return $this |
| 139 | + */ |
| 140 | + public function addBottomCss($cssCode, string $key = null) |
| 141 | + { |
| 142 | + return $this->addCss($cssCode, 'bottom', $key); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param string|array $cssCode |
| 147 | + * @param string $position |
| 148 | + * @param string $key |
| 149 | + * @return $this |
| 150 | + */ |
| 151 | + public function addCss($cssCode, string $position = 'top', string $key = null) |
| 152 | + { |
| 153 | + if (\is_array($cssCode)) { |
| 154 | + foreach ($cssCode as $k => $code) { |
| 155 | + $this->addCss($code, $position, \is_int($k) ? null : $k); |
| 156 | + } |
| 157 | + |
| 158 | + return $this; |
| 159 | + } |
| 160 | + |
| 161 | + if ($key) { |
| 162 | + $this->pageAssets['__cssCodes:' . $position][$key] = $cssCode; |
| 163 | + } else { |
| 164 | + $this->pageAssets['__cssCodes:' . $position][] = $cssCode; |
| 165 | + } |
| 166 | + |
| 167 | + return $this; |
| 168 | + } |
| 169 | + |
| 170 | + /******************************************************************************** |
| 171 | + * js codes |
| 172 | + *******************************************************************************/ |
| 173 | + |
| 174 | + /** |
| 175 | + * @param string|array $jsCode |
| 176 | + * @param string $key |
| 177 | + * @return $this |
| 178 | + */ |
| 179 | + public function addTopJs($jsCode, string $key = null) |
| 180 | + { |
| 181 | + return $this->addJs($jsCode, 'top', $key); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @param string|array $jsCode |
| 186 | + * @param string $key |
| 187 | + * @return $this |
| 188 | + */ |
| 189 | + public function addBottomJs($jsCode, string $key = null) |
| 190 | + { |
| 191 | + return $this->addJs($jsCode, 'bottom', $key); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * @param string|array $jsCode |
| 196 | + * @param string $position |
| 197 | + * @param string $key |
| 198 | + * @return $this |
| 199 | + */ |
| 200 | + public function addJs($jsCode, string $position = 'bottom', string $key = null) |
| 201 | + { |
| 202 | + if (\is_array($jsCode)) { |
| 203 | + foreach ($jsCode as $k => $code) { |
| 204 | + $this->addJs($code, $position, \is_int($k) ? null : $k); |
| 205 | + } |
| 206 | + |
| 207 | + return $this; |
| 208 | + } |
| 209 | + |
| 210 | + if ($key) { |
| 211 | + $this->pageAssets['__jsCodes:' . $position][$key] = $jsCode; |
| 212 | + } else { |
| 213 | + $this->pageAssets['__jsCodes:' . $position][] = $jsCode; |
| 214 | + } |
| 215 | + |
| 216 | + return $this; |
| 217 | + } |
| 218 | + |
| 219 | + /******************************************************************************** |
| 220 | + * dump assets |
| 221 | + *******************************************************************************/ |
| 222 | + |
| 223 | + /** |
| 224 | + * @param bool $echo |
| 225 | + * @return null|string |
| 226 | + */ |
| 227 | + public function dumpTopAssets($echo = true) |
| 228 | + { |
| 229 | + return $this->dumpAssets('top', $echo); |
| 230 | + } |
| 231 | + |
| 232 | + public function dumpBottomAssets($echo = true) |
| 233 | + { |
| 234 | + return $this->dumpAssets('bottom', $echo); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * dump Assets |
| 239 | + * |
| 240 | + * @param string $position |
| 241 | + * @param boolean $echo |
| 242 | + * @return string|null |
| 243 | + */ |
| 244 | + public function dumpAssets($position = 'top', $echo = true) |
| 245 | + { |
| 246 | + $assetHtml = ''; |
| 247 | + |
| 248 | + /** @var array $files css files */ |
| 249 | + if ($files = $this->getPageAsset('__cssFiles:' . $position)) { |
| 250 | + foreach ($files as $file) { |
| 251 | + $assetHtml .= '<link href="'. $file .'" rel="stylesheet">' . PHP_EOL; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + /** @var array $codes css codes */ |
| 256 | + if ($codes = $this->getPageAsset('__cssCodes:' . $position)) { |
| 257 | + $assetHtml .= '<style type="text/css">' . PHP_EOL; |
| 258 | + foreach ($codes as $code) { |
| 259 | + $assetHtml .= $code . PHP_EOL; |
| 260 | + } |
| 261 | + $assetHtml .= '</style>' . PHP_EOL; |
| 262 | + } |
| 263 | + |
| 264 | + /** @var array $files js files */ |
| 265 | + if ($files = $this->getPageAsset('__jsFiles:' . $position)) { |
| 266 | + foreach ($files as $file) { |
| 267 | + $assetHtml .= '<script src="'. $file .'"></script>' . PHP_EOL; |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + /** @var array $codes js codes */ |
| 272 | + if ($codes = $this->getPageAsset('__jsCodes:' . $position)) { |
| 273 | + $jsCode = ''; |
| 274 | + |
| 275 | + foreach ($codes as $code) { |
| 276 | + $jsCode .= $code . PHP_EOL; |
| 277 | + } |
| 278 | + |
| 279 | + if ($jsCode) { |
| 280 | + $assetHtml .= sprintf("<script type=\"text/javascript\">\n%s</script>\n", $jsCode); |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + if (!$echo) { |
| 285 | + return $assetHtml; |
| 286 | + } |
| 287 | + |
| 288 | + // echo it. |
| 289 | + if ($assetHtml) { |
| 290 | + echo '<!-- dumped assets -->' . PHP_EOL . $assetHtml; |
| 291 | + } |
| 292 | + |
| 293 | + return null; |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * @param string $key |
| 298 | + * @return array|null |
| 299 | + */ |
| 300 | + public function getPageAsset(string $key) |
| 301 | + { |
| 302 | + return $this->pageAssets[$key] ?? null; |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * @return array |
| 307 | + */ |
| 308 | + public function getPageAssets(): array |
| 309 | + { |
| 310 | + return $this->pageAssets; |
| 311 | + } |
| 312 | +} |
0 commit comments