Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Laravel 10 #665

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* text=auto
* text=auto eol=lf

*.blade.php diff=html
*.css diff=css
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.phpunit.cache
/node_modules
/public/hot
/public/storage
Expand Down
13 changes: 6 additions & 7 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
protected function schedule(Schedule $schedule): void
{
$schedule->command('cache:prune-stale-tags')
->hourly()
->onOneServer();

$schedule->command('queue:prune-batches')
->daily()
->onOneServer();
Expand Down Expand Up @@ -53,10 +54,8 @@ protected function schedule(Schedule $schedule)

/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
protected function commands(): void
{
$this->load(__DIR__.'/Commands');

Expand Down
13 changes: 1 addition & 12 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
//
];

/**
* A list of the inputs that are never flashed for validation exceptions.
*
Expand All @@ -32,10 +23,8 @@ class Handler extends ExceptionHandler

/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
public function register(): void
{
$this->reportable(function (Throwable $e) {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function index()
if (!Cache::has($key)) {
// The index should be continuously regenerated with a scheduled job. This is
// a fallback in case the job wasn't performed for some reason.
GenerateFederatedSearchIndex::dispatchNow();
GenerateFederatedSearchIndex::dispatchSync();
}

return Cache::get($key);
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ class Kernel extends HttpKernel
],

'api' => [
'throttle:api',
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];

/**
* The application's route middleware.
* The application's middleware aliases.
*
* These middleware may be assigned to groups or used individually.
* Aliases may be used instead of class names to conveniently assign middleware to routes and groups.
*
* @var array<string, class-string|string>
*/
protected $routeMiddleware = [
protected $middlewareAliases = [
'auth' => \Biigle\Http\Middleware\Authenticate::class,
'reg' => \Biigle\Http\Middleware\AuthenticateRegister::class,
// 'auth.api' => \Biigle\Http\Middleware\AuthenticateAPI::class,
Expand Down
10 changes: 3 additions & 7 deletions app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
namespace Biigle\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;

class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string
*/
protected function redirectTo($request)
protected function redirectTo(Request $request): ?string
{
if (!$request->expectsJson()) {
return route('login');
}
return $request->expectsJson() ? null : route('login');
}
}
8 changes: 3 additions & 5 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param string|null ...$guards
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, ...$guards)
public function handle(Request $request, Closure $next, string ...$guards): Response
{
$guards = empty($guards) ? [null] : $guards;

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/TrustHosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TrustHosts extends Middleware
*
* @return array<int, string|null>
*/
public function hosts()
public function hosts(): array
{
return [
$this->allSubdomainsOfApplicationUrl(),
Expand Down
12 changes: 0 additions & 12 deletions app/Http/Middleware/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,4 @@ class TrustProxies extends Middleware
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO |
Request::HEADER_X_FORWARDED_AWS_ELB;

/**
* Manual fix for: https://github.com/laravel/framework/pull/47844
*
* TODO: Remove after upgrading to Laravel 10.x.
*
* @return int
*/
protected function getTrustedHeaderNames()
{
return $this->headers;
}
}
10 changes: 1 addition & 9 deletions app/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ class Image extends VolumeFile
'lat' => 'float',
'lng' => 'float',
'tiled' => 'bool',
];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'taken_at',
'taken_at' => 'datetime',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Logging/CreateRedisLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CreateRedisLogger
* @param array $config
* @return \Monolog\Logger
*/
public function __invoke(array $config)
public function __invoke(array $config): Logger
{
$connection = $config['connection'] ?? null;
$capSize = $config['capSize'] ?? 1000;
Expand Down
3 changes: 0 additions & 3 deletions app/Logging/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
use File;
use Illuminate\Log\ParsesLogConfiguration;
use Illuminate\Support\Facades\Redis;
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\RedisHandler;
use Monolog\Logger;

class LogManager
{
Expand Down
8 changes: 2 additions & 6 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class AppServiceProvider extends ServiceProvider

/**
* Register any application services.
*
* @return void
*/
public function register()
public function register(): void
{
$this->app->singleton('modules', function () {
return new \Biigle\Services\Modules;
Expand All @@ -40,10 +38,8 @@ public function register()

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
public function boot(): void
{
View::composer('*', function ($view) {
// Make some variables available in any view.
Expand Down
6 changes: 1 addition & 5 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ class AuthServiceProvider extends ServiceProvider

/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
public function boot(): void
{
$this->registerPolicies();

// Ability of a global admin.
Gate::define('sudo', function (User $user) {
return $user->isInSuperUserMode;
Expand Down
4 changes: 1 addition & 3 deletions app/Providers/BroadcastServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
public function boot(): void
{
Broadcast::routes();

Expand Down
8 changes: 2 additions & 6 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ class EventServiceProvider extends ServiceProvider

/**
* Register any events for your application.
*
* @return void
*/
public function boot()
public function boot(): void
{
\Biigle\Project::observe(new \Biigle\Observers\ProjectObserver);
\Biigle\Volume::observe(new \Biigle\Observers\VolumeObserver);
Expand All @@ -39,10 +37,8 @@ public function boot()

/**
* Determine if events and listeners should be automatically discovered.
*
* @return bool
*/
public function shouldDiscoverEvents()
public function shouldDiscoverEvents(): bool
{
return false;
}
Expand Down
46 changes: 17 additions & 29 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
* The path to your application's "home" route.
*
* This is used by Laravel authentication to redirect users after login.
*
Expand All @@ -32,10 +32,8 @@ class RouteServiceProvider extends ServiceProvider

/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
public function boot(): void
{
// IDs are integers which can have the maximum value of 2147483647 (10 digits).
// Regex validation of a 32bit integer is very messy so we only do a rough check
Expand All @@ -51,7 +49,21 @@ public function boot()
'uuid' => "\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\z",
]);

$this->configureRateLimiting();
RateLimiter::for('api', function (Request $request) {
// Check User class because a FederatedSearchInstance could be a user here,
// too. The relaxeed rate limiting should only apply to actual users.
if ($request->user() instanceof User) {
if ($request->user()->can('sudo')) {
return Limit::none();
}

// 3 requests per second.
return Limit::perHour(10800)->by($request->user()->id);
}

// One request per second.
return Limit::perHour(3600)->by($request->ip());
});

$this->routes(function () {
// Web
Expand All @@ -72,28 +84,4 @@ public function boot()
->group(base_path('routes/federated-search.php'));
});
}

/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
// Check User class because a FederatedSearchInstance could be a user here,
// too. The relaxeed rate limiting should only apply to actual users.
if ($request->user() instanceof User) {
if ($request->user()->can('sudo')) {
return Limit::none();
}

// 3 requests per second.
return Limit::perHour(10800)->by($request->user()->id);
}

// One request per second.
return Limit::perHour(3600)->by($request->ip());
});
}
}
11 changes: 4 additions & 7 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ class User extends Authenticatable
protected $casts = [
'role_id' => 'int',
'attrs' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'login_at' => 'datetime',
'password' => 'hashed',
];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at', 'updated_at', 'login_at'];

/**
* Set the email attribute and transform it to lowercase.
*
Expand Down
Loading