Skip to content

Commit

Permalink
Support parse asset tag via middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
puleeno committed Sep 14, 2023
1 parent 0caeee8 commit 83db0a7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/Constracts/MiddlewareConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Constracts;

use Psr\Http\Server\MiddlewareInterface;

interface MiddlewareConstract extends MiddlewareInterface
{
public function getPriority(): int;
}
62 changes: 62 additions & 0 deletions app/Http/Middleware/AssetsMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Http\Middleware;

use App\Constracts\MiddlewareConstract;
use App\Core\HookManager;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Response;

class AssetsMiddleware implements MiddlewareConstract
{
public function getPriority(): int
{
return 9999;
}

protected function getHeadAssets()
{
ob_start();
HookManager::executeAction('head');
return ob_get_clean();
}

protected function getOpenBodyTags()
{
ob_start();
HookManager::executeAction('start_body');
return ob_get_clean();
}

protected function getFooterAssets()
{
ob_start();
HookManager::executeAction('footer');
return ob_get_clean();
}

// Replace template tags by action hooks
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

$template = (string) $response->getBody();

$template = str_replace([
'<!--asset:head-->',
'<!--asset:start_body-->',
'<!--asset:footer-->'
], [
$this->getHeadAssets(),
$this->getOpenBodyTags(),
$this->getFooterAssets()
], $template);

$response = new Response();
$response->getBody()->write($template);

return $response;
}
}
2 changes: 2 additions & 0 deletions configs/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
declare(strict_types=1);

use App\Core\Application;
use App\Http\Middleware\AssetsMiddleware;
use App\Http\Middleware\SessionMiddleware;

return function (Application $app) {
$app->add(SessionMiddleware::class);
$app->add(AssetsMiddleware::class);
};
2 changes: 1 addition & 1 deletion extensions/dashboard
2 changes: 1 addition & 1 deletion extensions/react
Submodule react updated from 126082 to aeaa4a

0 comments on commit 83db0a7

Please sign in to comment.