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 Apr 7, 2022
1 parent 466603f commit aba8e79
Showing 1 changed file with 32 additions and 45 deletions.
77 changes: 32 additions & 45 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 (! $dependency || ! $this->lastAddedAsset) {
return $this;
}

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

foreach ($collection as $path => $item) {
Expand All @@ -82,7 +66,7 @@ public function dependsOn($dependency)
*/
public function css()
{
$cssCollection = $this->sortDependencies($this->collection->get('css'), 'css');
$cssCollection = $this->sortDependencies('css');
$output = '';

foreach ($cssCollection as $key => $value) {
Expand All @@ -99,7 +83,7 @@ public function css()
*/
public function js()
{
$jsCollection = $this->sortDependencies($this->collection->get('js'), 'js');
$jsCollection = $this->sortDependencies('js');
$output = '';

foreach ($jsCollection as $key => $value) {
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,7 +134,7 @@ 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));
}

/**
Expand All @@ -159,22 +143,24 @@ protected function isBonsai($asset)
* @param array|string $assets
* @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 @@ -190,36 +176,37 @@ protected function addAsset($assets, $namespace = null)
/**
* Parse a bonsai.json file and add the assets to the collection.
*
* @param string $path
* @param string|array $assets
* @return Assets
*/
protected function parseBonsai($path)
protected function parseBonsai($assets)
{
$file = file_get_contents($path);
$assets = json_decode($file, true);
if (is_string($assets)) {
$file = file_get_contents($assets);
$assets = json_decode($file, true) ?: [];
}

foreach ($assets as $path => $meta) {
$namespace = (isset($meta['namespace'])) ? $meta['namespace'] : null;

$asset = $this->addAsset($path, $namespace);
$path = is_numeric($path) ? $meta : $path;
$meta = is_array($meta) ? $meta : [];

if (isset($meta['dependency'])) {
$asset->dependsOn($meta['dependency']);
}
$this->add($path, $meta['namespace'] ?? null)
->dependsOn($meta['dependency'] ?? null);
}

return;
return $this;
}

/**
* Sorts the dependencies of all assets, to ensure dependant
* assets are loaded first.
*
* @param array $assets
* @param string $type
* @return array
*/
protected function sortDependencies($assets = array(), $type = null)
protected function sortDependencies($type)
{
$assets = $this->collection->get($type);
$dependencyList = array();

foreach ($assets as $key => $value) {
Expand Down

0 comments on commit aba8e79

Please sign in to comment.