Skip to content

Commit

Permalink
Fix dependsOn bug when string is not an asset
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Mar 9, 2022
1 parent 466603f commit affd3ef
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,17 @@ 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.
*
* @return Asset
*/
public function dependsOn($dependency)
{
if (empty($this->lastAddedAsset)) {
return $this;
}

$collection = $this->collection->get($this->lastAddedType);

foreach ($collection as $path => $item) {
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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()
);

Expand All @@ -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;
}

/**
Expand Down

0 comments on commit affd3ef

Please sign in to comment.