AppServiceProvider.php
1 <?php 2 3 namespace App\Providers; 4 5 use Illuminate\Http\Resources\Json\JsonResource; 6 use Illuminate\Pagination\LengthAwarePaginator; 7 use Illuminate\Support\Collection; 8 use Illuminate\Support\Str; 9 use Illuminate\Support\Facades\Http; 10 use Illuminate\Support\ServiceProvider; 11 use Illuminate\View\ComponentAttributeBag; 12 use Laravel\Fortify\Fortify; 13 use Maatwebsite\Excel\Sheet; 14 15 class AppServiceProvider extends ServiceProvider 16 { 17 /** 18 * Register any application services. 19 * 20 * @return void 21 */ 22 public function register() 23 { 24 // 25 } 26 27 /** 28 * Bootstrap any application services. 29 * 30 * @return void 31 */ 32 public function boot() 33 { 34 Fortify::ignoreRoutes(); 35 ComponentAttributeBag::macro('hasStartsWith', function (string $key) { 36 return (bool) $this->whereStartsWith($key)->first(); 37 }); 38 39 Http::macro('inbox', function () { 40 return Http::withHeaders([ 41 'Accept' => 'application/json', 42 'Content-Type' => 'application/json', 43 'API-Key' => config('inbox.API_KEY'), 44 ])->withOptions(['verify' => false])->baseUrl(config('inbox.INBOX_HOST') . '/api'); 45 }); 46 47 Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') { 48 $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName); 49 50 return new LengthAwarePaginator( 51 $this->forPage($page, $perPage), 52 $total ?: $this->count(), 53 $perPage, 54 $page, 55 [ 56 'path' => LengthAwarePaginator::resolveCurrentPath(), 57 'pageName' => $pageName, 58 ] 59 ); 60 }); 61 62 Sheet::macro('setOrientation', function (Sheet $sheet, string $orientation) { 63 $sheet->getDelegate()->getPageSetup()->setOrientation($orientation); 64 }); 65 Sheet::macro('setPaperSize', function (Sheet $sheet, int $paper_size) { 66 $sheet->getDelegate()->getPageSetup()->setPaperSize($paper_size); 67 }); 68 Sheet::macro('setFitToPage', function (Sheet $sheet, bool $fit) { 69 $sheet->getDelegate()->getPageSetup()->setFitToPage($fit); 70 }); 71 72 Str::macro('password', function ($length = 32, $letters = true, $numbers = true, $symbols = true, $spaces = false) { 73 if (env('APP_ENV') === 'local') { 74 return 'password'; 75 } 76 77 return (new Collection) 78 ->when($letters, fn ($c) => $c->merge([ 79 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 80 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 81 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 82 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 83 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 84 ])) 85 ->when($numbers, fn ($c) => $c->merge([ 86 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 87 ])) 88 ->when($symbols, fn ($c) => $c->merge([ 89 '~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-', 90 '_', '.', ',', '<', '>', '?', '/', '{', '}', '[', 91 ']', '|', ':', ';', 92 ])) 93 ->when($spaces, fn ($c) => $c->merge([' '])) 94 ->pipe(fn ($c) => Collection::times($length, fn () => $c[random_int(0, $c->count() - 1)])) 95 ->implode(''); 96 }); 97 98 JsonResource::withoutWrapping(); 99 } 100 }