2022-08-10 07:00:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2024-01-03 17:02:18 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2024-01-07 12:43:10 +00:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2024-01-01 11:40:21 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2024-01-07 12:43:10 +00:00
|
|
|
use Illuminate\Support\Facades\File;
|
2024-01-03 17:02:18 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-08-10 07:00:54 +00:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2024-01-07 12:43:10 +00:00
|
|
|
use Illuminate\Testing\TestResponse;
|
2022-08-10 07:00:54 +00:00
|
|
|
|
|
|
|
class MacroProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
public function boot(): void
|
|
|
|
{
|
2024-01-01 11:40:21 +00:00
|
|
|
Collection::macro('orderByArray', function (array $orderBy, string $key = 'id'): Collection {
|
|
|
|
/** @var Collection $this */
|
|
|
|
return $this->sortBy(static fn ($item) => array_search($item->$key, $orderBy, true));
|
2022-08-10 07:00:54 +00:00
|
|
|
});
|
2024-01-03 17:02:18 +00:00
|
|
|
|
|
|
|
Builder::macro('logSql', function (): Builder {
|
|
|
|
/** @var Builder $this */
|
|
|
|
Log::info($this->toSql());
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
});
|
2024-01-07 12:43:10 +00:00
|
|
|
|
|
|
|
if (app()->runningUnitTests()) {
|
|
|
|
UploadedFile::macro('fromFile', static function (string $path, ?string $name = null): UploadedFile {
|
|
|
|
return UploadedFile::fake()->createWithContent($name ?? basename($path), File::get($path));
|
|
|
|
});
|
|
|
|
|
|
|
|
TestResponse::macro('log', function (string $file = 'test-response.json'): TestResponse {
|
|
|
|
/** @var TestResponse $this */
|
|
|
|
File::put(storage_path('logs/' . $file), $this->getContent());
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
});
|
|
|
|
}
|
2022-08-10 07:00:54 +00:00
|
|
|
}
|
|
|
|
}
|