Skip to content

Commit

Permalink
Upgrade to Laravel 11
Browse files Browse the repository at this point in the history
  • Loading branch information
jewei committed Apr 26, 2024
1 parent 2ba9f84 commit 384631c
Show file tree
Hide file tree
Showing 61 changed files with 2,051 additions and 2,597 deletions.
59 changes: 32 additions & 27 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,53 @@ APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_TIMEZONE=UTC
APP_URL=http://localhost

APP_LOCALE=en
APP_FALLBACK_LOCALE=en
APP_FAKER_LOCALE=en_US

APP_MAINTENANCE_DRIVER=file
APP_MAINTENANCE_STORE=database

BCRYPT_ROUNDS=12

LOG_CHANNEL=stack
LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_skeleton
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null

BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database

CACHE_STORE=database
CACHE_PREFIX=

MEMCACHED_HOST=127.0.0.1

REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_MAILER=log
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Expand All @@ -43,17 +61,4 @@ AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_APP_NAME="${APP_NAME}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
27 changes: 0 additions & 27 deletions app/Console/Kernel.php

This file was deleted.

40 changes: 0 additions & 40 deletions app/Exceptions/Handler.php

This file was deleted.

8 changes: 2 additions & 6 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
abstract class Controller
{
use AuthorizesRequests, ValidatesRequests;
//
}
68 changes: 0 additions & 68 deletions app/Http/Kernel.php

This file was deleted.

17 changes: 0 additions & 17 deletions app/Http/Middleware/Authenticate.php

This file was deleted.

17 changes: 0 additions & 17 deletions app/Http/Middleware/EncryptCookies.php

This file was deleted.

66 changes: 53 additions & 13 deletions app/Http/Middleware/Idempotency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class Idempotency
{
Expand Down Expand Up @@ -34,48 +35,87 @@ class Idempotency
*/
protected string $cachePrefix = 'idempotency_key_';

/**
* The cache duration in minutes.
*/
protected int $cacheDuration = 5;

/**
* The request instance.
*/
protected Request $request;

/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
public function handle(Request $request, Closure $next): SymfonyResponse
{
if (! $this->isIdempotentRequest($request) || is_null($idempotenceKey = $this->getIdempotencyKey($request))) {
if (! $this->isIdempotentRequest($request) || empty($idempotenceKey = $this->getIdempotencyKey($request))) {
return $next($request);
}

$response = $next($request);
$response = $next($this->request = $request);

if ($response->isClientError() || $response->isServerError()) {
return $response;
}

$cacheKey = sprintf("{$this->cachePrefix}_%s_{$idempotenceKey}", $request->user()?->id ?: $request->ip());

// If the response is already cached, return it.
if (Cache::has($cacheKey)) {
$cache = Cache::get($cacheKey);
if ($this->hasCache()) {
$cache = $this->getCache();

return response($cache['content'], $cache['status'], $cache['headers'])
return response((string) $cache['content'], (int) $cache['status'], (array) $cache['headers'])
->header($this->idempotenceKey, $idempotenceKey)
->header($this->statusKey, 'HIT')
->header($this->replayKey, 'true');
}

// Store the response in cache.
Cache::put($cacheKey, [
$this->setCache([
'content' => $response->getContent(),
'status' => $response->getStatusCode(),
'headers' => $response->headers->all(),
], now()->addMinutes(5));
]);

$response->headers->set($this->idempotenceKey, $idempotenceKey);
$response->headers->set($this->statusKey, 'MISS');

return $response;
}

protected function resolveCacheKey(): string
{
return sprintf(
'%s_%s_%s',
$this->cachePrefix,
$this->getIdempotencyKey($this->request),
$this->request->user()?->id ?: $this->request->ip()
);
}

protected function hasCache(): bool
{
return Cache::has($this->resolveCacheKey());
}

/**
* Get the cache value.
*/
protected function getCache(): array
{
return (array) Cache::get($this->resolveCacheKey());
}

/**
* Set the cache value.
*/
protected function setCache(array $value): void
{
Cache::put($this->resolveCacheKey(), $value, now()->addMinutes($this->cacheDuration));
}

/**
* Check if the request is idempotent.
*/
Expand All @@ -87,8 +127,8 @@ protected function isIdempotentRequest(Request $request): bool
/**
* Get the idempotency key from the request.
*/
protected function getIdempotencyKey(Request $request): ?string
protected function getIdempotencyKey(Request $request): string
{
return $request->header($this->idempotenceKey);
return $request->header($this->idempotenceKey, '');
}
}
Loading

0 comments on commit 384631c

Please sign in to comment.