diff --git a/API.md b/API.md index 59e9d0a..fb64d33 100644 --- a/API.md +++ b/API.md @@ -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) @@ -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) @@ -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) diff --git a/src/Manager.php b/src/Manager.php index f7e6245..9658e67 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -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. * @@ -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. * @@ -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 `` tags. * diff --git a/tests/AssetsManagerTest.php b/tests/AssetsManagerTest.php index 751ceaa..bc96347 100644 --- a/tests/AssetsManagerTest.php +++ b/tests/AssetsManagerTest.php @@ -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()); @@ -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()); @@ -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()); @@ -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());