Skip to content

Commit dd97a3e

Browse files
committed
Added support for adding assets at the beginning of the queue
1 parent 109e62b commit dd97a3e

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed

API.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,23 @@ You may add more than one asset passing an array as argument.
292292

293293

294294

295+
### prepend
296+
297+
Assets prepend(mixed $asset)
298+
299+
Add an asset or a collection of assets to the beginning of the queue.
300+
301+
It automatically detects the asset type (JavaScript, CSS or collection).
302+
You may prepend more than one asset passing an array as argument.
303+
304+
* Visibility: **public**
305+
306+
307+
#### Arguments
308+
* $asset **mixed**
309+
310+
311+
295312
### addCss
296313

297314
Assets addCss(mixed $asset)
@@ -309,6 +326,23 @@ You may add more than one asset passing an array as argument.
309326

310327

311328

329+
### prependCss
330+
331+
Assets prependCss(mixed $asset)
332+
333+
Add a CSS asset to the beginning of the queue.
334+
335+
It checks for duplicates.
336+
You may prepend more than one asset passing an array as argument.
337+
338+
* Visibility: **public**
339+
340+
341+
#### Arguments
342+
* $asset **mixed**
343+
344+
345+
312346
### addJs
313347

314348
Assets addJs(mixed $asset)
@@ -326,6 +360,23 @@ You may add more than one asset passing an array as argument.
326360

327361

328362

363+
### prependJs
364+
365+
Assets prependJs(mixed $asset)
366+
367+
Add a JavaScript asset to the beginning of the queue.
368+
369+
It checks for duplicates.
370+
You may prepend more than one asset passing an array as argument.
371+
372+
* Visibility: **public**
373+
374+
375+
#### Arguments
376+
* $asset **mixed**
377+
378+
379+
329380
### css
330381

331382
string css(array|\Closure $attributes)

src/Manager.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,39 @@ public function add($asset)
255255
return $this;
256256
}
257257

258+
/**
259+
* Add an asset or a collection of assets to the beginning of the queue.
260+
*
261+
* It automatically detects the asset type (JavaScript, CSS or collection).
262+
* You may prepend more than one asset passing an array as argument.
263+
*
264+
* @param mixed $asset
265+
* @return Manager
266+
*/
267+
public function prepend($asset)
268+
{
269+
// More than one asset
270+
if(is_array($asset))
271+
{
272+
foreach(array_reverse($asset) as $a)
273+
$this->prepend($a);
274+
}
275+
276+
// Collection
277+
elseif(isset($this->collections[$asset]))
278+
$this->prepend($this->collections[$asset]);
279+
280+
// JavaScript asset
281+
elseif(preg_match($this->js_regex, $asset))
282+
$this->prependJs($asset);
283+
284+
// CSS asset
285+
elseif(preg_match($this->css_regex, $asset))
286+
$this->prependCss($asset);
287+
288+
return $this;
289+
}
290+
258291
/**
259292
* Add a CSS asset.
260293
*
@@ -283,6 +316,34 @@ public function addCss($asset)
283316
return $this;
284317
}
285318

319+
/**
320+
* Add a CSS asset to the beginning of the queue.
321+
*
322+
* It checks for duplicates.
323+
* You may prepend more than one asset passing an array as argument.
324+
*
325+
* @param mixed $asset
326+
* @return Manager
327+
*/
328+
public function prependCss($asset)
329+
{
330+
if(is_array($asset))
331+
{
332+
foreach(array_reverse($asset) as $a)
333+
$this->prependCss($a);
334+
335+
return $this;
336+
}
337+
338+
if( ! $this->isRemoteLink($asset))
339+
$asset = $this->buildLocalLink($asset, $this->css_dir);
340+
341+
if( ! in_array($asset, $this->css))
342+
array_unshift($this->css, $asset);
343+
344+
return $this;
345+
}
346+
286347
/**
287348
* Add a JavaScript asset.
288349
*
@@ -311,6 +372,34 @@ public function addJs($asset)
311372
return $this;
312373
}
313374

375+
/**
376+
* Add a JavaScript asset to the beginning of the queue.
377+
*
378+
* It checks for duplicates.
379+
* You may prepend more than one asset passing an array as argument.
380+
*
381+
* @param mixed $asset
382+
* @return Manager
383+
*/
384+
public function prependJs($asset)
385+
{
386+
if(is_array($asset))
387+
{
388+
foreach(array_reverse($asset) as $a)
389+
$this->prependJs($a);
390+
391+
return $this;
392+
}
393+
394+
if( ! $this->isRemoteLink($asset))
395+
$asset = $this->buildLocalLink($asset, $this->js_dir);
396+
397+
if( ! in_array($asset, $this->js))
398+
array_unshift($this->js, $asset);
399+
400+
return $this;
401+
}
402+
314403
/**
315404
* Build the CSS `<link>` tags.
316405
*

tests/AssetsManagerTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ public function testAddOneCss()
7373
$this->assertCount(0, $assets);
7474
}
7575

76+
public function testPrependOneCss()
77+
{
78+
$this->assertCount(0, $this->manager->getCss());
79+
80+
$asset1 = uniqid('asset1');
81+
$asset2 = uniqid('asset2');
82+
$this->manager->addCss($asset2);
83+
$this->manager->prependCss($asset1);
84+
85+
$assets = $this->manager->getCss();
86+
$this->assertStringEndsWith($asset2, array_pop($assets));
87+
$this->assertStringEndsWith($asset1, array_pop($assets));
88+
$this->assertCount(0, $assets);
89+
}
90+
7691
public function testAddOneJs()
7792
{
7893
$this->assertCount(0, $this->manager->getJs());
@@ -86,6 +101,21 @@ public function testAddOneJs()
86101
$this->assertCount(0, $assets);
87102
}
88103

104+
public function testPrependOneJs()
105+
{
106+
$this->assertCount(0, $this->manager->getJs());
107+
108+
$asset1 = uniqid('asset1');
109+
$asset2 = uniqid('asset2');
110+
$this->manager->addJs($asset2);
111+
$this->manager->prependJs($asset1);
112+
113+
$assets = $this->manager->getJs();
114+
$this->assertStringEndsWith($asset2, array_pop($assets));
115+
$this->assertStringEndsWith($asset1, array_pop($assets));
116+
$this->assertCount(0, $assets);
117+
}
118+
89119
public function testAddMultipleCss()
90120
{
91121
$this->assertCount(0, $this->manager->getCss());
@@ -101,6 +131,24 @@ public function testAddMultipleCss()
101131
$this->assertCount(0, $assets);
102132
}
103133

134+
public function testPrependMultipleCss()
135+
{
136+
$this->assertCount(0, $this->manager->getCss());
137+
138+
$asset1 = uniqid('asset1');
139+
$asset2 = uniqid('asset2');
140+
$asset3 = uniqid('asset3');
141+
$this->manager->addCss($asset3);
142+
$this->manager->prependCss(array($asset1, $asset2));
143+
$assets = $this->manager->getCss();
144+
145+
$this->assertCount(3, $assets);
146+
$this->assertStringEndsWith($asset3, array_pop($assets));
147+
$this->assertStringEndsWith($asset2, array_pop($assets));
148+
$this->assertStringEndsWith($asset1, array_pop($assets));
149+
$this->assertCount(0, $assets);
150+
}
151+
104152
public function testAddMultipleJs()
105153
{
106154
$this->assertCount(0, $this->manager->getJs());
@@ -116,6 +164,24 @@ public function testAddMultipleJs()
116164
$this->assertCount(0, $assets);
117165
}
118166

167+
public function testPrependMultipleJs()
168+
{
169+
$this->assertCount(0, $this->manager->getJs());
170+
171+
$asset1 = uniqid('asset1');
172+
$asset2 = uniqid('asset2');
173+
$asset3 = uniqid('asset3');
174+
$this->manager->addJs($asset3);
175+
$this->manager->prependJs(array($asset1, $asset2));
176+
$assets = $this->manager->getJs();
177+
178+
$this->assertCount(3, $assets);
179+
$this->assertStringEndsWith($asset3, array_pop($assets));
180+
$this->assertStringEndsWith($asset2, array_pop($assets));
181+
$this->assertStringEndsWith($asset1, array_pop($assets));
182+
$this->assertCount(0, $assets);
183+
}
184+
119185
public function testDetectAndAddCss()
120186
{
121187
$this->assertCount(0, $this->manager->getCss());

0 commit comments

Comments
 (0)