|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Http\Controllers; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; |
| 6 | +use Binaryk\LaravelRestify\Repositories\Repository; |
| 7 | +use Binaryk\LaravelRestify\Restify; |
| 8 | +use Illuminate\Routing\Controller; |
| 9 | +use Illuminate\Support\Str; |
| 10 | + |
| 11 | +class RestifyJsSetupController extends Controller |
| 12 | +{ |
| 13 | + public function __invoke(RestifyRequest $request) |
| 14 | + { |
| 15 | + return response()->json([ |
| 16 | + 'config' => $this->config(), |
| 17 | + 'repositories' => $this->repositories($request), |
| 18 | + ]); |
| 19 | + } |
| 20 | + |
| 21 | + private function repositories(RestifyRequest $request): array |
| 22 | + { |
| 23 | + return collect(Restify::$repositories) |
| 24 | + ->map(fn (string $repository) => app($repository)) |
| 25 | + ->map(fn (Repository $repository) => $repository->restifyjsSerialize($request)) |
| 26 | + ->all(); |
| 27 | + } |
| 28 | + |
| 29 | + private function config(): array |
| 30 | + { |
| 31 | + return [ |
| 32 | + 'domain' => $this->deleteFirstAndLastSlash(config('app.url')), |
| 33 | + 'base' => $this->deleteFirstAndLastSlash(Restify::path()), |
| 34 | + ]; |
| 35 | + } |
| 36 | + |
| 37 | + private function deleteFirstAndLastSlash(string $domain): string |
| 38 | + { |
| 39 | + if (Str::startsWith($domain, '/')) { |
| 40 | + $domain = Str::replaceFirst('/', '', $domain); |
| 41 | + } |
| 42 | + |
| 43 | + if (Str::endsWith($domain, '/')) { |
| 44 | + $domain = Str::replaceLast('/', '', $domain); |
| 45 | + } |
| 46 | + |
| 47 | + return $domain; |
| 48 | + } |
| 49 | +} |
0 commit comments