Skip to content

Commit

Permalink
Merge pull request #9 from DigitallyHappy/fix-loading-asset-with-css-…
Browse files Browse the repository at this point in the history
…edinding-but-no-dot

fix loading asset with css ending but not dot
  • Loading branch information
tabacitu authored Nov 24, 2021
2 parents b7cb5cf + 44c0e98 commit 923185e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
26 changes: 13 additions & 13 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@

class AssetManager
{
public $loaded_assets;
public $loaded;

public function __construct()
{
$this->loaded_assets = [];
$this->loaded = [];
}

public function echoCss($path)
{
if ($this->isAssetLoaded($path)) {
if ($this->isLoaded($path)) {
return;
}

$this->markAssetAsLoaded($path);
$this->markAsLoaded($path);

echo '<link href="'.asset($path).'" rel="stylesheet" type="text/css" />';
}

public function echoJs($path)
{
if ($this->isAssetLoaded($path)) {
if ($this->isLoaded($path)) {
return;
}

$this->markAssetAsLoaded($path);
$this->markAsLoaded($path);

echo '<script src="'.asset($path).'"></script>';
}
Expand All @@ -39,10 +39,10 @@ public function echoJs($path)
* @param string $asset
* @return void
*/
public function markAssetAsLoaded($asset)
public function markAsLoaded($asset)
{
if (! $this->isAssetLoaded($asset)) {
$this->loaded_assets[] = $asset;
if (! $this->isLoaded($asset)) {
$this->loaded[] = $asset;
}
}

Expand All @@ -52,9 +52,9 @@ public function markAssetAsLoaded($asset)
* @param string $asset
* @return bool
*/
public function isAssetLoaded($asset)
public function isLoaded($asset)
{
if (in_array($asset, $this->loaded_assets)) {
if (in_array($asset, $this->loaded)) {
return true;
}

Expand All @@ -66,8 +66,8 @@ public function isAssetLoaded($asset)
*
* @return array
*/
public function loadedAssets()
public function loaded()
{
return $this->loaded_assets;
return $this->loaded;
}
}
22 changes: 8 additions & 14 deletions src/blade_directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,22 @@
// determine if it's a CSS or JS file
$cleanParameter = Str::of($parameter)->trim("'")->trim('"')->trim('`');
$filePath = Str::of($cleanParameter)->before('?')->before('#');
$extension = substr($filePath, -3);

// mey be useful to get the second parameter
// if (Str::contains($parameter, ',')) {
// $secondParameter = Str::of($parameter)->after(',')->trim(' ');
// }

switch ($extension) {
case 'css':
return "<?php Assets::echoCss({$parameter}); ?>";
break;

case '.js':
return "<?php Assets::echoJs({$parameter}); ?>";
break;

default:
// it's a block start
if (substr($filePath, -3) == '.js') {
return "<?php Assets::echoJs({$parameter}); ?>";
}

return "<?php if(! Assets::isAssetLoaded('".$cleanParameter."')) { Assets::markAssetAsLoaded('".$cleanParameter."'); ?>";
break;
if (substr($filePath, -4) == '.css') {
return "<?php Assets::echoCss({$parameter}); ?>";
}

// it's a block start
return "<?php if(! Assets::isLoaded('".$cleanParameter."')) { Assets::markAsLoaded('".$cleanParameter."'); ?>";
});

Blade::directive('endLoadOnce', function () {
Expand Down

0 comments on commit 923185e

Please sign in to comment.