Skip to content

Commit

Permalink
feat: AssetManager
Browse files Browse the repository at this point in the history
getAssets -> assets
  • Loading branch information
lee-to committed Nov 29, 2024
1 parent 2135c30 commit bdb4b61
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
29 changes: 20 additions & 9 deletions src/Core/src/Traits/WithAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MoonShine\Core\Traits;

use MoonShine\Contracts\AssetManager\AssetElementContract;
use MoonShine\Contracts\AssetManager\AssetManagerContract;

/**
Expand All @@ -21,28 +22,38 @@ public function getAssetManager(): AssetManagerContract
return $this->getCore()->getContainer(AssetManagerContract::class);
}

/**
* @return list<AssetElementContract>
*/
protected function assets(): array
{
return [];
}

/**
* @return list<AssetElementContract>
*/
public function getAssets(): array
{
if (! $this->shouldUseAssets()) {
return [];
}

return $this->assets;
}

public function addAssets(array $assets): static
{
$this->getAssetManager()->add($assets);

return $this;
return array_merge(
$this->assets,
$this->assets(),
);
}

protected function shouldUseAssets(): bool
{
return true;
}

public function pushAssets(array $assets): static
/**
* @param list<AssetElementContract> $assets
*/
public function addAssets(array $assets): static
{
$this->assets = array_merge($this->assets, $assets);

Expand Down
2 changes: 1 addition & 1 deletion src/UI/src/Components/Layout/Favicon.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
* web-manifest: string,
* } $assets
*/
public function assets(array $assets): self
public function customAssets(array $assets): self
{
$this->customAssets = $assets;

Expand Down
9 changes: 8 additions & 1 deletion src/UI/src/Components/MoonShineComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function __construct(
if (! $this->isConsoleMode()) {
$this->resolveAssets();
}

$this->booted();
}

protected function booted(): void
{
//
}

public static function consoleMode(bool $enable = true): void
Expand Down Expand Up @@ -71,7 +78,7 @@ public function withAttributes(array $attributes): static
/** @phpstan-ignore-next-line */
$this->attributes = $this->attributes ?: $this->newAttributeBag();
$this->attributes->setAttributes(
array_merge($this->attributes->jsonSerialize(), $attributes)
array_merge($this->attributes->jsonSerialize(), $attributes),
);

return $this;
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/ComponentsEqualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function renderBlade(string $alias, array $parameters = [], array $attributes =

it('favicon', function () {
compare(
Favicon::make()->assets(['test.js'])->bodyColor('#fff'),
Favicon::make()->customAssets(['test.js'])->bodyColor('#fff'),
['custom-assets' => ['test.js'], 'body-color' => '#fff']
);
});
Expand Down

0 comments on commit bdb4b61

Please sign in to comment.