Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Aug 16, 2024
1 parent 1bb2ead commit d7145dd
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/InjectAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

class InjectAssets
{
public function __construct(
protected AssetInjector $injector
) {}

/** Injects assets inside every full-page response */
public function __invoke(RequestHandled $handled)
{
$injector = $this->resolveInjector();

if (! $injector->enabled()) {
if (! $this->injector->enabled()) {
return;
}

Expand All @@ -28,7 +30,7 @@ public function __invoke(RequestHandled $handled)
}

// Skip if core was included before
if (str_contains($html, '<!--[{$injector->identifier()} ASSETS]-->')) {
if (str_contains($html, '<!--[{$this->injector->identifier()} ASSETS]-->')) {
return;
}

Expand All @@ -37,20 +39,15 @@ public function __invoke(RequestHandled $handled)

$handled->response->setContent(
$this->injectAssets($html, <<< HTML
<!--[{$injector->identifier()} ASSETS]-->
{$injector->inject()}
<!--[END{$injector->identifier()}]-->
<!--[{$this->injector->identifier()} ASSETS]-->
{$this->injector->inject()}
<!--[END{$this->injector->identifier()}]-->
HTML)
);

$handled->response->original = $originalContent;
}

protected function resolveInjector(): AssetInjector
{
return resolve(AssetInjector::class);
}

/** Injects assets into given html string (taken from Livewire's injection mechanism) */
protected function injectAssets(string $html, string $core): string
{
Expand Down

0 comments on commit d7145dd

Please sign in to comment.