Skip to content

Commit

Permalink
feat: change route in service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Feb 25, 2024
1 parent 1af0425 commit 55538e8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
1 change: 0 additions & 1 deletion app/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function middlewares(): array
\App\Middleware\CorsMiddleware::class,
\App\Middleware\XSSMiddleware::class,
\App\Middleware\GzipMiddleware::class,
\App\Middleware\CsrfMiddleware::class
];
}

Expand Down
4 changes: 2 additions & 2 deletions app/Middleware/CorsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function handle(Request $request, Closure $next)
$header = respond()->getHeader();
$header->set('Access-Control-Allow-Origin', '*');

$vary = (!$header->has('Vary')) ? [] : explode(', ', $header->get('Vary'));
$vary = $header->has('Vary') ? explode(', ', $header->get('Vary')) : [];
$vary = array_unique([...$vary, 'Accept', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Origin', 'User-Agent']);
$header->set('Vary', join(', ', $vary));

Expand All @@ -39,7 +39,7 @@ public function handle(Request $request, Closure $next)

$header->set(
'Access-Control-Allow-Headers',
$request->server->get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', 'Accept, Authorization, Content-Type, Origin, Token, User-Agent')
$request->server->get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', 'Accept, Authorization, Content-Type, Origin, User-Agent')
);

return respond()->setCode(Respond::HTTP_NO_CONTENT);
Expand Down
6 changes: 3 additions & 3 deletions app/Middleware/GzipMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ public function handle(Request $request, Closure $next): Stream|Respond
return $response;
}

if (!str_contains($request->server->get('HTTP_ACCEPT_ENCODING'), 'gzip')) {
if (!str_contains($request->server->get('HTTP_ACCEPT_ENCODING', ''), 'gzip')) {
return $response;
}

$compressed = gzencode($response->getContent(false), 1);
$compressed = gzencode($response->getContent(false), 3);

if ($compressed === false) {
return $response;
}

$response->setContent($compressed);

$vary = (!$response->headers->has('Vary')) ? [] : explode(', ', $response->headers->get('Vary'));
$vary = $response->headers->has('Vary') ? explode(', ', $response->headers->get('Vary')) : [];
$vary = array_unique([...$vary, 'Accept-Encoding']);

$response->headers
Expand Down
8 changes: 7 additions & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Middleware\CsrfMiddleware;
use Core\Facades\Provider;
use Core\Routing\Route;
use Core\Routing\Router;
Expand All @@ -16,6 +17,11 @@ class RouteServiceProvider extends Provider
public function booting()
{
$this->app->singleton(Router::class);
Route::setRouteFromCacheIfExist();

if (!Route::setRouteFromCacheIfExist()) {
Route::middleware(CsrfMiddleware::class)->group(function () {
Route::setRouteFromFile();
});
}
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"kamu/framework": "^3.2"
"kamu/framework": "^3.3"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand All @@ -35,4 +35,4 @@
},
"minimum-stability": "stable",
"prefer-stable": true
}
}

0 comments on commit 55538e8

Please sign in to comment.