LaravelRUS /
SleepingOwlAdmin
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SleepingOwl\Admin\Templates; |
||
| 4 | |||
| 5 | use Illuminate\Support\Collection; |
||
| 6 | use KodiCMS\Assets\AssetElement; |
||
| 7 | use KodiCMS\Assets\Assets as BaseAssets; |
||
| 8 | use KodiCMS\Assets\Contracts\AssetElementInterface; |
||
| 9 | use KodiCMS\Assets\Html; |
||
| 10 | use SleepingOwl\Admin\Contracts\Template\AssetsInterface as AssetsContract; |
||
| 11 | |||
| 12 | class Assets extends BaseAssets implements AssetsContract |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $globalVars = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Gets or sets javascript assets. |
||
| 21 | * |
||
| 22 | * @param bool|string $handle |
||
| 23 | * @param string $src Asset source |
||
| 24 | * @param array|string $dependency Dependencies |
||
| 25 | * @param bool $footer Whether to show in header or footer |
||
| 26 | * |
||
| 27 | 285 | * @return AssetElementInterface Setting returns asset array, getting returns asset HTML |
|
| 28 | */ |
||
| 29 | 285 | public function addJs($handle = false, $src = null, $dependency = null, $footer = true) |
|
| 30 | { |
||
| 31 | return parent::addJs($handle, $src, $dependency, $footer); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param AssetElementInterface[] $assets |
||
| 36 | * @return array|bool|static |
||
| 37 | */ |
||
| 38 | protected function sort($assets) |
||
| 39 | { |
||
| 40 | $mainAssets = collect($assets)->filter(function (AssetElement $item) { |
||
| 41 | return ! array_filter($item->getDependency()); |
||
| 42 | }); |
||
| 43 | |||
| 44 | $depAssets = collect($assets)->filter(function (AssetElement $item) { |
||
| 45 | return array_filter($item->getDependency()); |
||
| 46 | }); |
||
| 47 | |||
| 48 | foreach ($depAssets as $key => $asset) { |
||
| 49 | $mainAssets = $this->insertOn($asset, $mainAssets, collect($assets)); |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($mainAssets->count()) { |
||
| 53 | return $mainAssets; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 54 | } |
||
| 55 | |||
| 56 | return parent::sort($assets); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param AssetElement $asset |
||
| 61 | * @param Collection $mainAssets |
||
| 62 | * @param Collection $assets |
||
| 63 | * @return Collection |
||
| 64 | */ |
||
| 65 | protected function insertOn(AssetElement $asset, Collection &$mainAssets, Collection $assets) |
||
| 66 | { |
||
| 67 | $dependency = collect($asset->getDependency()); |
||
| 68 | $checkedDep = null; |
||
| 69 | $hasNotDep = null; |
||
| 70 | |||
| 71 | foreach ($dependency as $dep) { |
||
| 72 | if (! $mainAssets->has($dep)) { |
||
| 73 | $hasNotDep = $dep; |
||
| 74 | break; |
||
| 75 | } |
||
| 76 | |||
| 77 | $checkedDep = $dep; |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($hasNotDep && $assets->get($hasNotDep)) { |
||
|
0 ignored issues
–
show
|
|||
| 81 | return $this->insertOn($assets->get($hasNotDep), $mainAssets, $assets); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($checkedDep) { |
||
|
0 ignored issues
–
show
|
|||
| 85 | return $mainAssets = $this->insertAfter($checkedDep, $mainAssets, $asset->getHandle(), $asset); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $mainAssets; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Inserts a new key/value before the key in the array. |
||
| 93 | * |
||
| 94 | * @param $key |
||
| 95 | * The key to insert before. |
||
| 96 | * @param $array |
||
| 97 | * An array to insert in to. |
||
| 98 | * @param $new_key |
||
| 99 | * The key to insert. |
||
| 100 | * @param $new_value |
||
| 101 | * An value to insert. |
||
| 102 | * @return array|bool The new array if the key exists, FALSE otherwise. |
||
| 103 | * @see array_insert_after() |
||
| 104 | */ |
||
| 105 | protected function insertBefore($key, &$array, $new_key, $new_value) |
||
| 106 | { |
||
| 107 | if (array_key_exists($key, $array)) { |
||
| 108 | $new = []; |
||
| 109 | foreach ($array as $k => $value) { |
||
| 110 | if ($k === $key) { |
||
| 111 | $new[$new_key] = $new_value; |
||
| 112 | } |
||
| 113 | $new[$k] = $value; |
||
| 114 | } |
||
| 115 | |||
| 116 | return $new; |
||
| 117 | } |
||
| 118 | |||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Inserts a new key/value after the key in the array. |
||
| 124 | * |
||
| 125 | * @param $key |
||
| 126 | * The key to insert after. |
||
| 127 | * @param $array |
||
| 128 | * An array to insert in to. |
||
| 129 | * @param $new_key |
||
| 130 | * The key to insert. |
||
| 131 | * @param $new_value |
||
| 132 | * An value to insert. |
||
| 133 | * @return Collection|bool The new array if the key exists, FALSE otherwise. |
||
| 134 | */ |
||
| 135 | protected function insertAfter($key, Collection &$array, $new_key, $new_value) |
||
| 136 | { |
||
| 137 | if ($array->has($key)) { |
||
| 138 | $new = []; |
||
| 139 | foreach ($array as $k => $value) { |
||
| 140 | $new[$k] = $value; |
||
| 141 | if ($k === $key) { |
||
| 142 | $new[$new_key] = $new_value; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return collect($new); |
||
| 147 | } |
||
| 148 | |||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Добавление глобальной переменной. |
||
| 154 | * |
||
| 155 | * @param string $key |
||
| 156 | * @param mixed $value |
||
| 157 | * |
||
| 158 | * @return self |
||
| 159 | */ |
||
| 160 | public function putGlobalVar($key, $value) |
||
| 161 | { |
||
| 162 | $this->globalVars[$key] = $value; |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function globalVars() |
||
| 171 | { |
||
| 172 | return $this->globalVars; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function render() |
||
| 179 | { |
||
| 180 | return $this->renderGlobalVars().PHP_EOL.parent::render(); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function renderGlobalVars() |
||
| 187 | { |
||
| 188 | $json = json_encode($this->globalVars); |
||
| 189 | |||
| 190 | return (new Html())->vars("{$this->namespace}.GlobalConfig = {$json};"); |
||
| 191 | } |
||
| 192 | } |
||
| 193 |