diff --git a/src/Assets.php b/src/Assets.php index 9e8af27..d4a3897 100644 --- a/src/Assets.php +++ b/src/Assets.php @@ -32,26 +32,6 @@ public function __construct() $this->collection = new Collection(['css' => array(), 'js' => array()]); } - /** - * Add a new asset to the Bonsai collection. - * - * @return Asset - */ - public function add($assets, $namespace = null) - { - if (is_array($assets)) { - foreach ($assets as $asset) { - $this->add($asset, $namespace); - } - } elseif ($this->isBonsai($assets)) { - $this->parseBonsai($assets); - } elseif ($this->isAsset($assets)) { - $this->addAsset($assets, $namespace); - } - - return $this; - } - /** * Add a dependency to an asset. * @@ -59,6 +39,10 @@ public function add($assets, $namespace = null) */ public function dependsOn($dependency) { + if (empty($this->lastAddedAsset)) { + return $this; + } + $collection = $this->collection->get($this->lastAddedType); foreach ($collection as $path => $item) { @@ -128,7 +112,7 @@ protected function isAsset($asset) */ protected function isJs($asset) { - return stripos($asset, '.js') !== false; + return is_string($asset) && stripos($asset, '.js') !== false; } /** @@ -139,7 +123,7 @@ protected function isJs($asset) */ protected function isCss($asset) { - return stripos($asset, '.css') !== false; + return is_string($asset) && stripos($asset, '.css') !== false; } /** @@ -150,31 +134,34 @@ protected function isCss($asset) */ protected function isBonsai($asset) { - return preg_match('/bonsai\.json$/i', $asset); + return is_array($asset) || (is_string($asset) && preg_match('/bonsai\.json$/i', $asset)); } /** * Add an asset file to the collection. * * @param array|string $assets + * @param null|string $namespace * @return Assets */ - protected function addAsset($assets, $namespace = null) + public function add($assets, $namespace = null) { - if (is_array($assets)) { - foreach ($assets as $asset => $meta) { - $this->addAsset($asset); - } + if ($this->isBonsai($assets)) { + return $this->parseBonsai($assets); + } + + if (! $this->isAsset($assets)) { + $this->lastAddedAsset = ''; return $this; } - $type = ($this->isCss($assets)) ? 'css' : 'js'; + $type = $this->isCss($assets) ? 'css' : 'js'; $collection = $this->collection->get($type); if (! in_array($assets, $collection)) { $collection[$assets] = array( - 'namespace' => $namespace, + 'namespace' => $namespace ?: null, 'dependency' => array() ); @@ -195,20 +182,31 @@ protected function addAsset($assets, $namespace = null) */ protected function parseBonsai($path) { - $file = file_get_contents($path); - $assets = json_decode($file, true); + if (is_array($path)) { + $assets = $path; + } else { + $file = file_get_contents($path); + $assets = json_decode($file, true) ?: []; + } foreach ($assets as $path => $meta) { - $namespace = (isset($meta['namespace'])) ? $meta['namespace'] : null; + if (is_numeric($path)) { + $path = $meta; + } + if (! is_array($meta)) { + $meta = []; + } + + $namespace = $meta['namespace'] ?? null; - $asset = $this->addAsset($path, $namespace); + $asset = $this->add($path, $namespace); if (isset($meta['dependency'])) { $asset->dependsOn($meta['dependency']); } } - return; + return $this; } /**