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 650f11e
Showing 1 changed file with 36 additions and 52 deletions.
88 changes: 36 additions & 52 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,23 +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,
'dependency' => array()
'namespace' => $namespace
);

$this->collection->put($type, $collection);
Expand All @@ -190,44 +175,43 @@ 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) {
if (isset($value['dependency'])) {
$dependencyList[$key] = array($this->getNamespacedAsset($value['dependency'], $type));
} else {
$dependencyList[$key] = null;
}
$dependencyList[$key] = [
isset($value['dependency']) ? $this->getNamespacedAsset($value['dependency'], $type) : null
];
}

$dependencies = new Dependencies($dependencyList, true);
Expand Down Expand Up @@ -268,7 +252,7 @@ protected function getNamespacedAsset($namespace, $type)
$collection = $this->collection->get($type);

foreach ($collection as $key => $value) {
if ($value['namespace'] === $namespace) {
if (($value['namespace'] ?? null) === $namespace) {
return $key;
}
}
Expand Down

0 comments on commit 650f11e

Please sign in to comment.