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