From 246c5401cf7c9a4a70757ed85d16423ef1fc1747 Mon Sep 17 00:00:00 2001 From: Sam Maosa Date: Sat, 6 Apr 2024 14:20:43 +0300 Subject: [PATCH] Improved API for file creation - Added a Module class to help get the details of a module - Added ability to create migrations in the directory. --- src/Commands/MigrationMakeCommand.php | 43 ++++++++++++ src/Modular.php | 5 ++ src/Module.php | 98 +++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 src/Commands/MigrationMakeCommand.php create mode 100644 src/Module.php diff --git a/src/Commands/MigrationMakeCommand.php b/src/Commands/MigrationMakeCommand.php new file mode 100644 index 0000000..b9c17de --- /dev/null +++ b/src/Commands/MigrationMakeCommand.php @@ -0,0 +1,43 @@ +call('make:migration', [ + 'name' => $this->argument('name'), + '--create' => $this->option('create'), + '--table' => $this->option('table'), + '--path' => $this->getMigrationPath(), + ]); + } + + protected function getMigrationPath(): string + { + return Modular::module($this->argument('module'))->migrationsPath(relative: true); + } + + protected function promptForMissingArgumentsUsing(): array + { + return [ + 'name' => ['What should the migration name be?','e.g create_planets_table'], + 'module' => ['Enter the module name (It has to be existing)','e.g Blog, BlogPost, blog-post'] + ]; + } +} diff --git a/src/Modular.php b/src/Modular.php index 92e8a2e..c511686 100755 --- a/src/Modular.php +++ b/src/Modular.php @@ -11,4 +11,9 @@ public function execCommand(string $command): void proc_close($process); } } + + public function module(string $name): Module + { + return new Module($name); + } } diff --git a/src/Module.php b/src/Module.php new file mode 100644 index 0000000..9a719b5 --- /dev/null +++ b/src/Module.php @@ -0,0 +1,98 @@ +name = Str::kebab($name); + $this->title = Str::of($name)->kebab()->title()->replace('-', ' ')->toString(); + // If the module does not exist, throw an error + if (!is_dir(app()->basePath(config('modular.path') . DIRECTORY_SEPARATOR . $this->name))) { + abort(404,"Module $name does not exist"); + } + $this->studlyName = Str::of($name)->studly()->toString(); + $this->namespace = config('modular.namespace') . '\\' . $this->studlyName; + $this->basePath = app()->basePath(config('modular.path') . DIRECTORY_SEPARATOR . trim($this->name, DIRECTORY_SEPARATOR)); + } + + public function path(string $path = '', bool $relative = false): string + { + $basePath = $this->basePath; + if ($relative) { + $basePath = config('modular.path') . DIRECTORY_SEPARATOR . trim($this->name, DIRECTORY_SEPARATOR); + } + return $basePath . ($path ? DIRECTORY_SEPARATOR . trim($path, DIRECTORY_SEPARATOR) : ''); + } + + public function makeNamespace(string $relativeNamespace = ''): string + { + return $this->namespace . ($relativeNamespace ? '\\' . ltrim($relativeNamespace, '\\') : ''); + } + + public function name(): string + { + return $this->name; + } + + public function title(): string + { + return $this->title; + } + + public function studlyName(): string + { + return $this->studlyName; + } + + public function basePath(): string + { + return $this->basePath; + } + + public function __toString(): string + { + return $this->title(); + } + + public function configPath(string $path = '', bool $relative = false): string + { + return $this->path('config'.DIRECTORY_SEPARATOR. ltrim($path,DIRECTORY_SEPARATOR), relative: $relative); + } + + public function databasePath(string $path = '', bool $relative = false): string + { + return $this->path('database'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR), relative: $relative); + } + + public function migrationsPath(string $path = '', bool $relative = false): string + { + return $this->databasePath('migrations'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR),relative: $relative); + } + public function seedsPath(string $path = '', bool $relative = false): string + { + return $this->databasePath('seeds'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR), relative: $relative); + } + + public function factoriesPath(string $path = '', bool $relative = false): string + { + return $this->databasePath('factories'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR), $relative); + } + + public function srcPath(string $path = '', bool $relative = false): string + { + return $this->path('src'.DIRECTORY_SEPARATOR. trim($path,DIRECTORY_SEPARATOR), $relative); + } +}