Skip to content

Commit

Permalink
fixing injection middleware with array_map and explode, implode
Browse files Browse the repository at this point in the history
  • Loading branch information
devajmeireles committed Sep 17, 2024
1 parent 1da847a commit beaf428
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ parameters:
paths:
- src/
level: 5
ignoreErrors:
- '#Parameter \#2 \$replace of function substr_replace expects array\|string, Illuminate\\Contracts\\Support\\Htmlable given.#'
- '#Parameter \#2 \$replace of function substr_replace expects array\|string, Illuminate\\Contracts\\View\\View given.#'
18 changes: 14 additions & 4 deletions src/Response/ResponseHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ public function __invoke(): Response
$content = $this->response->getContent();

if (($head = strpos($content, '<head>')) !== false) {
$content = substr_replace($content, $this->render->css(), $head + 6, 0);
$content = substr_replace($content, $this->render->js(), $head + 6, 0);
$content = substr_replace($content, $this->render->css(), $head + 6, 0); // @phpstan-ignore-line
$content = substr_replace($content, $this->render->js(), $head + 6, 0); // @phpstan-ignore-line
}

$content = preg_replace('/(<body[^>]*>)/i', '$1'.$this->render->component(), $content);
// Although this code is not the best, it was the only viable way found, at least for now,
// to insert the envbar in the correct place, after <body>, for situations where we have things
// like "x-on:redirect="setTimeout(() => window.location.href = $event.detail.route, $event.detail.time ?? 3000)">"
// inside the <body>, things with symbols that mess up regex.
$explode = array_map(function (string $line): string {
if (str_contains($line, '<body')) {
return $line.$this->render->component(); // @phpstan-ignore-line
}

return $this->response->setContent($content);
return $line;
}, explode("\n", $content));

return $this->response->setContent(implode("\n", $explode));
}
}

0 comments on commit beaf428

Please sign in to comment.