Skip to content

Commit

Permalink
feat: update skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Nov 30, 2023
1 parent 64cb7fd commit f7ea8e8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}
Expand Down
19 changes: 13 additions & 6 deletions app/Middleware/CorsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Core\Http\Request;
use Core\Http\Respond;
use Core\Middleware\MiddlewareInterface;

final class CorsMiddleware implements MiddlewareInterface
Expand All @@ -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(
Expand All @@ -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);
}
}
52 changes: 52 additions & 0 deletions app/Middleware/GzipMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Middleware;

use Closure;
use Core\Http\Request;
use Core\Http\Stream;
use Core\Middleware\MiddlewareInterface;

final class GzipMiddleware implements MiddlewareInterface
{
public function handle(Request $request, Closure $next): Stream|\Core\Http\Respond
{
$response = $next($request);

if ($response instanceof Stream) {
return $response;
}

$response = respond()->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;
}
}
10 changes: 10 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Core\Model\Model;
use Core\Valid\Hash;

final class User extends Model
{
Expand All @@ -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)),
];
}
}
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion saya
Original file line number Diff line number Diff line change
Expand Up @@ -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());

0 comments on commit f7ea8e8

Please sign in to comment.