diff --git a/app/Kernel.php b/app/Kernel.php index 6f8b300..7319b45 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -58,6 +58,7 @@ public function middlewares(): array return [ \App\Middleware\CorsMiddleware::class, \App\Middleware\XSSMiddleware::class, + \App\Middleware\GzipMiddleware::class, \App\Middleware\CsrfMiddleware::class ]; } diff --git a/app/Middleware/CorsMiddleware.php b/app/Middleware/CorsMiddleware.php index 527f193..af7dc1c 100644 --- a/app/Middleware/CorsMiddleware.php +++ b/app/Middleware/CorsMiddleware.php @@ -4,6 +4,7 @@ use Closure; use Core\Http\Request; +use Core\Http\Respond; use Core\Middleware\MiddlewareInterface; final class CorsMiddleware implements MiddlewareInterface @@ -16,17 +17,23 @@ public function handle(Request $request, Closure $next) $header = respond()->getHeader(); $header->set('Access-Control-Allow-Origin', '*'); - $header->set( - 'Vary', - 'Accept, Accept-Encoding, Access-Control-Request-Method, Access-Control-Request-Headers, Origin, User-Agent' - ); + + if ($header->has('Vary')) { + $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)); + } else { + $header->set('Vary', 'Accept, Access-Control-Request-Method, Access-Control-Request-Headers, Origin, User-Agent'); + } if (!$request->method(Request::OPTIONS)) { return $next($request); } + $header->unset('Content-Type'); + if (!$request->server->has('HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { - return respond()->setCode(204); + return respond()->setCode(Respond::HTTP_NO_CONTENT); } $header->set( @@ -39,6 +46,6 @@ public function handle(Request $request, Closure $next) $request->server->get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', 'Accept, Authorization, Content-Type, Origin, Token, User-Agent') ); - return respond()->setCode(204); + return respond()->setCode(Respond::HTTP_NO_CONTENT); } } diff --git a/app/Middleware/GzipMiddleware.php b/app/Middleware/GzipMiddleware.php new file mode 100644 index 0000000..4f53617 --- /dev/null +++ b/app/Middleware/GzipMiddleware.php @@ -0,0 +1,52 @@ +transform($response); + + if (env('GZIP', 'true') != 'true') { + return $response; + } + + if (!in_array('gzip', explode(', ', $request->server->get('HTTP_ACCEPT_ENCODING')))) { + return $response; + } + + $compressed = gzencode($response->getContent(false), 1); + + if ($compressed === false) { + return $response; + } + + $response->setContent($compressed); + + if ($response->headers->has('Vary')) { + $vary = explode(', ', $response->headers->get('Vary')); + $vary = array_unique([...$vary, 'Accept-Encoding']); + $response->headers->set('Vary', join(', ', $vary)); + } else { + $response->headers->set('Vary', 'Accept-Encoding'); + } + + $response->headers + ->set('Content-Encoding', 'gzip') + ->set('Content-Length', strlen($compressed)); + + return $response; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index b0cc8d5..71cb2d3 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,6 +3,7 @@ namespace App\Models; use Core\Model\Model; +use Core\Valid\Hash; final class User extends Model { @@ -22,4 +23,13 @@ final class User extends Model 'created_at', 'updated_at', ]; + + protected function fakes() + { + return [ + 'nama' => fake()->name(), + 'email' => fake()->email(), + 'password' => Hash::make(fake()->text(8)), + ]; + } } diff --git a/composer.json b/composer.json index 3b44c86..74afdaf 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,11 @@ "license": "MIT", "require": { "php": "^8.1", - "kamu/framework": "^3.0" + "kamu/framework": "^3.1" + }, + "require-dev": { + "fakerphp/faker": "^1.23", + "psy/psysh": "^0.11.22" }, "autoload": { "psr-4": { diff --git a/saya b/saya index 711ed33..19d6b7e 100644 --- a/saya +++ b/saya @@ -18,6 +18,6 @@ require_once __DIR__ . '/vendor/autoload.php'; * Ini sangat simple. */ -exit(\Core\Kernel\Kernel::console( +exit(\Core\Kernel\Kernel::cli( new \App\Kernel() )->run());