From aaa126c0297a9308a83e81d13131666c04ffba99 Mon Sep 17 00:00:00 2001 From: Shavonn Brown Date: Tue, 2 Jan 2024 09:06:17 -0500 Subject: [PATCH] init:micro (#31) * service dir structure * generating directories via elements config * clean up make:service * make providers and route files * add welcome file, update tests, skip domain dir if disabled, change stubs * make services config like domain * check service existence, both kinds * pint --- .../resources/views/welcome.blade.php | 17 ++ .../resources/views/welcome.blade.php | 17 ++ .../resources/views/welcome.blade.php | 17 ++ .../Testy/resources/views/welcome.blade.php | 17 ++ config/config.php | 37 ++- docs/config-domains.md | 15 +- docs/config-structure.md | 4 +- src/Commands/DomainListCommand.php | 2 +- src/Commands/InitMicroserviceCommand.php | 215 ++++++++++++++++++ src/Commands/InitStructureCommand.php | 49 +--- src/Commands/LaracaGeneratorCommand.php | 215 ++++++++++++++++++ src/Commands/MakeCastCommand.php | 4 +- src/Commands/MakeChannelCommand.php | 4 +- src/Commands/MakeCommandCommand.php | 4 +- src/Commands/MakeComponentCommand.php | 4 +- src/Commands/MakeControllerCommand.php | 4 +- src/Commands/MakeEnumCommand.php | 4 +- src/Commands/MakeEventCommand.php | 4 +- src/Commands/MakeExceptionCommand.php | 4 +- src/Commands/MakeFactoryCommand.php | 1 - src/Commands/MakeJobCommand.php | 4 +- src/Commands/MakeListenerCommand.php | 4 +- src/Commands/MakeMailCommand.php | 4 +- src/Commands/MakeMiddlewareCommand.php | 4 +- src/Commands/MakeModelCommand.php | 6 +- src/Commands/MakeNotificationCommand.php | 4 +- src/Commands/MakeObserverCommand.php | 4 +- src/Commands/MakePolicyCommand.php | 4 +- src/Commands/MakeRequestCommand.php | 4 +- src/Commands/MakeResourceCommand.php | 4 +- src/Commands/MakeRuleCommand.php | 4 +- src/Commands/MakeScopeCommand.php | 4 +- src/Commands/MakeSeederCommand.php | 3 +- src/Commands/MakeServiceCommand.php | 117 +++------- src/Commands/MakeTestCommand.php | 9 +- src/Commands/MakeValueCommand.php | 4 +- src/Commands/MakeViewCommand.php | 4 +- src/Commands/Traits/CreatesView.php | 2 +- src/Commands/Traits/Directable.php | 32 +++ src/Commands/Traits/Domainable.php | 24 -- src/Commands/Traits/HasLocalStub.php | 28 --- src/Commands/Traits/LaracaCommand.php | 58 ++++- src/Commands/stubs/routes-api.stub | 21 ++ src/Commands/stubs/routes-channels.stub | 13 ++ src/Commands/stubs/routes-web.stub | 19 ++ .../stubs/serviceprovider-broadcast.stub | 19 ++ src/Commands/stubs/serviceprovider-route.stub | 38 ++++ src/Commands/stubs/serviceprovider.stub | 30 +++ src/Commands/stubs/welcome.stub | 17 ++ src/Exceptions/InvalidConfigKeyException.php | 5 +- src/Exceptions/StubNotFoundException.php | 10 + src/LaracaServiceProvider.php | 10 +- src/Traits/GetsConfigValues.php | 89 +++++--- tests/Feature/ArtiMigrationCommandTest.php | 4 +- tests/Feature/DomainListTest.php | 16 +- tests/Feature/DomainsTest.php | 41 +++- tests/Feature/InitMicroserviceCommandTest.php | 83 +++++++ tests/Feature/InitStructureCommandTest.php | 64 +++--- tests/Feature/MakeCastCommandTest.php | 2 +- tests/Feature/MakeChannelCommandTest.php | 2 +- tests/Feature/MakeCommandCommandTest.php | 2 +- tests/Feature/MakeComponentCommandTest.php | 51 ----- tests/Feature/MakeControllerCommandTest.php | 2 +- tests/Feature/MakeEnumCommandTest.php | 2 +- tests/Feature/MakeEventCommandTest.php | 2 +- tests/Feature/MakeExceptionCommandTest.php | 2 +- tests/Feature/MakeFactoryCommandTest.php | 2 +- tests/Feature/MakeJobCommandTest.php | 2 +- tests/Feature/MakeListenerCommandTest.php | 2 +- tests/Feature/MakeMailCommandTest.php | 2 +- tests/Feature/MakeMiddlewareCommandTest.php | 2 +- tests/Feature/MakeModelCommandTest.php | 4 +- tests/Feature/MakeNotificationCommandTest.php | 2 +- tests/Feature/MakeObserverCommandTest.php | 2 +- tests/Feature/MakePolicyCommandTest.php | 2 +- tests/Feature/MakeProviderCommandTest.php | 2 +- tests/Feature/MakeRequestCommandTest.php | 2 +- tests/Feature/MakeResourceCommandTest.php | 2 +- tests/Feature/MakeRuleCommandTest.php | 2 +- tests/Feature/MakeScopeCommandTest.php | 2 +- tests/Feature/MakeSeederCommandTest.php | 2 +- tests/Feature/MakeServiceCommandTest.php | 9 +- tests/Feature/MakeTestCommandTest.php | 2 +- tests/Feature/MakeValueCommandTest.php | 2 +- tests/Feature/MakeViewCommandTest.php | 2 +- tests/Pest.php | 6 +- tests/Unit/ConfigPathTest.php | 8 +- 87 files changed, 1130 insertions(+), 444 deletions(-) create mode 100644 Test/Microservices/DataThing/resources/views/welcome.blade.php create mode 100644 Test/Microservices/New_Object/resources/views/welcome.blade.php create mode 100644 Test/Microservices/PackageClass/resources/views/welcome.blade.php create mode 100644 Test/Microservices/Testy/resources/views/welcome.blade.php create mode 100644 src/Commands/InitMicroserviceCommand.php create mode 100644 src/Commands/LaracaGeneratorCommand.php create mode 100644 src/Commands/Traits/Directable.php delete mode 100644 src/Commands/Traits/Domainable.php delete mode 100644 src/Commands/Traits/HasLocalStub.php create mode 100644 src/Commands/stubs/routes-api.stub create mode 100644 src/Commands/stubs/routes-channels.stub create mode 100644 src/Commands/stubs/routes-web.stub create mode 100644 src/Commands/stubs/serviceprovider-broadcast.stub create mode 100644 src/Commands/stubs/serviceprovider-route.stub create mode 100644 src/Commands/stubs/serviceprovider.stub create mode 100644 src/Commands/stubs/welcome.stub create mode 100644 src/Exceptions/StubNotFoundException.php create mode 100644 tests/Feature/InitMicroserviceCommandTest.php delete mode 100644 tests/Feature/MakeComponentCommandTest.php diff --git a/Test/Microservices/DataThing/resources/views/welcome.blade.php b/Test/Microservices/DataThing/resources/views/welcome.blade.php new file mode 100644 index 0000000..936395c --- /dev/null +++ b/Test/Microservices/DataThing/resources/views/welcome.blade.php @@ -0,0 +1,17 @@ + + + + + + Welcome to DataThing + + +
+ +

DataThing

+
+ + + \ No newline at end of file diff --git a/Test/Microservices/New_Object/resources/views/welcome.blade.php b/Test/Microservices/New_Object/resources/views/welcome.blade.php new file mode 100644 index 0000000..38b324b --- /dev/null +++ b/Test/Microservices/New_Object/resources/views/welcome.blade.php @@ -0,0 +1,17 @@ + + + + + + Welcome to New_Object + + +
+ +

New_Object

+
+ + + \ No newline at end of file diff --git a/Test/Microservices/PackageClass/resources/views/welcome.blade.php b/Test/Microservices/PackageClass/resources/views/welcome.blade.php new file mode 100644 index 0000000..af5627f --- /dev/null +++ b/Test/Microservices/PackageClass/resources/views/welcome.blade.php @@ -0,0 +1,17 @@ + + + + + + Welcome to PackageClass + + +
+ +

PackageClass

+
+ + + \ No newline at end of file diff --git a/Test/Microservices/Testy/resources/views/welcome.blade.php b/Test/Microservices/Testy/resources/views/welcome.blade.php new file mode 100644 index 0000000..4b935b6 --- /dev/null +++ b/Test/Microservices/Testy/resources/views/welcome.blade.php @@ -0,0 +1,17 @@ + + + + + + Welcome to Testy + + +
+ +

Testy

+
+ + + \ No newline at end of file diff --git a/config/config.php b/config/config.php index 3613849..8e4e76c 100644 --- a/config/config.php +++ b/config/config.php @@ -4,23 +4,12 @@ * Laraca config */ return [ - /* - |-------------------------------------------------------------------------- - | Domains - |-------------------------------------------------------------------------- - | Enables optional domain argument in make commands - */ - 'domains' => [ - 'enabled' => true, - 'parent_dir' => 'Domains', // dir name or null - ], - /* |-------------------------------------------------------------------------- | Directory structure config |-------------------------------------------------------------------------- */ - 'structure' => [ + 'struct' => [ /* |-------------------------------------------------------------------------- | Cast Path @@ -87,6 +76,18 @@ 'parent' => 'base', ], + /* + |-------------------------------------------------------------------------- + | Domains + |-------------------------------------------------------------------------- + | Enables optional domain argument in make commands + */ + 'domain' => [ + 'enabled' => true, + 'path' => 'Domains', // dir name or null + 'parent' => 'app', + ], + /* |-------------------------------------------------------------------------- | Enum Path @@ -164,6 +165,18 @@ 'parent' => 'app', ], + /* + |-------------------------------------------------------------------------- + | Microservice Path + |-------------------------------------------------------------------------- + | make:micro + */ + 'microservice' => [ + 'enabled' => true, + 'path' => 'Microservices', + 'parent' => 'app', + ], + /* |-------------------------------------------------------------------------- | Middleware Path diff --git a/docs/config-domains.md b/docs/config-domains.md index 6ac4870..30550ce 100644 --- a/docs/config-domains.md +++ b/docs/config-domains.md @@ -5,19 +5,20 @@ If you're interested in domain-driven design or even separating your application ```php 'domains' => [ 'enabled' => true, - 'parent_dir' => 'Domains', // dir name or null + 'path' => 'Domains', // dir name or null + 'parent' => 'app', ], ``` -| Option | Description | -| ------------ | :---------------------------------------------------------------------: | -| `parent_dir` | The directory you want to use as a parent. It can be whatever you like. | +| Option | Description | +| ------ | :---------------------------------------------------------------------: | +| `path` | The directory you want to use as a parent. It can be whatever you like. | Examples: -- `'parent_dir' => 'Domains'` => `(app/Domains/Foo)` -- `'parent_dir' => 'Service'` => `(app/Service/Foo)` -- `'parent_dir' => null` => `(app/Foo)` +- `'path' => 'Domains'` => `(app/Domains/Foo)` +- `'path' => 'Service'` => `(app/Service/Foo)` +- `'path' => null` => `(app/Foo)` ## Use with make commands diff --git a/docs/config-structure.md b/docs/config-structure.md index 3969fd9..7e8836f 100644 --- a/docs/config-structure.md +++ b/docs/config-structure.md @@ -6,7 +6,7 @@ outline: deep Generally, every key in the config corresponds to a fairly obvious command. -Ex: `cast` key in `structure` corresponds to `make:cast` +Ex: `cast` key in `struct` corresponds to `make:cast` ## Buildng Your Structure @@ -35,7 +35,7 @@ Results: `app/Data/Models` ### Via Parent -Additional keys can be added to the `structure` to serve as parent directories. +Additional keys can be added to the `struct` to serve as parent directories. ```php 'data' => [ diff --git a/src/Commands/DomainListCommand.php b/src/Commands/DomainListCommand.php index 3f5de4e..4df98f9 100644 --- a/src/Commands/DomainListCommand.php +++ b/src/Commands/DomainListCommand.php @@ -57,7 +57,7 @@ public function handle() { $domains = []; - $domainDir = Config::get('laraca.domains.parent_dir'); + $domainDir = Config::get('laraca.struct.domain.path'); $domainPath = app_path($domainDir); if ($this->files->isDirectory($domainPath)) { diff --git a/src/Commands/InitMicroserviceCommand.php b/src/Commands/InitMicroserviceCommand.php new file mode 100644 index 0000000..4965b11 --- /dev/null +++ b/src/Commands/InitMicroserviceCommand.php @@ -0,0 +1,215 @@ +serviceName = $this->getClassName($this->input->getArgument('name')); + $servicePath = $this->getGenerationPath('microservice'); + $this->servicePath = "$servicePath/$this->serviceName"; + + if (! parent::handle()) { + return false; + } + + $this->makeDirectories(); + + $this->makeProviders(); + + $this->makeRouteFiles(); + + $this->makeFile('welcome', __DIR__.'/stubs/welcome.stub'); + + $this->components->bulletList($this->generated); + $this->components->info('Microservice created successfully.'); + $this->components->info('Don\'t forget to '.$this->serviceName.'ServiceProvider.php to providers in app.php'); + + return Command::SUCCESS; + } + + /** + * Replace the tags for the given stub. + */ + protected function replaceTags(string &$stub, string $name): string + { + $namespace = $this->getDefaultNamespace($this->rootNamespace()); + $controllerNamespace = $this->assembleNamespace('controller', null, $this->serviceName); + + $search = ['{{ namespace }}', '{{ slug }}', '{{ service }}', '{{ controller_namespace }}']; + $replace = [$namespace, Str::slug($this->serviceName), $this->serviceName, $controllerNamespace]; + + $stub = str_replace($search, $replace, $stub); + + return $stub; + } + + /** + * Get the console command arguments. + */ + protected function makeRouteFiles(): void + { + $this->makeFile('web', __DIR__.'/stubs/routes-web.stub'); + $this->makeFile('api', __DIR__.'/stubs/routes-api.stub'); + $this->makeFile('channels', __DIR__.'/stubs/routes-channels.stub'); + } + + /** + * Get the console command arguments. + */ + protected function makeDirectories(): void + { + $elements = [ + 'channel', + 'controller', + 'provider', + 'route', + 'test', + 'view', + ]; + + foreach ($elements as $element) { + switch ($element) { + case 'route': + $path = "$this->servicePath/routes"; + break; + case 'test': + $path = "$this->servicePath/".$this->assembleRelativePath('test', null, null, false); + break; + case 'view': + $path = "$this->servicePath/".$this->assembleRelativePath('view', null, null, false); + break; + + default: + $path = $this->assembleFullPath($element, null, $this->serviceName); + break; + } + + $this->makeDirectory($path); + } + } + + /** + * Get the console command arguments. + */ + protected function makeProviders() + { + // RouteServiceProvider + $this->makeFile('RouteServiceProvider', __DIR__.'/stubs/serviceprovider-route.stub'); + + // BroadcastServiceProvider + $this->makeFile('BroadcastServiceProvider', __DIR__.'/stubs/serviceprovider-broadcast.stub'); + + // Root ServiceProvider + $serviceProviderName = Str::of($this->serviceName)->finish('ServiceProvider'); + $this->makeFile($serviceProviderName, __DIR__.'/stubs/serviceprovider.stub'); + } + + /** + * Get the destination class path. + */ + protected function getPath(string $name): string + { + $name = Str::of($name)->replaceFirst($this->getDefaultNamespace($this->rootNamespace()), '')->replace('\\', '/'); + + $path = $this->servicePath; + + switch ($name) { + case 'BroadcastServiceProvider': + case 'RouteServiceProvider': + $path = $this->assembleFullPath('provider', null, $this->serviceName)."/$name.php"; + break; + case 'api': + case 'channels': + case 'web': + $path = $path."/routes/$name.php"; + break; + case 'welcome': + $path = app_path($this->assembleRelativePath('view', null, $this->serviceName)."/$name.blade.php"); + break; + default: + $path = $path."/$name.php"; + } + + return $path; + } + + protected function getDefaultNamespace($rootNamespace): string + { + $path = $this->assembleNamespace('microservice'); + + return "$path\\$this->serviceName"; + } + + /** + * Determine if the class already exists. + * + * @param string $rawName + * @return bool + */ + protected function alreadyExists($rawName) + { + $serviceProviderName = Str::of($this->serviceName)->finish('ServiceProvider'); + + return $this->files->exists($this->getPath($serviceProviderName)); + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return [ + ['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)], + ]; + } +} diff --git a/src/Commands/InitStructureCommand.php b/src/Commands/InitStructureCommand.php index 77508a4..098a12f 100644 --- a/src/Commands/InitStructureCommand.php +++ b/src/Commands/InitStructureCommand.php @@ -2,17 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Console\Command; -use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Config; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'init:structure')] -class InitStructureCommand extends Command +class InitStructureCommand extends LaracaGeneratorCommand { - use LaracaCommand; - /** * The console command name. * @@ -27,25 +23,6 @@ class InitStructureCommand extends Command */ protected $description = 'Create a new structure object class'; - /** - * The filesystem instance. - * - * @var \Illuminate\Filesystem\Filesystem - */ - protected $files; - - /** - * Create a new controller creator command instance. - * - * @return void - */ - public function __construct(Filesystem $files) - { - parent::__construct(); - - $this->files = $files; - } - /** * Execute the console command. * @@ -53,30 +30,22 @@ public function __construct(Filesystem $files) */ public function handle() { - $config = Config::get('laraca.structure'); - $messages = []; + $config = Config::get('laraca.struct'); - $this->components->info('Creating directy structure from Laraca config.'); + $this->components->info('Creating directory structure from Laraca config.'); foreach (array_keys($config) as $key) { - $fullPath = self::assembleFullPath($key); - $relativePath = self::assembleRelativePath($key); - - if (! $this->files->isDirectory($fullPath)) { - $this->files->makeDirectory($fullPath, 0777, true, true); - $state = 'created'; - } else { - $state = ' already exists'; + if ($key == 'domain' && ! $config['domain']['enabled']) { + continue; + } elseif ($key == 'microservice' && ! $config['microservice']['enabled']) { + continue; } - if ($this->files->isEmptyDirectory($fullPath)) { - $this->files->put($fullPath.'/.gitkeep', ''); - } + $fullPath = self::assembleFullPath($key); - array_push($messages, sprintf('[%s] %s.', $relativePath, $state)); + $this->makeEmptyDirectory($fullPath); } - $this->components->bulletList($messages); $this->components->info('Configured structure generated successfully.'); return Command::SUCCESS; diff --git a/src/Commands/LaracaGeneratorCommand.php b/src/Commands/LaracaGeneratorCommand.php new file mode 100644 index 0000000..7a6dc08 --- /dev/null +++ b/src/Commands/LaracaGeneratorCommand.php @@ -0,0 +1,215 @@ +key = strtolower($this->type); + $this->files = $files; + } + + /** + * Execute the console command. + * + * @return bool|null + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + public function handle() + { + if ((! $this->hasOption('force') || + ! $this->option('force')) && + $this->alreadyExists($this->input->getArgument('name')) + ) { + $this->components->error($this->type.' already exists.'); + + return false; + } + + return true; + } + + /** + * Build the directory for the class if necessary. + * + * @param string $path + * @return string + */ + protected function makeDirectory($path) + { + if (! $this->files->isDirectory($path)) { + $this->files->makeDirectory($path, 0777, true, true); + } + + return $path; + } + + /** + * Build the directory for the class if necessary. + * + * @param string $path + * @return string + */ + protected function makeEmptyDirectory($path) + { + $this->makeDirectory($path); + + if ($this->files->isEmptyDirectory($path)) { + $this->files->put($path.'/.gitkeep', ''); + } + + return $path; + } + + /** + * Build the class with the given name. + * + * @param string $name + * @param string $stub + * @return string + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + protected function build($name, $stub) + { + $stub = $this->files->get($stub); + + return $this->replaceNamespace($stub, $name); + } + + /** + * Build the class with the given name. + * + * @param string $name + * @param string $stub + * @return string + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + protected function makeFile($name, $stub) + { + $path = $this->getPath($name); + $this->makeDirectory(dirname($path)); + + $stub = $this->files->get($stub); + + $content = $this->replaceTags($stub, $name); + $this->files->put($path, $content); + + array_push($this->generated, $path); + + return $path; + } + + /** + * Replace the tags for the given stub. + */ + protected function replaceTags(string &$stub, string $name): string + { + $search = ['{{ namespace }}', '{{ class }}']; + $replace = [$this->assembleNamespace($this->key), $name]; + + $stub = str_replace($search, $replace, $stub); + + return $stub; + } + + /** + * Get the destination class path. + */ + protected function getPath(string $name): string + { + $name = Str::of($name)->replaceFirst($this->rootNamespace(), '')->replace('\\', '/'); + + return self::assembleFullPath($this->key)."/$name.php"; + } + + /** + * Get the default namespace for the class. + * + * @param string $rootNamespace + * @return string + */ + protected function getDefaultNamespace($rootNamespace) + { + return $this->getClassNamespace($this->key); + } + + /** + * Determine if the class already exists. + * + * @param string $rawName + * @return bool + */ + protected function alreadyExists($rawName) + { + return $this->files->exists($this->getPath($this->getClassName($rawName))); + } + + /** + * Get the root namespace for the class. + * + * @return string + */ + protected function rootNamespace() + { + return $this->laravel->getNamespace(); + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['force', 'f', InputOption::VALUE_NONE, 'Create the '.strtolower($this->type).' even if it already exists'], + ]; + } +} diff --git a/src/Commands/MakeCastCommand.php b/src/Commands/MakeCastCommand.php index b000e4e..534cd9e 100644 --- a/src/Commands/MakeCastCommand.php +++ b/src/Commands/MakeCastCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\CastMakeCommand; class MakeCastCommand extends CastMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeChannelCommand.php b/src/Commands/MakeChannelCommand.php index 0f10a5e..7e479da 100644 --- a/src/Commands/MakeChannelCommand.php +++ b/src/Commands/MakeChannelCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ChannelMakeCommand; class MakeChannelCommand extends ChannelMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeCommandCommand.php b/src/Commands/MakeCommandCommand.php index b49c341..1fd3627 100644 --- a/src/Commands/MakeCommandCommand.php +++ b/src/Commands/MakeCommandCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ConsoleMakeCommand; class MakeCommandCommand extends ConsoleMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeComponentCommand.php b/src/Commands/MakeComponentCommand.php index e90cb6e..9b55da7 100644 --- a/src/Commands/MakeComponentCommand.php +++ b/src/Commands/MakeComponentCommand.php @@ -3,14 +3,14 @@ namespace HandsomeBrown\Laraca\Commands; use HandsomeBrown\Laraca\Commands\Traits\CreatesView; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ComponentMakeCommand; class MakeComponentCommand extends ComponentMakeCommand { use CreatesView; - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeControllerCommand.php b/src/Commands/MakeControllerCommand.php index 4ac1aec..4988d3c 100644 --- a/src/Commands/MakeControllerCommand.php +++ b/src/Commands/MakeControllerCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Routing\Console\ControllerMakeCommand; class MakeControllerCommand extends ControllerMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeEnumCommand.php b/src/Commands/MakeEnumCommand.php index a09ef7d..a19bb8f 100644 --- a/src/Commands/MakeEnumCommand.php +++ b/src/Commands/MakeEnumCommand.php @@ -2,7 +2,7 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; @@ -10,7 +10,7 @@ #[AsCommand(name: 'make:enum')] class MakeEnumCommand extends GeneratorCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * The console command name. diff --git a/src/Commands/MakeEventCommand.php b/src/Commands/MakeEventCommand.php index 0fc657d..5aaa6e1 100644 --- a/src/Commands/MakeEventCommand.php +++ b/src/Commands/MakeEventCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\EventMakeCommand; class MakeEventCommand extends EventMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeExceptionCommand.php b/src/Commands/MakeExceptionCommand.php index db7266f..76e26b5 100644 --- a/src/Commands/MakeExceptionCommand.php +++ b/src/Commands/MakeExceptionCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ExceptionMakeCommand; class MakeExceptionCommand extends ExceptionMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeFactoryCommand.php b/src/Commands/MakeFactoryCommand.php index 09954fc..3979193 100644 --- a/src/Commands/MakeFactoryCommand.php +++ b/src/Commands/MakeFactoryCommand.php @@ -19,7 +19,6 @@ class MakeFactoryCommand extends FactoryMakeCommand protected $name = 'make:factory'; /** - * getPath * Get the destination class path. * * @param string $name diff --git a/src/Commands/MakeJobCommand.php b/src/Commands/MakeJobCommand.php index 5a50242..f0ee5bd 100644 --- a/src/Commands/MakeJobCommand.php +++ b/src/Commands/MakeJobCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\JobMakeCommand; class MakeJobCommand extends JobMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeListenerCommand.php b/src/Commands/MakeListenerCommand.php index b9d940c..09d36cc 100644 --- a/src/Commands/MakeListenerCommand.php +++ b/src/Commands/MakeListenerCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ListenerMakeCommand; class MakeListenerCommand extends ListenerMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeMailCommand.php b/src/Commands/MakeMailCommand.php index e77ecf5..d419ec3 100644 --- a/src/Commands/MakeMailCommand.php +++ b/src/Commands/MakeMailCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\MailMakeCommand; class MakeMailCommand extends MailMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeMiddlewareCommand.php b/src/Commands/MakeMiddlewareCommand.php index 9f31b46..f57435a 100644 --- a/src/Commands/MakeMiddlewareCommand.php +++ b/src/Commands/MakeMiddlewareCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Routing\Console\MiddlewareMakeCommand; class MakeMiddlewareCommand extends MiddlewareMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeModelCommand.php b/src/Commands/MakeModelCommand.php index ef19259..cdaf043 100644 --- a/src/Commands/MakeModelCommand.php +++ b/src/Commands/MakeModelCommand.php @@ -2,16 +2,14 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; -use HandsomeBrown\Laraca\Commands\Traits\HasLocalStub; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ModelMakeCommand; use Symfony\Component\Console\Input\InputOption; class MakeModelCommand extends ModelMakeCommand { - use Domainable, LaracaCommand; - use HasLocalStub; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeNotificationCommand.php b/src/Commands/MakeNotificationCommand.php index 05a7af0..15358e6 100644 --- a/src/Commands/MakeNotificationCommand.php +++ b/src/Commands/MakeNotificationCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\NotificationMakeCommand; class MakeNotificationCommand extends NotificationMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeObserverCommand.php b/src/Commands/MakeObserverCommand.php index a6c965c..ce5225b 100644 --- a/src/Commands/MakeObserverCommand.php +++ b/src/Commands/MakeObserverCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ObserverMakeCommand; class MakeObserverCommand extends ObserverMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakePolicyCommand.php b/src/Commands/MakePolicyCommand.php index 3700b79..973ae32 100644 --- a/src/Commands/MakePolicyCommand.php +++ b/src/Commands/MakePolicyCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\PolicyMakeCommand; class MakePolicyCommand extends PolicyMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeRequestCommand.php b/src/Commands/MakeRequestCommand.php index b5393c7..35bd8e6 100644 --- a/src/Commands/MakeRequestCommand.php +++ b/src/Commands/MakeRequestCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\RequestMakeCommand; class MakeRequestCommand extends RequestMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeResourceCommand.php b/src/Commands/MakeResourceCommand.php index 557b03f..d23c635 100644 --- a/src/Commands/MakeResourceCommand.php +++ b/src/Commands/MakeResourceCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ResourceMakeCommand; class MakeResourceCommand extends ResourceMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeRuleCommand.php b/src/Commands/MakeRuleCommand.php index 9ac6c03..92b4948 100644 --- a/src/Commands/MakeRuleCommand.php +++ b/src/Commands/MakeRuleCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\RuleMakeCommand; class MakeRuleCommand extends RuleMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeScopeCommand.php b/src/Commands/MakeScopeCommand.php index 1e8381f..d85c045 100644 --- a/src/Commands/MakeScopeCommand.php +++ b/src/Commands/MakeScopeCommand.php @@ -2,13 +2,13 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ScopeMakeCommand; class MakeScopeCommand extends ScopeMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/MakeSeederCommand.php b/src/Commands/MakeSeederCommand.php index ab90580..b1de671 100644 --- a/src/Commands/MakeSeederCommand.php +++ b/src/Commands/MakeSeederCommand.php @@ -19,14 +19,13 @@ class MakeSeederCommand extends SeederMakeCommand protected $name = 'make:seeder'; /** - * getPath * Get the destination class path. * * @param string $name */ protected function getPath($name): string { - $name = Str::replace('\\', '/', Str::replaceFirst($this->rootNamespace(), '', $name)); + $name = Str::of($name)->replaceFirst($this->rootNamespace(), '')->replace('\\', '/'); return self::assembleFullPath('seeder')."/$name.php"; } diff --git a/src/Commands/MakeServiceCommand.php b/src/Commands/MakeServiceCommand.php index 93cdf74..fef62eb 100644 --- a/src/Commands/MakeServiceCommand.php +++ b/src/Commands/MakeServiceCommand.php @@ -2,17 +2,17 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Console\Concerns\CreatesMatchingTest; -use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Input\InputArgument; #[AsCommand(name: 'make:service')] -class MakeServiceCommand extends GeneratorCommand +class MakeServiceCommand extends LaracaGeneratorCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * The console command name. @@ -44,91 +44,40 @@ class MakeServiceCommand extends GeneratorCommand */ public function handle() { - if ($this->isReservedName($this->getNameInput())) { - $this->components->error('The name "'.$this->getNameInput().'" is reserved by PHP.'); + $name = $this->getClassName($this->input->getArgument('name')); + if (! parent::handle()) { return false; } - $name = ucfirst($this->getNameInput()); - $name = $this->qualifyClass($name); - $name = Str::of($name)->endsWith('Service') ? $name : Str::of($name)->finish('Service'); - $path = $this->getPath($name); - $interfaceName = Str::of($name)->finish('Interface'); - $interfacePath = $this->getPath($interfaceName); + $interface = Str::of($name)->finish('Interface'); - if ((! $this->hasOption('force') || - ! $this->option('force')) && - $this->alreadyExists($this->getNameInput())) { - $this->components->error($this->type.' already exists.'); - - return false; - } - - $this->makeDirectory($path); - $this->files->put($path, $this->sortImports($this->buildClass($name))); - - $this->makeDirectory($interfacePath); - $this->files->put($interfacePath, $this->sortImports($this->buildInterface($interfaceName))); + $interfacePath = $this->makeFile($interface, __DIR__.'/stubs/interface.stub'); + $servicePath = $this->makeFile($name, __DIR__.'/stubs/service.stub'); $info = $this->type; if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) { - if ($this->handleTestCreation($path)) { + if ($this->handleTestCreation($servicePath)) { $info .= ' and test'; } } if (windows_os()) { - $path = str_replace('/', '\\', $path); + $servicePath = str_replace('/', '\\', $servicePath); } - $this->components->info(sprintf('%s [%s] and interface [%s] created successfully.', $info, $path, $interfacePath)); + $this->components->info(sprintf('%s [%s] and interface [%s] created successfully.', $info, $servicePath, $interfacePath)); } /** - * Get the destination class path. - * - * @param string $name + * Get the class name */ - protected function getPath($name): string + protected function getClassName($name): string { - $name = Str::replaceFirst($this->rootNamespace(), '', $name); + $name = parent::getClassName($name); - return self::assembleFullPath('service').Str::replace('\\', '/', $name).'.php'; - } - - /** - * Parse the class name and format according to the root namespace. - * - * @param string $name - * @return string - */ - protected function qualifyClass($name) - { - $name = parent::qualifyClass($name); - - return Str::ucfirst($name); - } - - /** - * Get the stub file for the generator. - * - * @return string - */ - protected function getStub() - { - return __DIR__.'/stubs/service.stub'; - } - - /** - * Get the stub file for the generator. - * - * @return string - */ - protected function getInterfaceStub() - { - return __DIR__.'/stubs/interface.stub'; + return Str::of($name)->endsWith('Service') ? $name : Str::of($name)->finish('Service'); } /** @@ -140,35 +89,25 @@ protected function getInterfaceStub() */ protected function replaceNamespace(&$stub, $name) { - parent::replaceNamespace($stub, $name); - - $interface = Str::of($name)->replaceFirst($this->rootNamespace(), '')->replace('\\', '')->finish('Interface'); - $stub = str_replace('{{ interface }}', $interface, $stub); + $interface = Str::of($name)->finish('Interface'); - return $this; - } + $search = ['{{ namespace }}', '{{ class }}', '{{ interface }}']; + $replace = [$this->assembleNamespace('service'), $name, $interface]; - /** - * Build the class with the given name. - * - * @param string $name - * @return string - * - * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException - */ - protected function buildInterface($name) - { - $stub = $this->files->get($this->getInterfaceStub()); + $stub = str_replace($search, $replace, $stub); - return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); + return $stub; } /** - * rootNamespace - * Get the root namespace for the class. + * Get the console command arguments. + * + * @return array */ - protected function rootNamespace(): string + protected function getArguments() { - return $this->getClassNamespace('service'); + return [ + ['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)], + ]; } } diff --git a/src/Commands/MakeTestCommand.php b/src/Commands/MakeTestCommand.php index 573ebe6..d3e0a73 100644 --- a/src/Commands/MakeTestCommand.php +++ b/src/Commands/MakeTestCommand.php @@ -2,14 +2,14 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\TestMakeCommand; use Illuminate\Support\Str; class MakeTestCommand extends TestMakeCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name @@ -20,15 +20,14 @@ class MakeTestCommand extends TestMakeCommand protected $name = 'make:test'; /** - * getPath * Get the destination class path. * * @param string $name */ protected function getPath($name): string { - $name = Str::replaceFirst($this->rootNamespace(), '', $name); + $name = Str::of($name)->replaceFirst($this->rootNamespace(), '')->replace('\\', '/'); - return self::assembleFullPath('test').Str::replace('\\', '/', $name).'.php'; + return self::assembleFullPath('test')."/$name.php"; } } diff --git a/src/Commands/MakeValueCommand.php b/src/Commands/MakeValueCommand.php index e68eab1..c05385c 100644 --- a/src/Commands/MakeValueCommand.php +++ b/src/Commands/MakeValueCommand.php @@ -2,7 +2,7 @@ namespace HandsomeBrown\Laraca\Commands; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; @@ -11,7 +11,7 @@ #[AsCommand(name: 'make:value')] class MakeValueCommand extends GeneratorCommand { - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * The console command name. diff --git a/src/Commands/MakeViewCommand.php b/src/Commands/MakeViewCommand.php index 6627f14..fc8220a 100644 --- a/src/Commands/MakeViewCommand.php +++ b/src/Commands/MakeViewCommand.php @@ -3,14 +3,14 @@ namespace HandsomeBrown\Laraca\Commands; use HandsomeBrown\Laraca\Commands\Traits\CreatesView; -use HandsomeBrown\Laraca\Commands\Traits\Domainable; +use HandsomeBrown\Laraca\Commands\Traits\Directable; use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand; use Illuminate\Foundation\Console\ViewMakeCommand; class MakeViewCommand extends ViewMakeCommand { use CreatesView; - use Domainable, LaracaCommand; + use Directable, LaracaCommand; /** * name diff --git a/src/Commands/Traits/CreatesView.php b/src/Commands/Traits/CreatesView.php index fa7b421..3e82d2e 100644 --- a/src/Commands/Traits/CreatesView.php +++ b/src/Commands/Traits/CreatesView.php @@ -5,8 +5,8 @@ trait CreatesView { /** - * viewPath * Get the first view directory path from the application configuration. + * Laravel func * * @param string $path * @return string diff --git a/src/Commands/Traits/Directable.php b/src/Commands/Traits/Directable.php new file mode 100644 index 0000000..8660351 --- /dev/null +++ b/src/Commands/Traits/Directable.php @@ -0,0 +1,32 @@ +laravel->basePath(trim($stub, '/'))) - ? $customPath - : __DIR__.'/../../Foundation/Console'.$stub; - } -} diff --git a/src/Commands/Traits/LaracaCommand.php b/src/Commands/Traits/LaracaCommand.php index 768d735..3b6b555 100644 --- a/src/Commands/Traits/LaracaCommand.php +++ b/src/Commands/Traits/LaracaCommand.php @@ -8,20 +8,68 @@ trait LaracaCommand { use GetsConfigValues; + public function hasParents() + { + return (bool) class_parents($this); + } + /** - * Get the class namespace + * Get the class name + */ + protected function getClassName($name): string + { + $name = ltrim($name, '\\/'); + + $name = str_replace('/', '\\', $name); + + return ucfirst($name); + } + + /** + * Get the namespace with the possibility of domain or service flags * * @param string $key */ protected function getClassNamespace($key): string { - if ($this->input->hasArgument('domain')) { - $domain = $this->input->getArgument('domain'); - if (self::domainsEnabled() && $domain) { - return self::assembleNamespace($key, $domain); + if ($this->input->hasOption('domain')) { + $domainName = $this->getClassName($this->input->getOption('domain')); + if (self::domainsEnabled() && $domainName) { + return self::assembleNamespace($key, $domainName); + } + } + + if ($this->input->hasOption('service')) { + $serviceName = $this->getClassName($this->input->getOption('service')); + if (self::microservicesEnabled() && $serviceName) { + return self::assembleNamespace($key, null, $serviceName); } } return self::assembleNamespace($key); } + + /** + * Get the path with the possibility of domain or service flags + * + * @param string $key + */ + protected function getGenerationPath($key): string + { + if ($this->input->hasOption('domain')) { + $domainName = $this->getClassName($this->input->getOption('domain')); + if (self::domainsEnabled() && $domainName) { + return self::assembleFullPath($key, $domainName); + } + } + + if ($this->input->hasOption('service')) { + $serviceName = $this->getClassName($this->input->getOption('service')); + if (self::microservicesEnabled() && $serviceName) { + return self::assembleFullPath($key, null, $serviceName); + } + } + + return self::assembleFullPath($key); + } } diff --git a/src/Commands/stubs/routes-api.stub b/src/Commands/stubs/routes-api.stub new file mode 100644 index 0000000..52a6452 --- /dev/null +++ b/src/Commands/stubs/routes-api.stub @@ -0,0 +1,21 @@ +json(['path' => '/api/{{ slug }}']); +}); + +Route::middleware('auth:api')->get('/user', function (Request $request) { + return $request->user(); +}); \ No newline at end of file diff --git a/src/Commands/stubs/routes-channels.stub b/src/Commands/stubs/routes-channels.stub new file mode 100644 index 0000000..3019cfe --- /dev/null +++ b/src/Commands/stubs/routes-channels.stub @@ -0,0 +1,13 @@ +id === (int) $id; +}); \ No newline at end of file diff --git a/src/Commands/stubs/routes-web.stub b/src/Commands/stubs/routes-web.stub new file mode 100644 index 0000000..4e0d416 --- /dev/null +++ b/src/Commands/stubs/routes-web.stub @@ -0,0 +1,19 @@ +app['router']->group([ + 'middleware' => 'web', + 'prefix' => '{{ slug }}', + 'namespace' => '{{ controller_namespace }}', + ], function () { + require __DIR__.'/../routes/web.php'; + }); + + $this->app['router']->group([ + 'middleware' => 'api', + 'prefix' => 'api/{{ slug }}', + 'namespace' => '{{ controller_namespace }}', + ], function () { + require __DIR__.'/../routes/api.php'; + }); + } +} \ No newline at end of file diff --git a/src/Commands/stubs/serviceprovider.stub b/src/Commands/stubs/serviceprovider.stub new file mode 100644 index 0000000..28fbe35 --- /dev/null +++ b/src/Commands/stubs/serviceprovider.stub @@ -0,0 +1,30 @@ +app->register(RouteServiceProvider::class); + $this->app->register(BroadcastServiceProvider::class); + + View::addNamespace('{{ service }}', realpath(__DIR__.'/../resources/views')); + } + + /** + * Bootstrap services. + */ + public function boot(): void + { + // + } +} \ No newline at end of file diff --git a/src/Commands/stubs/welcome.stub b/src/Commands/stubs/welcome.stub new file mode 100644 index 0000000..6e9bfba --- /dev/null +++ b/src/Commands/stubs/welcome.stub @@ -0,0 +1,17 @@ + + + + + + Welcome to {{ service }} + + +
+ +

{{ service }}

+
+ + + \ No newline at end of file diff --git a/src/Exceptions/InvalidConfigKeyException.php b/src/Exceptions/InvalidConfigKeyException.php index b5a6ef4..aad5f7d 100644 --- a/src/Exceptions/InvalidConfigKeyException.php +++ b/src/Exceptions/InvalidConfigKeyException.php @@ -6,5 +6,8 @@ class InvalidConfigKeyException extends Exception { - // + public function __construct($key) + { + $this->message = 'Invalid configuration key: '.$key; + } } diff --git a/src/Exceptions/StubNotFoundException.php b/src/Exceptions/StubNotFoundException.php new file mode 100644 index 0000000..dce2e89 --- /dev/null +++ b/src/Exceptions/StubNotFoundException.php @@ -0,0 +1,10 @@ + ArtiMigrationCommand::class, + 'InitMicroservice' => InitMicroserviceCommand::class, + 'InitStructure' => InitStructureCommand::class, 'MakeCast' => MakeCastCommand::class, 'MakeChannel' => MakeChannelCommand::class, 'MakeCommand' => MakeCommandCommand::class, @@ -80,7 +82,6 @@ class LaracaServiceProvider extends ServiceProvider 'MakeScope' => MakeScopeCommand::class, 'MakeService' => MakeServiceCommand::class, 'MakeSeeder' => MakeSeederCommand::class, - 'MakeStructure' => InitStructureCommand::class, 'MakeTest' => MakeTestCommand::class, 'MakeValue' => MakeValueCommand::class, 'MakeView' => MakeViewCommand::class, @@ -99,10 +100,7 @@ public function boot(): void $this->app->useDatabasePath($this->assembleFullPath('database')); - $domainsEnabled = Config::get('laraca.domains.enabled'); - $domainsParentDir = Config::get('laraca.domains.parent_dir'); - - if ($domainsEnabled && $domainsParentDir) { + if ($this->domainsEnabled() && $this->domainParentDir()) { $this->commands['DomainList'] = DomainListCommand::class; } diff --git a/src/Traits/GetsConfigValues.php b/src/Traits/GetsConfigValues.php index 62058fb..a7c01b2 100644 --- a/src/Traits/GetsConfigValues.php +++ b/src/Traits/GetsConfigValues.php @@ -14,7 +14,7 @@ trait GetsConfigValues */ public static function domainsEnabled(): bool { - $domainsEnabled = Config::get('laraca.domains.enabled'); + $domainsEnabled = Config::get('laraca.struct.domain.enabled'); return $domainsEnabled; } @@ -22,26 +22,43 @@ public static function domainsEnabled(): bool /** * Domain parent dir */ - public static function domainParentDir(): string + public static function domainParentDir(): ?string { - $parentDir = Config::get('laraca.domains.parent_dir'); + $parentDir = Config::get('laraca.struct.domain.path'); + + return $parentDir; + } + + /** + * Using domains + */ + public static function microservicesEnabled(): bool + { + $microservicesEnabled = Config::get('laraca.struct.microservice.enabled'); + + return $microservicesEnabled; + } + + /** + * Domain parent dir + */ + public static function microserviceParentDir(): ?string + { + $parentDir = Config::get('laraca.struct.microservice.path'); return $parentDir; } /** * assembleRelativePath - * - * @param string $key - * @param bool $full */ - public static function assembleRelativePath($key, $domain = null): string + public static function assembleRelativePath(string $key, ?string $domain = null, ?string $service = null, bool $withRoot = true): string { - [$pathArray, $root] = self::assemblePathArray($key, $domain); + [$pathArray, $root] = self::assemblePathArray($key, $domain, $service); $path = implode('/', $pathArray); - if ($root == 'app') { + if ($root == 'app' && $withRoot) { $path = 'app/'.$path; } @@ -50,20 +67,19 @@ public static function assembleRelativePath($key, $domain = null): string /** * assembleFullPath - * - * @param string $key - * @param string $domain */ - public static function assembleFullPath($key, $domain = null): string + public static function assembleFullPath(string $key, ?string $domain = null, ?string $service = null, bool $withRoot = true): string { - [$pathArray, $root] = self::assemblePathArray($key, $domain); + [$pathArray, $root] = self::assemblePathArray($key, $domain, $service); $path = implode('/', $pathArray); - if ($root == 'app') { - $path = app_path($path); - } elseif ($root == 'base') { - $path = base_path($path); + if ($withRoot) { + if ($root == 'app') { + $path = app_path($path); + } elseif ($root == 'base') { + $path = base_path($path); + } } return $path; @@ -71,13 +87,10 @@ public static function assembleFullPath($key, $domain = null): string /** * assembleNamespace - * - * @param string $key - * @param string $domain */ - public static function assembleNamespace($key, $domain = null): string + public static function assembleNamespace(string $key, ?string $domain = null, ?string $service = null, bool $withRoot = true): string { - [$pathArray, $root] = self::assemblePathArray($key, $domain); + [$pathArray, $root] = self::assemblePathArray($key, $domain, $service); if ($key === 'test') { array_shift($pathArray); @@ -89,7 +102,7 @@ public static function assembleNamespace($key, $domain = null): string $namespace = implode('\\', $pathArray); - if ($root == 'app') { + if ($root == 'app' && $withRoot) { $namespace = app()->getNamespace().$namespace; } @@ -98,17 +111,15 @@ public static function assembleNamespace($key, $domain = null): string /** * assemblePathArray - * - * @param string,string $key */ - protected static function assemblePathArray($key, $domain = null): array + protected static function assemblePathArray(string $key, ?string $domain = null, ?string $service = null): array { - if (! Config::has('laraca.structure.'.$key)) { - throw new InvalidConfigKeyException(); + if (! Config::has('laraca.struct.'.$key)) { + throw new InvalidConfigKeyException($key); } $path = []; - $current = Config::get('laraca.structure.'.$key); + $current = Config::get('laraca.struct.'.$key); $done = false; do { @@ -116,7 +127,7 @@ protected static function assemblePathArray($key, $domain = null): array $path = array_merge(explode('/', $current['path']), $path); } else { // key config missing path or namespace value - throw new MissingPathNamespaceKeyException(); + throw new MissingPathNamespaceKeyException($key); } if (array_key_exists('parent', $current) && (bool) $current['parent']) { @@ -132,17 +143,25 @@ protected static function assemblePathArray($key, $domain = null): array } } + if (self::microservicesEnabled() && $service) { + array_unshift($path, ucfirst($service)); + + if (self::microserviceParentDir()) { + array_unshift($path, self::microserviceParentDir()); + } + } + $base = $parentKey; $done = true; - } elseif (Config::has('laraca.structure.'.$parentKey)) { - $current = Config::get('laraca.structure.'.$parentKey); + } elseif (Config::has('laraca.struct.'.$parentKey)) { + $current = Config::get('laraca.struct.'.$parentKey); } else { // parent key not found in config - throw new InvalidConfigKeyException(); + throw new InvalidConfigKeyException($parentKey); } } else { // path has led up to parent never finding 'base' or 'app' as a root - throw new MissingRootPathException(); + throw new MissingRootPathException($key); } } while ($done !== true); diff --git a/tests/Feature/ArtiMigrationCommandTest.php b/tests/Feature/ArtiMigrationCommandTest.php index e3a407e..58ad11f 100644 --- a/tests/Feature/ArtiMigrationCommandTest.php +++ b/tests/Feature/ArtiMigrationCommandTest.php @@ -7,7 +7,7 @@ describe('arti:migration', function () { it('should create the Migration class when used', function (string $class) { - Config::set('laraca.structure.database.path', 'test/database'); + Config::set('laraca.struct.database.path', 'test/database'); $this->artisan('arti:migration', ['name' => $class]); @@ -30,7 +30,7 @@ })->with('classes'); it('should create the Migration class using path option', function (string $class) { - Config::set('laraca.structure.database.path', 'test/database'); + Config::set('laraca.struct.database.path', 'test/database'); $this->artisan('arti:migration', ['name' => $class, '--path' => 'test/db/migrations']); diff --git a/tests/Feature/DomainListTest.php b/tests/Feature/DomainListTest.php index bb02e62..3dd673e 100644 --- a/tests/Feature/DomainListTest.php +++ b/tests/Feature/DomainListTest.php @@ -5,14 +5,14 @@ describe('domain:list', function () { it('should list direct children of the configured domain folder', function (string $class, string $domain) { - Config::set('laraca.domains.enabled', true); - Config::set('laraca.domains.parent_dir', 'TestDomains'); + Config::set('laraca.struct.domain.enabled', true); + Config::set('laraca.struct.domain.path', 'TestDomains'); $this->artisan('make:controller', ['name' => $class, - 'domain' => $domain]); + '--domain' => $domain]); $this->artisan('make:job', ['name' => $class, - 'domain' => $domain]); + '--domain' => $domain]); $this->artisan('domain:list'); @@ -23,16 +23,16 @@ })->with('classes', 'domains'); it('should not be available when domains are disabled', function () { - Config::set('laraca.domains.enabled', false); - Config::set('laraca.domains.parent_dir', 'TestDomains'); + Config::set('laraca.struct.domain.enabled', false); + Config::set('laraca.struct.domain.path', 'TestDomains'); $output = Artisan::output(); expect($output) ->not->toContain('domain:list'); }); it('should not be available when domain parent dir is null', function () { - Config::set('laraca.domains.enabled', true); - Config::set('laraca.domains.parent_dir', null); + Config::set('laraca.struct.domain.enabled', true); + Config::set('laraca.struct.domain.path', null); $output = Artisan::output(); expect($output) ->not->toContain('domain:list'); diff --git a/tests/Feature/DomainsTest.php b/tests/Feature/DomainsTest.php index 90e221b..c8c894b 100644 --- a/tests/Feature/DomainsTest.php +++ b/tests/Feature/DomainsTest.php @@ -6,21 +6,44 @@ describe('use domains', function () { it('should use domain settings in path/namespace when enabled and domain arg', function (string $class, string $domain) { - Config::set('laraca.domains.enabled', true); - Config::set('laraca.domains.parent_dir', 'TestDomains'); + Config::set('laraca.struct.domain.enabled', true); + Config::set('laraca.struct.domain.path', 'TestDomains'); $this->artisan('make:controller', ['name' => $class, - 'domain' => $domain]); + '--domain' => $domain]); $output = Artisan::output(); $configPath = assembleFullPath('controller', $domain); $filePath = "$configPath/$class.php"; + $configNamespace = fullNamespaceStr("App\TestDomains\\".ucfirst($domain)."\Http\Controllers"); + expect(File::exists($filePath)) ->toBe(true, "File not created at expected path:\n".$filePath."\nCommand result:\n".$output."\n\n"); - $configNamespace = fullNamespaceStr("App\TestDomains\\".ucfirst($domain)."\Http\Controllers"); + expect(File::get($filePath)) + ->toContain($configNamespace); + + })->with('classes', 'domains'); + + it('should not use parent domain when path is null', function (string $class, string $domain) { + Config::set('laraca.struct.domain.enabled', true); + Config::set('laraca.struct.domain.path', null); + Config::set('laraca.struct.enum.path', 'Test/Enums'); + $this->artisan('make:enum', + ['name' => $class, + '--domain' => $domain]); + + $configPath = assembleFullPath('enum', $domain); + $filePath = "$configPath/$class.php"; + + $configNamespace = fullNamespaceStr('App\\'.ucfirst($domain)."\Test\Enums"); + + $output = Artisan::output(); + + expect(File::exists($filePath)) + ->toBe(true, "File not created at expected path:\n".$filePath."\nCommand result:\n".$output."\n\n"); expect(File::get($filePath)) ->toContain($configNamespace); @@ -28,22 +51,22 @@ })->with('classes', 'domains'); it('should not use domain settings in path/namespace when enabled and no domain arg', function (string $class) { - Config::set('laraca.domains.enabled', true); - Config::set('laraca.domains.parent_dir', 'TestDomains'); - Config::set('laraca.structure.controller.path', 'Test/Http/Controllers'); + Config::set('laraca.struct.domain.enabled', true); + Config::set('laraca.struct.domain.path', 'TestDomains'); + Config::set('laraca.struct.controller.path', 'Test/Http/Controllers'); $this->artisan('make:controller', ['name' => $class]); $configPath = assembleFullPath('controller'); $filePath = "$configPath/$class.php"; + $configNamespace = fullNamespaceStr('App\Test\Http\Controllers'); + $output = Artisan::output(); expect(File::exists($filePath)) ->toBe(true, "File not created at expected path:\n".$filePath."\nCommand result:\n".$output."\n\n"); - $configNamespace = fullNamespaceStr('App\Test\Http\Controllers'); - expect(File::get($filePath)) ->toContain($configNamespace); diff --git a/tests/Feature/InitMicroserviceCommandTest.php b/tests/Feature/InitMicroserviceCommandTest.php new file mode 100644 index 0000000..7dcd7b0 --- /dev/null +++ b/tests/Feature/InitMicroserviceCommandTest.php @@ -0,0 +1,83 @@ +artisan('init:micro', [ + 'name' => $class, + ]))->toBe(0); + + $output = Artisan::output(); + + $class = ucfirst($class); + $slug = Str::slug($class); + + $paths = [ + "app/Test/Microservices/$class/Broadcasting", + "app/Test/Microservices/$class/Http/Controllers", + "app/Test/Microservices/$class/Providers", + "app/Test/Microservices/$class/resources/views", + "app/Test/Microservices/$class/routes", + "app/Test/Microservices/$class/tests", + ]; + + foreach ($paths as $p) { + $dirPath = base_path($p); + expect(File::isDirectory($dirPath)) + ->toBe(true, "Directory not created:\n".$dirPath."\n"); + } + + $root = "Test/Microservices/$class"; + $servicePath = app_path($root.'/'.$class.'ServiceProvider.php'); + $routeServicePath = app_path("$root/Providers/RouteServiceProvider.php"); + $broadcastServicePath = app_path("$root/Providers/BroadcastServiceProvider.php"); + $welcomePath = app_path("$root/resources/views/welcome.blade.php"); + $routesPath = app_path("$root/routes/"); + + $serviceNamespace = fullNamespaceStr("App\\Test\\Microservices\\$class"); + $providerNamespace = fullNamespaceStr("App\\Test\\Microservices\\$class\\Providers"); + + expect(File::exists($servicePath)) + ->toBe(true, "File not created at expected path:\n".$servicePath."\nCommand result:\n".$output."\n\n"); + expect(File::get($servicePath)) + ->toContain($serviceNamespace); + + expect(File::exists($routeServicePath)) + ->toBe(true, "File not created at expected path:\n".$routeServicePath."\nCommand result:\n".$output."\n\n"); + expect(File::get($routeServicePath)) + ->toContain($providerNamespace) + ->toContain('require __DIR__.\'/../routes/web.php\';'); + + expect(File::exists($broadcastServicePath)) + ->toBe(true, "File not created at expected path:\n".$broadcastServicePath."\nCommand result:\n".$output."\n\n"); + expect(File::get($broadcastServicePath)) + ->toContain($providerNamespace) + ->toContain('require __DIR__.\'/../routes/channels.php\';'); + + expect(File::exists($routesPath.'web.php')) + ->toBe(true, "File not created at expected path:\n".$routesPath.'web.php'."\nCommand result:\n".$output."\n\n"); + expect(File::get($routesPath.'web.php')) + ->toContain($slug) + ->toContain("prefix: /$slug"); + + expect(File::exists($routesPath.'api.php')) + ->toBe(true, "File not created at expected path:\n".$routesPath.'api.php'."\nCommand result:\n".$output."\n\n"); + expect(File::get($routesPath.'api.php')) + ->toContain($slug) + ->toContain("/api/$slug"); + + expect(File::exists($routesPath.'channels.php')) + ->toBe(true, "File not created at expected path:\n".$routesPath.'api.php'."\nCommand result:\n".$output."\n\n"); + expect(File::get($routesPath.'channels.php')) + ->toContain($class) + ->toContain('Broadcast::channel(\''.$class.'.User.{id}\', function ($user, $id) {'); + + expect(File::exists($welcomePath)) + ->toBe(true, "File not created at expected path:\n".$welcomePath."\nCommand result:\n".$output."\n\n"); + })->with('classes'); +}); diff --git a/tests/Feature/InitStructureCommandTest.php b/tests/Feature/InitStructureCommandTest.php index 89950e1..9aea4d3 100644 --- a/tests/Feature/InitStructureCommandTest.php +++ b/tests/Feature/InitStructureCommandTest.php @@ -8,30 +8,30 @@ describe('init:structure', function () { it('should create directory Structure from config', function () { - Config::set('laraca.structure.database.path', 'test/database'); - Config::set('laraca.structure.cast.path', 'Test/Data/Casts'); - Config::set('laraca.structure.channel.path', 'Test/Broadcasting'); - Config::set('laraca.structure.command.path', 'Test/Console/Commands'); - Config::set('laraca.structure.component.path', 'Test/View/Components'); - Config::set('laraca.structure.controller.path', 'Test/Http/Controllers'); - Config::set('laraca.structure.event.path', 'Test/Events'); - Config::set('laraca.structure.enum.path', 'Test/Enums'); - Config::set('laraca.structure.exception.path', 'Test/Exceptions'); - Config::set('laraca.structure.job.path', 'Test/Jobs'); - Config::set('laraca.structure.listener.path', 'Test/Listeners'); - Config::set('laraca.structure.mail.path', 'Test/Mail'); - Config::set('laraca.structure.middleware.path', 'Test/Http/Middleware'); - Config::set('laraca.structure.model.path', 'Test/Data/Models'); - Config::set('laraca.structure.notification.path', 'Test/Notifications'); - Config::set('laraca.structure.observer.path', 'Test/Data/Observers'); - Config::set('laraca.structure.policy.path', 'Test/Policies'); - Config::set('laraca.structure.provider.path', 'Test/Providers'); - Config::set('laraca.structure.request.path', 'Test/Http/Requests'); - Config::set('laraca.structure.resource.path', 'Test/Http/Resources'); - Config::set('laraca.structure.rule.path', 'Test/Rules'); - Config::set('laraca.structure.test.path', 'test/tests'); - Config::set('laraca.structure.value.path', 'Test/Data/Values'); - Config::set('laraca.structure.view.path', 'test/resources/views'); + Config::set('laraca.struct.database.path', 'test/database'); + Config::set('laraca.struct.cast.path', 'Test/Data/Casts'); + Config::set('laraca.struct.channel.path', 'Test/Broadcasting'); + Config::set('laraca.struct.command.path', 'Test/Console/Commands'); + Config::set('laraca.struct.component.path', 'Test/View/Components'); + Config::set('laraca.struct.controller.path', 'Test/Http/Controllers'); + Config::set('laraca.struct.event.path', 'Test/Events'); + Config::set('laraca.struct.enum.path', 'Test/Enums'); + Config::set('laraca.struct.exception.path', 'Test/Exceptions'); + Config::set('laraca.struct.job.path', 'Test/Jobs'); + Config::set('laraca.struct.listener.path', 'Test/Listeners'); + Config::set('laraca.struct.mail.path', 'Test/Mail'); + Config::set('laraca.struct.middleware.path', 'Test/Http/Middleware'); + Config::set('laraca.struct.model.path', 'Test/Data/Models'); + Config::set('laraca.struct.notification.path', 'Test/Notifications'); + Config::set('laraca.struct.observer.path', 'Test/Data/Observers'); + Config::set('laraca.struct.policy.path', 'Test/Policies'); + Config::set('laraca.struct.provider.path', 'Test/Providers'); + Config::set('laraca.struct.request.path', 'Test/Http/Requests'); + Config::set('laraca.struct.resource.path', 'Test/Http/Resources'); + Config::set('laraca.struct.rule.path', 'Test/Rules'); + Config::set('laraca.struct.test.path', 'test/tests'); + Config::set('laraca.struct.value.path', 'Test/Data/Values'); + Config::set('laraca.struct.view.path', 'test/resources/views'); expect($this->artisan('init:structure')) ->toBe(0); @@ -76,27 +76,27 @@ ->toBe(true, "File not created at expected path:\n".$keepFile."\n\n"); } - })->with('classes'); + }); it('throws a MissingPathNamespaceKeyException when a key has no path or namespace', function () { - Config::set('laraca.structure.empty_key', []); - Config::set('laraca.structure.command.parent', 'empty_key'); + Config::set('laraca.struct.empty_key', []); + Config::set('laraca.struct.command.parent', 'empty_key'); $this->artisan('init:structure'); - })->with('classes')->throws(MissingPathNamespaceKeyException::class); + })->throws(MissingPathNamespaceKeyException::class); it('throws an InvalidConfigKeyException when a parent key does not exist in the config', function () { - Config::set('laraca.structure.model.parent', 'nonexistent_key'); + Config::set('laraca.struct.model.parent', 'nonexistent_key'); $this->artisan('init:structure'); - })->with('classes')->throws(InvalidConfigKeyException::class); + })->throws(InvalidConfigKeyException::class); it('throws a MissingRootPathException when a tree does not lead to a base or app parent', function () { - Config::set('laraca.structure.model.parent', ''); + Config::set('laraca.struct.model.parent', ''); $this->artisan('init:structure'); - })->with('classes')->throws(MissingRootPathException::class); + })->throws(MissingRootPathException::class); }); diff --git a/tests/Feature/MakeCastCommandTest.php b/tests/Feature/MakeCastCommandTest.php index 630a1f9..09a8722 100644 --- a/tests/Feature/MakeCastCommandTest.php +++ b/tests/Feature/MakeCastCommandTest.php @@ -6,7 +6,7 @@ describe('make:cast', function () { it('should create Cast class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.cast.path', 'Test/Data/Casts'); + Config::set('laraca.struct.cast.path', 'Test/Data/Casts'); $this->artisan('make:cast', ['name' => $class]); diff --git a/tests/Feature/MakeChannelCommandTest.php b/tests/Feature/MakeChannelCommandTest.php index 468ad12..3946c06 100644 --- a/tests/Feature/MakeChannelCommandTest.php +++ b/tests/Feature/MakeChannelCommandTest.php @@ -6,7 +6,7 @@ describe('make:channel', function () { it('should create Channel class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.channel.path', 'Test/Broadcasting'); + Config::set('laraca.struct.channel.path', 'Test/Broadcasting'); $this->artisan('make:channel', ['name' => $class]); diff --git a/tests/Feature/MakeCommandCommandTest.php b/tests/Feature/MakeCommandCommandTest.php index fc68637..abf1279 100644 --- a/tests/Feature/MakeCommandCommandTest.php +++ b/tests/Feature/MakeCommandCommandTest.php @@ -6,7 +6,7 @@ describe('make:command', function () { it('should create Command class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.command.path', 'Test/Console/Commands'); + Config::set('laraca.struct.command.path', 'Test/Console/Commands'); $this->artisan('make:command', ['name' => $class]); diff --git a/tests/Feature/MakeComponentCommandTest.php b/tests/Feature/MakeComponentCommandTest.php deleted file mode 100644 index b9abfec..0000000 --- a/tests/Feature/MakeComponentCommandTest.php +++ /dev/null @@ -1,51 +0,0 @@ -artisan('make:component', - ['name' => $class]); - - $configPath = assembleFullPath('component'); - $filePath = "$configPath/$class.php"; - - $output = Artisan::output(); - - expect(File::exists($filePath)) - ->toBe(true, "File not created at expected path:\n".$filePath."\nCommand result:\n".$output."\n\n"); - - $configNamespace = fullNamespaceStr('App\Test\View\Components'); - - expect(File::get($filePath)) - ->toContain($configNamespace); - - $snake_class = Str::snake($class, '-'); - $viewPath = assembleFullPath('view')."/components/{$snake_class}.blade.php"; - - expect(File::exists($viewPath)) - ->toBe(true, "File not created at expected path:\n".$viewPath."\nCommand result:\n".$output."\n\n"); - - })->with('classes'); - - it('should create blade file at Laravel configured path when view not set in laraca config', function (string $class) { - Config::set('laraca.structure.component.path', 'Test/View/Components'); - Config::offsetUnset('laraca.structure.view'); - $this->artisan('make:component', - ['name' => $class]); - - $output = Artisan::output(); - - $snake_class = Str::snake($class, '-'); - $viewPath = base_path("resources/views/components/{$snake_class}.blade.php"); - - expect(File::exists($viewPath)) - ->toBe(true, "File not created at expected path:\n".$viewPath."\nCommand result:\n".$output."\n\n"); - - })->with('classes'); -}); diff --git a/tests/Feature/MakeControllerCommandTest.php b/tests/Feature/MakeControllerCommandTest.php index ec648c4..03941f6 100644 --- a/tests/Feature/MakeControllerCommandTest.php +++ b/tests/Feature/MakeControllerCommandTest.php @@ -6,7 +6,7 @@ describe('make:controller', function () { it('should create Controller class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.controller.path', 'Test/Http/Controllers'); + Config::set('laraca.struct.controller.path', 'Test/Http/Controllers'); $this->artisan('make:controller', ['name' => $class]); diff --git a/tests/Feature/MakeEnumCommandTest.php b/tests/Feature/MakeEnumCommandTest.php index 6973f7e..53ba442 100644 --- a/tests/Feature/MakeEnumCommandTest.php +++ b/tests/Feature/MakeEnumCommandTest.php @@ -6,7 +6,7 @@ describe('make:enum', function () { it('should create Enum class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.enum.path', 'Test/Enums'); + Config::set('laraca.struct.enum.path', 'Test/Enums'); $this->artisan('make:enum', ['name' => $class]); diff --git a/tests/Feature/MakeEventCommandTest.php b/tests/Feature/MakeEventCommandTest.php index fc6663c..db85562 100644 --- a/tests/Feature/MakeEventCommandTest.php +++ b/tests/Feature/MakeEventCommandTest.php @@ -6,7 +6,7 @@ describe('make:event', function () { it('should create Event class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.event.path', 'Test/Events'); + Config::set('laraca.struct.event.path', 'Test/Events'); $this->artisan('make:event', ['name' => $class]); diff --git a/tests/Feature/MakeExceptionCommandTest.php b/tests/Feature/MakeExceptionCommandTest.php index e4d9493..b596dd2 100644 --- a/tests/Feature/MakeExceptionCommandTest.php +++ b/tests/Feature/MakeExceptionCommandTest.php @@ -6,7 +6,7 @@ describe('make:exception', function () { it('should create Exception class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.exception.path', 'Test/Exceptions'); + Config::set('laraca.struct.exception.path', 'Test/Exceptions'); $this->artisan('make:exception', ['name' => $class]); diff --git a/tests/Feature/MakeFactoryCommandTest.php b/tests/Feature/MakeFactoryCommandTest.php index fc98ba5..45631b1 100644 --- a/tests/Feature/MakeFactoryCommandTest.php +++ b/tests/Feature/MakeFactoryCommandTest.php @@ -6,7 +6,7 @@ describe('make:factory', function () { it('should create Factory class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.database.path', 'test/database'); + Config::set('laraca.struct.database.path', 'test/database'); $this->artisan('make:factory', ['name' => $class]); diff --git a/tests/Feature/MakeJobCommandTest.php b/tests/Feature/MakeJobCommandTest.php index 5e6035d..940e5bc 100644 --- a/tests/Feature/MakeJobCommandTest.php +++ b/tests/Feature/MakeJobCommandTest.php @@ -6,7 +6,7 @@ describe('make:job', function () { it('should create Job class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.job.path', 'Test/Jobs'); + Config::set('laraca.struct.job.path', 'Test/Jobs'); $this->artisan('make:job', ['name' => $class]); diff --git a/tests/Feature/MakeListenerCommandTest.php b/tests/Feature/MakeListenerCommandTest.php index 0d0ecb5..dfb23bb 100644 --- a/tests/Feature/MakeListenerCommandTest.php +++ b/tests/Feature/MakeListenerCommandTest.php @@ -6,7 +6,7 @@ describe('make:listener', function () { it('should create Listener class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.listener.path', 'Test/Listeners'); + Config::set('laraca.struct.listener.path', 'Test/Listeners'); $this->artisan('make:listener', ['name' => $class]); diff --git a/tests/Feature/MakeMailCommandTest.php b/tests/Feature/MakeMailCommandTest.php index 3ab392f..9764dd1 100644 --- a/tests/Feature/MakeMailCommandTest.php +++ b/tests/Feature/MakeMailCommandTest.php @@ -6,7 +6,7 @@ describe('make:mail', function () { it('should create Mail class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.mail.path', 'Test/Mail'); + Config::set('laraca.struct.mail.path', 'Test/Mail'); $this->artisan('make:mail', ['name' => $class]); diff --git a/tests/Feature/MakeMiddlewareCommandTest.php b/tests/Feature/MakeMiddlewareCommandTest.php index 11f7a58..2360bba 100644 --- a/tests/Feature/MakeMiddlewareCommandTest.php +++ b/tests/Feature/MakeMiddlewareCommandTest.php @@ -6,7 +6,7 @@ describe('make:middleware', function () { it('should create Middleware class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.middleware.path', 'Test/Http/Middleware'); + Config::set('laraca.struct.middleware.path', 'Test/Http/Middleware'); $this->artisan('make:middleware', ['name' => $class]); diff --git a/tests/Feature/MakeModelCommandTest.php b/tests/Feature/MakeModelCommandTest.php index a01773d..8d1f737 100644 --- a/tests/Feature/MakeModelCommandTest.php +++ b/tests/Feature/MakeModelCommandTest.php @@ -6,7 +6,7 @@ describe('make:model', function () { it('should create Model class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.model.path', 'Test/Data/Models'); + Config::set('laraca.struct.model.path', 'Test/Data/Models'); $this->artisan('make:model', ['name' => $class]); @@ -26,7 +26,7 @@ })->with('classes'); it('should create a Model class with HasUuids trait', function (string $class) { - Config::set('laraca.structure.model.path', 'Test/Data/Models'); + Config::set('laraca.struct.model.path', 'Test/Data/Models'); $this->artisan('make:model', ['name' => $class, '--uuid' => true]); diff --git a/tests/Feature/MakeNotificationCommandTest.php b/tests/Feature/MakeNotificationCommandTest.php index 2aa277f..312789f 100644 --- a/tests/Feature/MakeNotificationCommandTest.php +++ b/tests/Feature/MakeNotificationCommandTest.php @@ -6,7 +6,7 @@ describe('make:notification', function () { it('should create Notification class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.notification.path', 'Test/Notifications'); + Config::set('laraca.struct.notification.path', 'Test/Notifications'); $this->artisan('make:notification', ['name' => $class]); diff --git a/tests/Feature/MakeObserverCommandTest.php b/tests/Feature/MakeObserverCommandTest.php index 9784aaf..9d7ae1f 100644 --- a/tests/Feature/MakeObserverCommandTest.php +++ b/tests/Feature/MakeObserverCommandTest.php @@ -6,7 +6,7 @@ describe('make:observer', function () { it('should create Observer class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.observer.path', 'Test/Data/Observers'); + Config::set('laraca.struct.observer.path', 'Test/Data/Observers'); $this->artisan('make:observer', ['name' => $class]); diff --git a/tests/Feature/MakePolicyCommandTest.php b/tests/Feature/MakePolicyCommandTest.php index de8dcb8..65e40be 100644 --- a/tests/Feature/MakePolicyCommandTest.php +++ b/tests/Feature/MakePolicyCommandTest.php @@ -6,7 +6,7 @@ describe('make:policy', function () { it('should create Policy class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.policy.path', 'Test/Policies'); + Config::set('laraca.struct.policy.path', 'Test/Policies'); $this->artisan('make:policy', ['name' => $class]); diff --git a/tests/Feature/MakeProviderCommandTest.php b/tests/Feature/MakeProviderCommandTest.php index 72a4815..250f544 100644 --- a/tests/Feature/MakeProviderCommandTest.php +++ b/tests/Feature/MakeProviderCommandTest.php @@ -6,7 +6,7 @@ describe('make:provider', function () { it('should create Provider class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.provider.path', 'Test/Providers'); + Config::set('laraca.struct.provider.path', 'Test/Providers'); $this->artisan('make:provider', ['name' => $class]); diff --git a/tests/Feature/MakeRequestCommandTest.php b/tests/Feature/MakeRequestCommandTest.php index 006103b..dcc5ec1 100644 --- a/tests/Feature/MakeRequestCommandTest.php +++ b/tests/Feature/MakeRequestCommandTest.php @@ -6,7 +6,7 @@ describe('make:request', function () { it('should create Request class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.request.path', 'Test/Http/Requests'); + Config::set('laraca.struct.request.path', 'Test/Http/Requests'); $this->artisan('make:request', ['name' => $class]); diff --git a/tests/Feature/MakeResourceCommandTest.php b/tests/Feature/MakeResourceCommandTest.php index 1ae0105..67fa03f 100644 --- a/tests/Feature/MakeResourceCommandTest.php +++ b/tests/Feature/MakeResourceCommandTest.php @@ -6,7 +6,7 @@ describe('make:resource', function () { it('should create Resource class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.resource.path', 'Test/Http/Resources'); + Config::set('laraca.struct.resource.path', 'Test/Http/Resources'); $this->artisan('make:resource', ['name' => $class]); diff --git a/tests/Feature/MakeRuleCommandTest.php b/tests/Feature/MakeRuleCommandTest.php index daad56d..b698825 100644 --- a/tests/Feature/MakeRuleCommandTest.php +++ b/tests/Feature/MakeRuleCommandTest.php @@ -6,7 +6,7 @@ describe('make:rule', function () { it('should create Rule class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.rule.path', 'Test/Rules'); + Config::set('laraca.struct.rule.path', 'Test/Rules'); $this->artisan('make:rule', ['name' => $class]); diff --git a/tests/Feature/MakeScopeCommandTest.php b/tests/Feature/MakeScopeCommandTest.php index 2152d7d..446f82a 100644 --- a/tests/Feature/MakeScopeCommandTest.php +++ b/tests/Feature/MakeScopeCommandTest.php @@ -6,7 +6,7 @@ describe('make:scope', function () { it('should create Scope class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.model.path', 'Test/Data/Models'); + Config::set('laraca.struct.model.path', 'Test/Data/Models'); $this->artisan('make:scope', ['name' => $class]); diff --git a/tests/Feature/MakeSeederCommandTest.php b/tests/Feature/MakeSeederCommandTest.php index fd314a0..8fae14b 100644 --- a/tests/Feature/MakeSeederCommandTest.php +++ b/tests/Feature/MakeSeederCommandTest.php @@ -6,7 +6,7 @@ describe('make:seeder', function () { it('should create Seeder class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.database.path', 'test/database'); + Config::set('laraca.struct.database.path', 'test/database'); $this->artisan('make:seeder', ['name' => $class]); diff --git a/tests/Feature/MakeServiceCommandTest.php b/tests/Feature/MakeServiceCommandTest.php index d163d78..32d6359 100644 --- a/tests/Feature/MakeServiceCommandTest.php +++ b/tests/Feature/MakeServiceCommandTest.php @@ -6,9 +6,11 @@ describe('make:service', function () { it('should create Service class and interface with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.service.path', 'Test/Services'); - $this->artisan('make:service', - ['name' => $class]); + Config::set('laraca.struct.service.path', 'Test/Services'); + $this->artisan( + 'make:service', + ['name' => $class] + ); $class = ucfirst($class); $configPath = assembleFullPath('service'); @@ -30,6 +32,5 @@ expect(File::get($interfaceFilePath)) ->toContain($configNamespace); - })->with('classes'); }); diff --git a/tests/Feature/MakeTestCommandTest.php b/tests/Feature/MakeTestCommandTest.php index 08d1bc7..99ef1b0 100644 --- a/tests/Feature/MakeTestCommandTest.php +++ b/tests/Feature/MakeTestCommandTest.php @@ -6,7 +6,7 @@ describe('make:test', function () { it('should create Test class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.test.path', 'test/tests'); + Config::set('laraca.struct.test.path', 'test/tests'); $this->artisan('make:test', ['name' => $class]); diff --git a/tests/Feature/MakeValueCommandTest.php b/tests/Feature/MakeValueCommandTest.php index 4cf6a10..a2584da 100644 --- a/tests/Feature/MakeValueCommandTest.php +++ b/tests/Feature/MakeValueCommandTest.php @@ -7,7 +7,7 @@ describe('make:value', function () { it('should create Value class with namespace and path created from configured vals', function (string $class) { - Config::set('laraca.structure.value.path', 'Test/Data/Values'); + Config::set('laraca.struct.value.path', 'Test/Data/Values'); $this->artisan('make:value', ['name' => $class]); diff --git a/tests/Feature/MakeViewCommandTest.php b/tests/Feature/MakeViewCommandTest.php index 4505733..f073177 100644 --- a/tests/Feature/MakeViewCommandTest.php +++ b/tests/Feature/MakeViewCommandTest.php @@ -6,7 +6,7 @@ describe('make:view', function () { it('should create blade file at package configured path', function (string $class) { - Config::set('laraca.structure.view.path', 'test/resources/views'); + Config::set('laraca.struct.view.path', 'test/resources/views'); $this->artisan('make:view', ['name' => $class]); diff --git a/tests/Pest.php b/tests/Pest.php index f908621..88778df 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -18,9 +18,9 @@ * * @param string $key */ -function assembleFullPath($key, $domain = null): string +function assembleFullPath($key, $domain = null, $service = null): string { - return HandsomeBrown\Laraca\Traits\GetsConfigValues::assembleFullPath($key, $domain); + return HandsomeBrown\Laraca\Traits\GetsConfigValues::assembleFullPath($key, $domain, $service); } /** @@ -36,7 +36,7 @@ function assembleNamespace($key): string /** * fullNamespaceStr */ -function fullNamespaceStr(string $namespace, bool $app = true): string +function fullNamespaceStr(string $namespace): string { return 'namespace '.$namespace.';'; } diff --git a/tests/Unit/ConfigPathTest.php b/tests/Unit/ConfigPathTest.php index 6007e1e..5f73a3c 100644 --- a/tests/Unit/ConfigPathTest.php +++ b/tests/Unit/ConfigPathTest.php @@ -7,8 +7,8 @@ describe('Config path assembly exceptions', function () { it('throws a MissingPathNamespaceKeyException when a key has no path or namespace', function () { - Config::set('laraca.structure.empty_key', []); - Config::set('laraca.structure.command.parent', 'empty_key'); + Config::set('laraca.struct.empty_key', []); + Config::set('laraca.struct.command.parent', 'empty_key'); assembleFullPath('command'); @@ -21,14 +21,14 @@ })->with('classes')->throws(InvalidConfigKeyException::class); it('throws an InvalidConfigKeyException when a parent key does not exist in the config', function () { - Config::set('laraca.structure.model.parent', 'nonexistent_key'); + Config::set('laraca.struct.model.parent', 'nonexistent_key'); assembleFullPath('model'); })->with('classes')->throws(InvalidConfigKeyException::class); it('throws a MissingRootPathException when a tree does not lead to a base or app parent', function () { - Config::set('laraca.structure.model.parent', ''); + Config::set('laraca.struct.model.parent', ''); assembleFullPath('model');