diff --git a/src/Config/laravel-stubs.php b/src/Config/laravel-stubs.php index f59378d..d2da30d 100644 --- a/src/Config/laravel-stubs.php +++ b/src/Config/laravel-stubs.php @@ -49,6 +49,14 @@ 'facade' => [ 'namespace' => '\App\Facades' ], + /** + * make:helper + * + * Allows you to change the location of the helpers. + */ + 'helper' => [ + 'folder' => 'Helpers' + ], /** * make:interface * diff --git a/src/Console/Make/MakeHelper.php b/src/Console/Make/MakeHelper.php new file mode 100644 index 0000000..c290938 --- /dev/null +++ b/src/Console/Make/MakeHelper.php @@ -0,0 +1,101 @@ +resolveStubPath('/../stubs/helper.stub'); + } + + protected function replaceNamespace(&$stub, $name) + { + $this->parseFunctionName($stub); + + return parent::replaceNamespace($stub, $name); + } + + protected function parseFunctionName(&$stub) + { + $functionName = $this->getNameInput(); + $functionName = \preg_replace('/helper$/i', '', $functionName); + $functionName = Str::snake($functionName); + + $stub = \str_replace('DummyFunction', $functionName, $stub); + } + + public function handle() + { + $name = $this->getNameInput(); + + $path = $this->getPath($name); + + // First we will check to see if the class already exists. If it does, we don't want + // to create the class and overwrite the user's code. So, we will bail out so the + // code is untouched. Otherwise, we will continue generating this class' files. + if ((! $this->hasOption('force') || + ! $this->option('force')) && + $this->alreadyExists($this->getNameInput())) { + $this->error($this->type.' already exists!'); + + return false; + } + + // Next, we will generate the path to the location where this class' file should get + // written. Then, we will build the class and make the proper replacements on the + // stub files so that it gets the correctly formatted namespace and class name. + $this->makeDirectory($path); + + $this->files->put($path, $this->sortImports($this->buildClass($name))); + + $this->info($this->type.' created successfully.'); + } + + protected function getPath($name) + { + return $this->laravel['path'].'/'.config('laravel-stubs.make.helper.folder').'/'.str_replace('\\', '/', $name).'.php'; + } + + protected function getNameInput() + { + $name = trim($this->argument('name')); + + $name = Str::endsWith($name, 'Helper') + ? $name + : $name . 'Helper'; + + return $name; + } + + protected function getArguments() + { + return [ + ['name', InputArgument::REQUIRED, 'The name of the helper'] + ]; + } +} \ No newline at end of file diff --git a/src/LaravelStubsServiceProvider.php b/src/LaravelStubsServiceProvider.php index 42c1eb3..0511637 100644 --- a/src/LaravelStubsServiceProvider.php +++ b/src/LaravelStubsServiceProvider.php @@ -22,6 +22,7 @@ public function boot() Console\Make\MakeInterface::class, Console\Make\MakeViewComposer::class, Console\Make\MakeFacade::class, + Console\Make\MakeHelper::class, /** Patch */ Console\Patch::class, /** Publish */ diff --git a/src/stubs/helper.stub b/src/stubs/helper.stub new file mode 100644 index 0000000..5509eee --- /dev/null +++ b/src/stubs/helper.stub @@ -0,0 +1,15 @@ +