Skip to content

Commit 187a12d

Browse files
authored
Add Remove script/style items directly. (#8)
* Add Remove script/style items directly. * Apple styleci * Apple styleci
1 parent cf37d6a commit 187a12d

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

Diff for: src/Assets.php

+50-25
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class Assets
5757
/**
5858
* Assets constructor.
5959
*
60-
* @param Repository $config
61-
* @param HtmlBuilder $htmlBuilder
60+
* @param Repository $config
61+
* @param HtmlBuilder $htmlBuilder
6262
*/
6363
public function __construct(Repository $config, HtmlBuilder $htmlBuilder)
6464
{
@@ -74,7 +74,7 @@ public function __construct(Repository $config, HtmlBuilder $htmlBuilder)
7474
/**
7575
* Add scripts to current module.
7676
*
77-
* @param array $assets
77+
* @param array $assets
7878
* @return $this
7979
*/
8080
public function addScripts($assets)
@@ -87,7 +87,7 @@ public function addScripts($assets)
8787
/**
8888
* Add Css to current module.
8989
*
90-
* @param string[] $assets
90+
* @param string[] $assets
9191
* @return $this
9292
*/
9393
public function addStyles($assets)
@@ -100,14 +100,14 @@ public function addStyles($assets)
100100
/**
101101
* Add styles directly.
102102
*
103-
* @param array|string $assets
103+
* @param array|string $assets
104104
* @return $this
105105
*/
106106
public function addStylesDirectly($assets)
107107
{
108108
foreach ((array)$assets as &$item) {
109109
$item = ltrim(trim($item), '/');
110-
110+
111111
if (!in_array($item, $this->appendedStyles)) {
112112
$this->appendedStyles[$item] = [
113113
'src' => $item,
@@ -122,8 +122,8 @@ public function addStylesDirectly($assets)
122122
/**
123123
* Add scripts directly.
124124
*
125-
* @param string|array $assets
126-
* @param string $location
125+
* @param string|array $assets
126+
* @param string $location
127127
* @return $this
128128
*/
129129
public function addScriptsDirectly($assets, $location = self::ASSETS_SCRIPT_POSITION_FOOTER)
@@ -145,7 +145,7 @@ public function addScriptsDirectly($assets, $location = self::ASSETS_SCRIPT_POSI
145145
/**
146146
* Remove Css to current module.
147147
*
148-
* @param array $assets
148+
* @param array $assets
149149
* @return $this
150150
*/
151151
public function removeStyles($assets)
@@ -169,7 +169,7 @@ public function removeStyles($assets)
169169
/**
170170
* Add scripts.
171171
*
172-
* @param array $assets
172+
* @param array $assets
173173
* @return $this
174174
*/
175175
public function removeScripts($assets)
@@ -190,10 +190,33 @@ public function removeScripts($assets)
190190
return $this;
191191
}
192192

193+
/**
194+
* Remove script/style items directly.
195+
*
196+
* @param array|string $assets
197+
* @param string|null $location `header` or `footer`
198+
* @return $this
199+
*/
200+
public function removeItemDirectly($assets, ?string $location = null)
201+
{
202+
foreach ((array)$assets as $item) {
203+
$item = ltrim(trim($item), '/');
204+
205+
if ($location && in_array($location, [self::ASSETS_SCRIPT_POSITION_HEADER, self::ASSETS_SCRIPT_POSITION_FOOTER])) {
206+
Arr::forget($this->appendedScripts[$location], $item);
207+
} else {
208+
Arr::forget($this->appendedScripts[self::ASSETS_SCRIPT_POSITION_HEADER], $item);
209+
Arr::forget($this->appendedScripts[self::ASSETS_SCRIPT_POSITION_FOOTER], $item);
210+
}
211+
}
212+
213+
return $this;
214+
}
215+
193216
/**
194217
* Get All scripts in current module.
195218
*
196-
* @param string $location `header` or `footer`
219+
* @param string $location `header` or `footer`
197220
* @return array
198221
*/
199222
public function getScripts($location = null)
@@ -218,7 +241,7 @@ public function getScripts($location = null)
218241
/**
219242
* Get All CSS in current module.
220243
*
221-
* @param array $lastStyles Append last CSS to current module
244+
* @param array $lastStyles Append last CSS to current module
222245
* @return array
223246
*/
224247
public function getStyles($lastStyles = [])
@@ -242,8 +265,8 @@ public function getStyles($lastStyles = [])
242265
/**
243266
* Convert script to html.
244267
*
245-
* @param string $name
246-
* @return string|null
268+
* @param string $name
269+
* @return string|null
247270
*/
248271
public function scriptToHtml($name)
249272
{
@@ -253,7 +276,7 @@ public function scriptToHtml($name)
253276
/**
254277
* Convert style to html.
255278
*
256-
* @param string $name
279+
* @param string $name
257280
*/
258281
public function styleToHtml($name)
259282
{
@@ -263,9 +286,9 @@ public function styleToHtml($name)
263286
/**
264287
* Get script item.
265288
*
266-
* @param string $location
267-
* @param string $configName
268-
* @param string $script
289+
* @param string $location
290+
* @param string $configName
291+
* @param string $script
269292
* @return array
270293
*/
271294
protected function getScriptItem($location, $configName, $script)
@@ -282,8 +305,8 @@ protected function getScriptItem($location, $configName, $script)
282305
/**
283306
* Convert item to html.
284307
*
285-
* @param string $name
286-
* @param string $type
308+
* @param string $name
309+
* @param string $type
287310
* @return string
288311
*/
289312
protected function itemToHtml($name, $type = 'style')
@@ -310,7 +333,7 @@ protected function itemToHtml($name, $type = 'style')
310333
}
311334

312335
/**
313-
* @param string $configName
336+
* @param string $configName
314337
* @return string|array
315338
*/
316339
protected function getSourceUrl($configName)
@@ -329,7 +352,7 @@ protected function getSourceUrl($configName)
329352
}
330353

331354
/**
332-
* @param string $configName
355+
* @param string $configName
333356
* @return bool
334357
*/
335358
protected function isUsingCdn($configName)
@@ -338,8 +361,8 @@ protected function isUsingCdn($configName)
338361
}
339362

340363
/**
341-
* @param string $configName
342-
* @param string $location
364+
* @param string $configName
365+
* @param string $location
343366
* @return array
344367
*/
345368
protected function getSource($configName, $location = null)
@@ -396,8 +419,9 @@ public function getHtmlBuilder()
396419
/**
397420
* Render assets to header.
398421
*
399-
* @param array $lastStyles
422+
* @param array $lastStyles
400423
* @return string
424+
*
401425
* @throws \Throwable
402426
*/
403427
public function renderHeader($lastStyles = [])
@@ -413,6 +437,7 @@ public function renderHeader($lastStyles = [])
413437
* Render assets to footer.
414438
*
415439
* @return string
440+
*
416441
* @throws \Throwable
417442
*/
418443
public function renderFooter()

0 commit comments

Comments
 (0)