diff --git a/packages/core/composer.json b/packages/core/composer.json index c4911d9f96..4b6ed5ed5e 100644 --- a/packages/core/composer.json +++ b/packages/core/composer.json @@ -42,6 +42,7 @@ "kalnoy/nestedset": "^6.0", "doctrine/dbal": "^3.6", "spatie/laravel-blink": "^1.6", - "lukascivil/treewalker": "0.9.1" + "lukascivil/treewalker": "0.9.1", + "spatie/php-structure-discoverer": "^2.0" } } diff --git a/packages/core/src/Base/ModelManifest.php b/packages/core/src/Base/ModelManifest.php index ecf7a4f0e3..856033ae4f 100644 --- a/packages/core/src/Base/ModelManifest.php +++ b/packages/core/src/Base/ModelManifest.php @@ -5,24 +5,29 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; -use Lunar\Models; -use Lunar\Models\Contracts; +use Spatie\StructureDiscoverer\Discover; class ModelManifest implements ModelManifestInterface { /** * The collection of models to register to this manifest. */ - protected array $models = [ - Contracts\ProductType::class => Models\ProductType::class, - ]; + protected array $models = []; /** * Bind initial models in container and set explicit model binding. */ public function register(): void { - foreach ($this->models as $interfaceClass => $modelClass) { + // Discover models + $modelClasses = Discover::in(__DIR__ . '/../Models') + ->classes() + ->extending(BaseModel::class) + ->get(); + + foreach ($modelClasses as $modelClass) { + $interfaceClass = $this->guessContractClass($modelClass); + $this->models[$interfaceClass] = $modelClass; $this->bindModel($interfaceClass, $modelClass); } } @@ -82,4 +87,11 @@ protected function bindingName(string $modelClass): string return Str::camel($shortName); } + + public function guessContractClass(string $modelClass) + { + $shortName = (new \ReflectionClass($modelClass))->getShortName(); + + return 'Lunar\\Models\\Contracts\\'.$shortName; + } }