From a8c958ac3f40a61dccf53d3bd5bc2dd1f0770c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C3=ADbal=20=C3=81lvarez?= Date: Sun, 3 Apr 2022 23:40:24 -0400 Subject: [PATCH] feat(controller): Allow using Builder controllers with custom Models/Controllers' directories --- src/Providers/PbControllerServiceProvider.php | 6 +++++- src/Traits/PbControllerTrait.php | 4 ++-- src/Utilities/PbUtilities.php | 9 +++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Providers/PbControllerServiceProvider.php b/src/Providers/PbControllerServiceProvider.php index c09dbb7..546063d 100644 --- a/src/Providers/PbControllerServiceProvider.php +++ b/src/Providers/PbControllerServiceProvider.php @@ -44,7 +44,11 @@ public function boot(Kernel $kernel) if (Schema::hasTable('modules')) { $models = PbModule::whereIn('modulekey', app(PbUtilities::class)->modulekeys)->pluck('modulekey'); foreach ($models as $model) { - $this->app->make($this->namespace.'\\'.ucfirst($model).'\\'.$this->prefix.ucfirst($model).$this->suffix); + if (class_exists($this->namespace.'\\'.ucfirst($model).'\\'.$this->prefix.ucfirst($model).$this->suffix)) { + $this->app->make($this->namespace.'\\'.ucfirst($model).'\\'.$this->prefix.ucfirst($model).$this->suffix); + } elseif (class_exists('App\\Http\\Controllers\\'.ucfirst($model).$this->suffix)) { + $this->app->make('App\\Http\\Controllers\\'.ucfirst($model).$this->suffix); + } } } } diff --git a/src/Traits/PbControllerTrait.php b/src/Traits/PbControllerTrait.php index 2f275f4..1251cef 100644 --- a/src/Traits/PbControllerTrait.php +++ b/src/Traits/PbControllerTrait.php @@ -716,8 +716,8 @@ public function buildControllerVars($key): object $object->names = Str::plural($object->name); $object->prefixName = strtolower($this->vars->helper->prefix . $key); $object->prefixNames = Str::plural($object->prefixName); - $object->modelPath = $this->vars->helper->vendor . "\\" . $this->vars->helper->package . "\\Models\\" . $this->vars->helper->prefix . $key; - $object->viewsPath = $this->vars->helper->package . "/" . $object->keys . "/"; + $object->modelPath = $this->vars->modelPath ?? $this->vars->helper->vendor . "\\" . $this->vars->helper->package . "\\Models\\" . $this->vars->helper->prefix . $key; + $object->viewsPath = ($this->vars->viewsPath ?? $this->vars->helper->package . "/" . $object->keys) . "/"; $object->table = resolve($object->modelPath)->getTable(); return $object; } diff --git a/src/Utilities/PbUtilities.php b/src/Utilities/PbUtilities.php index 75e625f..9cf621e 100644 --- a/src/Utilities/PbUtilities.php +++ b/src/Utilities/PbUtilities.php @@ -55,8 +55,13 @@ public function buildCrudRoutes($type): void $names = PbModule::whereIn('modulekey', $this->modulekeys)->pluck('modulekey'); foreach ($names as $name) { - $modelClass = $this->vendor . '\\' . $this->package . '\\Models\\' . $this->prefix . ucfirst($name); - $controllerClass = $this->vendor . '\\' . $this->package . '\\Controllers\\' . ucfirst($name) . '\\' . $this->prefix . ucfirst($name) . 'Controller'; + if (class_exists($this->vendor . '\\' . $this->package . '\\Models\\' . $this->prefix . ucfirst($name))) { + $modelClass = $this->vendor . '\\' . $this->package . '\\Models\\' . $this->prefix . ucfirst($name); + $controllerClass = $this->vendor . '\\' . $this->package . '\\Controllers\\' . ucfirst($name) . '\\' . $this->prefix . ucfirst($name) . 'Controller'; + } elseif (class_exists('App\\Models\\' . ucfirst($name))) { + $modelClass = 'App\\Models\\' . ucfirst($name); + $controllerClass = 'App\\Http\\Controllers\\' . ucfirst($name) . 'Controller'; + } switch ($type) { case 'web': Route::resource($name . 's', $controllerClass)->middleware([