diff --git a/src/Commands/FactoryMakeCommand.php b/src/Commands/FactoryMakeCommand.php new file mode 100644 index 0000000..ee440a5 --- /dev/null +++ b/src/Commands/FactoryMakeCommand.php @@ -0,0 +1,126 @@ +resolveStubPath('/stubs/factory.stub'); + } + + protected function buildClass($name): string + { + $factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name))); + + $namespaceModel = $this->option('model') + ? $this->qualifyModel($this->option('model')) + : $this->qualifyModel($this->guessModelName($name)); + + $model = class_basename($namespaceModel); + + $namespace = $this->getNamespace( + $this->getModule()->makeNamespace('Database\\Factories\\') + ); + + $replace = [ + '{{ factoryNamespace }}' => $namespace, + 'NamespacedDummyModel' => $namespaceModel, + '{{ namespacedModel }}' => $namespaceModel, + '{{namespacedModel}}' => $namespaceModel, + 'DummyModel' => $model, + '{{ model }}' => $model, + '{{model}}' => $model, + '{{ factory }}' => $factory, + '{{factory}}' => $factory, + ]; + + return str_replace( + array_keys($replace), array_values($replace), parent::buildClass($name) + ); + } + + /** + * Get the destination class path. + * + * @param string $name + * @return string + */ + protected function getPath($name) + { + $name = (string) Str::of($name)->replaceFirst($this->rootNamespace(), '')->finish('Factory'); + + return $this->getModule()->factoriesPath(str_replace('\\', '/', $name).'.php'); + } + + /** + * Guess the model name from the Factory name or return a default model name. + * + * @param string $name + * @return string + */ + protected function guessModelName($name) + { + if (str_ends_with($name, 'Factory')) { + $name = substr($name, 0, -7); + } + + $modelName = $this->qualifyModel(Str::after($name, $this->rootNamespace())); + + if (class_exists($modelName)) { + return $modelName; + } + + if (is_dir(app_path('Models/'))) { + return $this->rootNamespace().'Models\Model'; + } + + return $this->rootNamespace().'Model'; + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'], + ]; + } +} diff --git a/src/Commands/ModelMakeCommand.php b/src/Commands/ModelMakeCommand.php index a3f038f..0e6940e 100644 --- a/src/Commands/ModelMakeCommand.php +++ b/src/Commands/ModelMakeCommand.php @@ -6,51 +6,24 @@ use Illuminate\Support\Str; use Savannabits\Modular\Facades\Modular; use Savannabits\Modular\Module; +use Savannabits\Modular\Support\Concerns\GeneratesModularFiles; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputArgument; #[AsCommand(name: 'modular:make-model')] class ModelMakeCommand extends BaseModelMakeCommand { + use GeneratesModularFiles; protected $name = 'modular:make-model'; protected $description = 'Create a new Eloquent model class in a modular package'; - - protected function getArguments(): array - { - return array_merge(parent::getArguments(), [ - ['module', InputArgument::REQUIRED, 'The name of the module in which this should be installed'], - ]); - } - - public function getModule(): Module - { - return Modular::module($this->argument('module')); - } - - protected function getDefaultNamespace($rootNamespace): string - { - return $rootNamespace.'\\Models'; - } - - protected function rootNamespace(): string - { - return $this->getModule()->getRootNamespace(); - } - - protected function getPath($name): string - { - $name = Str::replaceFirst($this->rootNamespace(), '', $name); - - return $this->getModule()->srcPath(str_replace('\\', '/', $name).'.php'); - } - protected function createFactory(): void { $factory = Str::studly($this->argument('name')); - $this->call('make:factory', [ + $this->call('modular:make-factory', [ 'name' => "{$factory}Factory", + 'module' => $this->getModule()->name(), '--model' => $this->qualifyClass($this->getNameInput()), ]); } @@ -116,4 +89,9 @@ protected function createPolicy(): void '--model' => $this->qualifyClass($this->getNameInput()), ]); } + + protected function getRelativeNamespace(): string + { + return 'Models'; + } } diff --git a/src/Modular.php b/src/Modular.php index c511686..79583d3 100755 --- a/src/Modular.php +++ b/src/Modular.php @@ -16,4 +16,10 @@ public function module(string $name): Module { return new Module($name); } + + public function packagePath(string $path = ''): string + { + //return the base path of this package + return __DIR__ . '/../'.($path ? DIRECTORY_SEPARATOR . trim($path,DIRECTORY_SEPARATOR) : ''); + } } diff --git a/src/Support/Concerns/GeneratesModularFiles.php b/src/Support/Concerns/GeneratesModularFiles.php new file mode 100644 index 0000000..c1f80da --- /dev/null +++ b/src/Support/Concerns/GeneratesModularFiles.php @@ -0,0 +1,51 @@ +laravel->basePath(trim($stub, '/'))) + ? $customPath + : Modular::packagePath(trim($stub, DIRECTORY_SEPARATOR)); + } + public function getModule(): Module + { + return Modular::module($this->argument('module')); + } + + protected function getDefaultNamespace($rootNamespace): string + { + return trim($rootNamespace,'\\').'\\'.trim(Str::replace(DIRECTORY_SEPARATOR,'\\',$this->getRelativeNamespace()), '\\'); + } + + protected function getRelativeNamespace(): string + { + return ''; + } + + protected function rootNamespace(): string + { + return $this->getModule()->getRootNamespace(); + } + + protected function getPath($name): string + { + $name = Str::replaceFirst($this->rootNamespace(), '', $name); + + return $this->getModule()->srcPath(str_replace('\\', '/', $name).'.php'); + } +}