Skip to content

Commit

Permalink
feat: enhance AppServiceProvider with configuration methods for comma…
Browse files Browse the repository at this point in the history
…nds, models, dates, and password validation
  • Loading branch information
MrPunyapal committed Feb 28, 2025
1 parent 89f2cc4 commit bdf1bc9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
47 changes: 44 additions & 3 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace App\Providers;

use App\Support\FileUploaderFromUrl;
use Illuminate\Http\UploadedFile;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rules\Password;
use Override;

final class AppServiceProvider extends ServiceProvider
Expand All @@ -22,6 +25,44 @@ public function register(): void {}
*/
public function boot(): void
{
UploadedFile::macro('makeFromUrl', fn (string $url): ?UploadedFile => (new FileUploaderFromUrl)($url));
$this->configureCommands();
$this->configureModels();
$this->configurePasswordValidation();
$this->configureDates();
}

/**
* Configure the application's commands.
*/
private function configureCommands(): void
{
DB::prohibitDestructiveCommands(
$this->app->isProduction()
);
}

/**
* Configure the dates.
*/
private function configureDates(): void
{
Date::use(CarbonImmutable::class);
}

/**
* Configure the models.
*/
private function configureModels(): void
{
Model::shouldBeStrict(! $this->app->isProduction());
Model::unguard();
}

/**
* Configure the password validation rules.
*/
private function configurePasswordValidation(): void
{
Password::defaults(fn () => $this->app->isProduction() ? Password::min(8)->uncompromised() : null);
}
}
6 changes: 2 additions & 4 deletions app/Traits/HasFileFromUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Traits;

use App\Support\FileUploaderFromUrl;
use Illuminate\Http\UploadedFile;

/**
Expand All @@ -14,10 +15,7 @@ trait HasFileFromUrl
public function resolveFileFromUrl(string $field): void
{
if (! $this->hasFile($field) && filter_var($this->get($field), FILTER_VALIDATE_URL)) {
/**
* @see \App\Support\FileUploaderFromUrl::__invoke()
*/
$file = UploadedFile::makeFromUrl(
$file = app(FileUploaderFromUrl::class)(
(string) $this->string($field)
);

Expand Down

0 comments on commit bdf1bc9

Please sign in to comment.