Skip to content

Commit

Permalink
Added support for adding assets at the beginning of the queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Stolz committed Nov 1, 2017
1 parent 109e62b commit dd97a3e
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 0 deletions.
51 changes: 51 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,23 @@ You may add more than one asset passing an array as argument.



### prepend

Assets prepend(mixed $asset)

Add an asset or a collection of assets to the beginning of the queue.

It automatically detects the asset type (JavaScript, CSS or collection).
You may prepend more than one asset passing an array as argument.

* Visibility: **public**


#### Arguments
* $asset **mixed**



### addCss

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



### prependCss

Assets prependCss(mixed $asset)

Add a CSS asset to the beginning of the queue.

It checks for duplicates.
You may prepend more than one asset passing an array as argument.

* Visibility: **public**


#### Arguments
* $asset **mixed**



### addJs

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



### prependJs

Assets prependJs(mixed $asset)

Add a JavaScript asset to the beginning of the queue.

It checks for duplicates.
You may prepend more than one asset passing an array as argument.

* Visibility: **public**


#### Arguments
* $asset **mixed**



### css

string css(array|\Closure $attributes)
Expand Down
89 changes: 89 additions & 0 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,39 @@ public function add($asset)
return $this;
}

/**
* Add an asset or a collection of assets to the beginning of the queue.
*
* It automatically detects the asset type (JavaScript, CSS or collection).
* You may prepend more than one asset passing an array as argument.
*
* @param mixed $asset
* @return Manager
*/
public function prepend($asset)
{
// More than one asset
if(is_array($asset))
{
foreach(array_reverse($asset) as $a)
$this->prepend($a);
}

// Collection
elseif(isset($this->collections[$asset]))
$this->prepend($this->collections[$asset]);

// JavaScript asset
elseif(preg_match($this->js_regex, $asset))
$this->prependJs($asset);

// CSS asset
elseif(preg_match($this->css_regex, $asset))
$this->prependCss($asset);

return $this;
}

/**
* Add a CSS asset.
*
Expand Down Expand Up @@ -283,6 +316,34 @@ public function addCss($asset)
return $this;
}

/**
* Add a CSS asset to the beginning of the queue.
*
* It checks for duplicates.
* You may prepend more than one asset passing an array as argument.
*
* @param mixed $asset
* @return Manager
*/
public function prependCss($asset)
{
if(is_array($asset))
{
foreach(array_reverse($asset) as $a)
$this->prependCss($a);

return $this;
}

if( ! $this->isRemoteLink($asset))
$asset = $this->buildLocalLink($asset, $this->css_dir);

if( ! in_array($asset, $this->css))
array_unshift($this->css, $asset);

return $this;
}

/**
* Add a JavaScript asset.
*
Expand Down Expand Up @@ -311,6 +372,34 @@ public function addJs($asset)
return $this;
}

/**
* Add a JavaScript asset to the beginning of the queue.
*
* It checks for duplicates.
* You may prepend more than one asset passing an array as argument.
*
* @param mixed $asset
* @return Manager
*/
public function prependJs($asset)
{
if(is_array($asset))
{
foreach(array_reverse($asset) as $a)
$this->prependJs($a);

return $this;
}

if( ! $this->isRemoteLink($asset))
$asset = $this->buildLocalLink($asset, $this->js_dir);

if( ! in_array($asset, $this->js))
array_unshift($this->js, $asset);

return $this;
}

/**
* Build the CSS `<link>` tags.
*
Expand Down
66 changes: 66 additions & 0 deletions tests/AssetsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ public function testAddOneCss()
$this->assertCount(0, $assets);
}

public function testPrependOneCss()
{
$this->assertCount(0, $this->manager->getCss());

$asset1 = uniqid('asset1');
$asset2 = uniqid('asset2');
$this->manager->addCss($asset2);
$this->manager->prependCss($asset1);

$assets = $this->manager->getCss();
$this->assertStringEndsWith($asset2, array_pop($assets));
$this->assertStringEndsWith($asset1, array_pop($assets));
$this->assertCount(0, $assets);
}

public function testAddOneJs()
{
$this->assertCount(0, $this->manager->getJs());
Expand All @@ -86,6 +101,21 @@ public function testAddOneJs()
$this->assertCount(0, $assets);
}

public function testPrependOneJs()
{
$this->assertCount(0, $this->manager->getJs());

$asset1 = uniqid('asset1');
$asset2 = uniqid('asset2');
$this->manager->addJs($asset2);
$this->manager->prependJs($asset1);

$assets = $this->manager->getJs();
$this->assertStringEndsWith($asset2, array_pop($assets));
$this->assertStringEndsWith($asset1, array_pop($assets));
$this->assertCount(0, $assets);
}

public function testAddMultipleCss()
{
$this->assertCount(0, $this->manager->getCss());
Expand All @@ -101,6 +131,24 @@ public function testAddMultipleCss()
$this->assertCount(0, $assets);
}

public function testPrependMultipleCss()
{
$this->assertCount(0, $this->manager->getCss());

$asset1 = uniqid('asset1');
$asset2 = uniqid('asset2');
$asset3 = uniqid('asset3');
$this->manager->addCss($asset3);
$this->manager->prependCss(array($asset1, $asset2));
$assets = $this->manager->getCss();

$this->assertCount(3, $assets);
$this->assertStringEndsWith($asset3, array_pop($assets));
$this->assertStringEndsWith($asset2, array_pop($assets));
$this->assertStringEndsWith($asset1, array_pop($assets));
$this->assertCount(0, $assets);
}

public function testAddMultipleJs()
{
$this->assertCount(0, $this->manager->getJs());
Expand All @@ -116,6 +164,24 @@ public function testAddMultipleJs()
$this->assertCount(0, $assets);
}

public function testPrependMultipleJs()
{
$this->assertCount(0, $this->manager->getJs());

$asset1 = uniqid('asset1');
$asset2 = uniqid('asset2');
$asset3 = uniqid('asset3');
$this->manager->addJs($asset3);
$this->manager->prependJs(array($asset1, $asset2));
$assets = $this->manager->getJs();

$this->assertCount(3, $assets);
$this->assertStringEndsWith($asset3, array_pop($assets));
$this->assertStringEndsWith($asset2, array_pop($assets));
$this->assertStringEndsWith($asset1, array_pop($assets));
$this->assertCount(0, $assets);
}

public function testDetectAndAddCss()
{
$this->assertCount(0, $this->manager->getCss());
Expand Down

0 comments on commit dd97a3e

Please sign in to comment.