mshamaseen /
laravel-repositories
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Shamaseen\Repository\Utility; |
||
| 4 | |||
| 5 | use Illuminate\Contracts\Pagination\Paginator; |
||
| 6 | use Illuminate\Contracts\View\View; |
||
| 7 | use Illuminate\Database\Eloquent\Collection; |
||
| 8 | use Illuminate\Database\Eloquent\Model; |
||
|
0 ignored issues
–
show
|
|||
| 9 | use Illuminate\Http\JsonResponse; |
||
| 10 | use Illuminate\Http\RedirectResponse; |
||
| 11 | use Shamaseen\Repository\Interfaces\CrudResponse; |
||
| 12 | |||
| 13 | class ResponseDispatcher implements CrudResponse |
||
| 14 | { |
||
| 15 | private WebResponses|APIResponses $dispatcher; |
||
| 16 | |||
| 17 | public function __construct(Controller $controller) |
||
| 18 | { |
||
| 19 | if ('api' === config('repository.responses')) { |
||
| 20 | $this->dispatcher = (new APIResponses($controller)); |
||
| 21 | } elseif ('web' === config('repository.responses')) { |
||
| 22 | $this->dispatcher = (new WebResponses($controller)); |
||
| 23 | } elseif ($controller->isAPI) { |
||
| 24 | $this->dispatcher = (new APIResponses($controller)); |
||
| 25 | } else { |
||
| 26 | $this->dispatcher = (new WebResponses($controller)); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | public function index(Paginator|Collection $paginate): View|JsonResponse |
||
| 31 | { |
||
| 32 | return $this->dispatcher->index($paginate); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function show(Model $entity): View|JsonResponse |
||
| 36 | { |
||
| 37 | return $this->dispatcher->show($entity); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function create(): View|JsonResponse |
||
| 41 | { |
||
| 42 | return $this->dispatcher->create(); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function store(Model $entity): JsonResponse|RedirectResponse |
||
| 46 | { |
||
| 47 | return $this->dispatcher->store($entity); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function edit(Model $entity): View|JsonResponse |
||
| 51 | { |
||
| 52 | return $this->dispatcher->edit($entity); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function update(int|bool|Model $updatedCount): JsonResponse|RedirectResponse |
||
| 56 | { |
||
| 57 | return $this->dispatcher->update($updatedCount); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function destroy(int|bool $destroyedCount): JsonResponse|RedirectResponse |
||
| 61 | { |
||
| 62 | return $this->dispatcher->destroy($destroyedCount); |
||
| 63 | } |
||
| 64 | } |
||
| 65 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: