From b72d8dc0cf37cfd86c393f17ffcfc35f465d4901 Mon Sep 17 00:00:00 2001 From: Esdert Folkers Date: Fri, 24 Jul 2020 13:23:49 +0200 Subject: [PATCH] Make facade, more trait templates, signatures --- src/Config/laravel-stubs.php | 10 +++ src/Console/Create/CreateUser.php | 8 ++- src/Console/Make/MakeFacade.php | 91 +++++++++++++++++++++++++++ src/Console/Make/MakeInterface.php | 3 + src/Console/Make/MakeScope.php | 3 + src/Console/Make/MakeTrait.php | 26 ++++++-- src/Console/Make/MakeViewComposer.php | 3 + src/Console/Publish/PublishStubs.php | 5 +- src/Console/Run/RunFactory.php | 8 ++- src/LaravelStubsServiceProvider.php | 1 + src/stubs/facade.stub | 21 +++++++ src/stubs/trait-anonymous.stub | 29 +++++++++ src/stubs/trait-uuid.stub | 27 ++++++++ 13 files changed, 225 insertions(+), 10 deletions(-) create mode 100644 src/Console/Make/MakeFacade.php create mode 100644 src/stubs/facade.stub create mode 100644 src/stubs/trait-anonymous.stub create mode 100644 src/stubs/trait-uuid.stub diff --git a/src/Config/laravel-stubs.php b/src/Config/laravel-stubs.php index 652cecb..c410207 100644 --- a/src/Config/laravel-stubs.php +++ b/src/Config/laravel-stubs.php @@ -49,6 +49,8 @@ ], /** * make:scope + * + * Allows you to change the namespace (and so the folder) where the scope will be stored */ 'scope' => [ 'namespace' => '\App\Scopes' @@ -63,9 +65,17 @@ ], /** * make:view:composer + * + * Allows you to change the namespace (and so the folder) where the view composer will be stored */ 'view:composer' => [ 'namespace' => '\App\Http\View\Composers' + ], + /** + * make:view:composer + */ + 'facade' => [ + 'namespace' => '\App\Facades' ] ] ]; \ No newline at end of file diff --git a/src/Console/Create/CreateUser.php b/src/Console/Create/CreateUser.php index e3a65ba..53d8811 100644 --- a/src/Console/Create/CreateUser.php +++ b/src/Console/Create/CreateUser.php @@ -12,7 +12,7 @@ class CreateUser extends Command * * @var string */ - protected $signature = 'create:user'; + protected $name = 'create:user'; /** * The console command description. @@ -21,6 +21,8 @@ class CreateUser extends Command */ protected $description = 'Create an user'; + protected $signature = 'create:user'; + /** * Create a new command instance. * @@ -43,7 +45,7 @@ public function handle() if (!\class_exists($model)) { - return $this->error('Model does not exist.'); + return $this->error('Model does not exist'); } $fields = config('laravel-stubs.create.user.fields'); @@ -93,7 +95,7 @@ public function handle() $model::where(config('laravel-stubs.create.user.unique'), $saving[config('laravel-stubs.create.user.unique')])->first()) { - $this->error('This user already exists.'); + $this->error('This user already exists'); } else { diff --git a/src/Console/Make/MakeFacade.php b/src/Console/Make/MakeFacade.php new file mode 100644 index 0000000..4916481 --- /dev/null +++ b/src/Console/Make/MakeFacade.php @@ -0,0 +1,91 @@ +resolveStubPath('/../stubs/facade.stub'); + } + + protected function replaceNamespace(&$stub, $name) + { + $this->parseAccessor($stub); + + return parent::replaceNamespace($stub, $name); + } + + protected function parseAccessor(&$stub) + { + + $accessor = $this->argument('accessor'); + $accessor = Str::endsWith($accessor, '/') + ? $accessor + : '/' . $accessor; + + $accessor = \str_replace('/', '\\', $accessor); + + $stub = \str_replace('DummyAccessor', $accessor, $stub); + } + + protected function getDefaultNamespace($rootNamespace) + { + + return (empty(config('laravel-stubs.make.facade.namespace'))) + ? $rootNamespace + : config('laravel-stubs.make.facade.namespace'); + } + + protected function getNameInput() + { + + $name = trim($this->argument('name')); + + $name = Str::endsWith($name, 'Facade') + ? $name + : $name . 'Facade'; + + return $name; + } + + protected function getArguments() + { + return [ + ['name', InputArgument::REQUIRED, 'The name of the facade'], + ['accessor', InputArgument::REQUIRED, 'The accessor for the facade'] + ]; + } +} \ No newline at end of file diff --git a/src/Console/Make/MakeInterface.php b/src/Console/Make/MakeInterface.php index f5749e2..1a4f7a6 100644 --- a/src/Console/Make/MakeInterface.php +++ b/src/Console/Make/MakeInterface.php @@ -23,6 +23,9 @@ class MakeInterface extends CustomGeneratorCommand */ protected $description = 'Create a new interface'; + protected $signature = 'make:interface + {name : The name of the interface}'; + protected $type = 'Interface'; public function handle() diff --git a/src/Console/Make/MakeScope.php b/src/Console/Make/MakeScope.php index f32f37b..e828899 100644 --- a/src/Console/Make/MakeScope.php +++ b/src/Console/Make/MakeScope.php @@ -24,6 +24,9 @@ class MakeScope extends CustomGeneratorCommand */ protected $description = 'Create a new scope'; + protected $signature = 'make:scope + {name : The name of the scope}'; + protected $type = 'Scope'; public function handle() diff --git a/src/Console/Make/MakeTrait.php b/src/Console/Make/MakeTrait.php index 4ff884a..bf3b64f 100644 --- a/src/Console/Make/MakeTrait.php +++ b/src/Console/Make/MakeTrait.php @@ -24,6 +24,12 @@ class MakeTrait extends CustomGeneratorCommand */ protected $description = 'Create a new trait'; + protected $signature = 'make:trait + {name : The name of the trait} + {--b|boot : Create a boot trait} + {--a|anonymous : Create a trait to anonymous data} + {--u|uuid : Create a trait to generate an uuid field}'; + protected $type = 'Trait'; public function handle() @@ -34,9 +40,19 @@ public function handle() protected function getStub() { - return $this->option('boot') - ? $this->resolveStubPath('/../stubs/trait-boot.stub') - : $this->resolveStubPath('/../stubs/trait.stub'); + if ($this->option('boot')) { + return $this->resolveStubPath('/../stubs/trait-boot.stub'); + } + + if ($this->option('anonymous')) { + return $this->resolveStubPath('/../stubs/trait-anonymous.stub'); + } + + if ($this->option('uuid')) { + return $this->resolveStubPath('/../stubs/trait-uuid.stub'); + } + + return $this->resolveStubPath('/../stubs/trait.stub'); } protected function getDefaultNamespace($rootNamespace) @@ -69,7 +85,9 @@ protected function getArguments() protected function getOptions() { return [ - ['boot', 'b', InputOption::VALUE_NONE, 'Create a boot trait.'], + ['boot', 'b', InputOption::VALUE_NONE, 'Create a boot trait'], + ['anonymous', 'a', InputOption::VALUE_NONE, 'Create a trait to anonymous data'], + ['uuid', 'u', InputOption::VALUE_NONE, 'Create a trait to generate an uuid field'], ]; } } \ No newline at end of file diff --git a/src/Console/Make/MakeViewComposer.php b/src/Console/Make/MakeViewComposer.php index 390e795..c527c95 100644 --- a/src/Console/Make/MakeViewComposer.php +++ b/src/Console/Make/MakeViewComposer.php @@ -24,6 +24,9 @@ class MakeViewComposer extends CustomGeneratorCommand */ protected $description = 'Create a new view composer'; + protected $signature = 'make:view:composer + {name : The name of the view composer}'; + protected $type = 'View composer'; public function handle() diff --git a/src/Console/Publish/PublishStubs.php b/src/Console/Publish/PublishStubs.php index a12d409..b00a88c 100644 --- a/src/Console/Publish/PublishStubs.php +++ b/src/Console/Publish/PublishStubs.php @@ -23,6 +23,9 @@ class PublishStubs extends Command */ protected $description = 'Publish the stubs of this package'; + protected $signature = 'publish:stubs + {--f|force : Overwrite any existing files}'; + /** * Create a new command instance. * @@ -47,7 +50,7 @@ public function handle() File::copyDirectory(__DIR__.'/../../stubs/', $stubsPath); - return $this->info('Stubs published successfully.'); + return $this->info('Stubs published successfully'); } protected function getOptions() diff --git a/src/Console/Run/RunFactory.php b/src/Console/Run/RunFactory.php index 17650ae..836bdf3 100644 --- a/src/Console/Run/RunFactory.php +++ b/src/Console/Run/RunFactory.php @@ -22,6 +22,10 @@ class RunFactory extends Command */ protected $description = 'Run a factory'; + protected $signature = 'run:factory + {model= : The model to run the factory for} + {amount= : The amount of records generated by the factory}'; + /** * Create a new command instance. * @@ -100,8 +104,8 @@ protected function getAmountOption() protected function getArguments() { return [ - ['model', InputArgument::OPTIONAL, 'The model to run the factory for.'], - ['amount', InputArgument::OPTIONAL, 'The amount of records generated by the factory.'], + ['model', InputArgument::OPTIONAL, 'The model to run the factory for'], + ['amount', InputArgument::OPTIONAL, 'The amount of records generated by the factory'], ]; } } \ No newline at end of file diff --git a/src/LaravelStubsServiceProvider.php b/src/LaravelStubsServiceProvider.php index 7f68394..57785ff 100644 --- a/src/LaravelStubsServiceProvider.php +++ b/src/LaravelStubsServiceProvider.php @@ -19,6 +19,7 @@ public function boot() { Console\Make\MakeScope::class, Console\Make\MakeInterface::class, Console\Make\MakeViewComposer::class, + Console\Make\MakeFacade::class, /** Publish */ Console\Publish\PublishStubs::class, /** Run */ diff --git a/src/stubs/facade.stub b/src/stubs/facade.stub new file mode 100644 index 0000000..db35680 --- /dev/null +++ b/src/stubs/facade.stub @@ -0,0 +1,21 @@ +$field = null; + } + + $model->save(); + }); + } +} \ No newline at end of file diff --git a/src/stubs/trait-uuid.stub b/src/stubs/trait-uuid.stub new file mode 100644 index 0000000..136d6bc --- /dev/null +++ b/src/stubs/trait-uuid.stub @@ -0,0 +1,27 @@ +$uuidField = Str::uuid(); + }); + } +} \ No newline at end of file