From 35c3dd6a7e8ecd4d6f37f69d9db4d5a3587d1395 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 27 Mar 2024 11:38:13 +0100 Subject: [PATCH 1/2] Draft for make domain command --- config/ddd.php | 22 +++++++ src/Commands/MakeDomainCommand.php | 98 ++++++++++++++++++++++++++++++ src/LaravelDDDServiceProvider.php | 1 + 3 files changed, 121 insertions(+) create mode 100644 src/Commands/MakeDomainCommand.php diff --git a/config/ddd.php b/config/ddd.php index a1bd431..8a49c05 100644 --- a/config/ddd.php +++ b/config/ddd.php @@ -110,4 +110,26 @@ | */ 'base_action' => null, + + + 'make_domain_structure' => [ + 'Actions' => '', + 'Commands' => '', + 'Data' => '', + 'Enums' => '', + 'Exceptions' => '', + 'Database' => [ + 'Factories' => '', + 'Migrations' => '', + 'Seeders' => '', + ], + 'Jobs' => '', + 'Models' => '', + 'Policies' => '', + 'Providers' => [ + 'files' => [ + BaseServiceProvider::class, // How should we specify file you want created? Should use the logic the make commands + ] + ], + ] ]; diff --git a/src/Commands/MakeDomainCommand.php b/src/Commands/MakeDomainCommand.php new file mode 100644 index 0000000..7a7169e --- /dev/null +++ b/src/Commands/MakeDomainCommand.php @@ -0,0 +1,98 @@ + ['What is the name of the domain?', 'E.g. Payments'] + ]; + } + + public function handle(): int + { + $domain = $this->argument('domain'); + + if (empty($domain)) { + $this->error('Domain name is required'); + return self::FAILURE; + } + + $this->domain = ucfirst($domain); + + $this->domainStructure = config('ddd.make_domain_structure'); + $this->domainPath = DomainResolver::domainPath().'/'.$this->domain; + $this->domainRootNamespace = DomainResolver::domainRootNamespace().'\\'.$domain; + + $this->createDomainStructure($this->domainStructure); + + return self::SUCCESS; + } + + protected function createDomainStructure($domainStructure, $currentDir = ''): void + { + foreach ($domainStructure as $directory => $content) { + $this->createDirectoryWithContents($this->domainPath, $directory, $content); + } + + $this->info('Domain structure created'); + } + + protected function createDirectoryWithContents($path, $directory, $content): void + { + $path .= '/' . $directory; + if (!is_dir($path)) { + if (! mkdir($path, 0755, true) && ! is_dir($path)) { + throw new \RuntimeException(sprintf('Directory "%s" was not created', $path)); + } + } + + if (empty($content)) { + return; + } + + if (is_array($content)) { + if (isset($content['files']) && is_array($content['files'])) { + + $this->createFiles($path, $content['files']); + unset($content['files']); + } + + foreach ($content as $subDirectory => $subContent) { + $this->createDirectoryWithContents($path, $subDirectory, $subContent); + } + } + } + + protected function createFiles($path, $files): void + { + foreach ($files as $file) { + $this->createFile($path, $file, $this->domainRootNamespace); + } + } + + protected function createFile(string $path, string $file, string $namespace): void + { + // TODO: Make proper implementation + $file = $path.'/'.$file.'.php'; + if (!file_exists($file)) { + $content = file_get_contents(__DIR__.'/../BaseServiceProvider.php'); + file_put_contents($file, $content); + } + } +} diff --git a/src/LaravelDDDServiceProvider.php b/src/LaravelDDDServiceProvider.php index cd5cbc4..cca06de 100644 --- a/src/LaravelDDDServiceProvider.php +++ b/src/LaravelDDDServiceProvider.php @@ -19,6 +19,7 @@ public function configurePackage(Package $package): void ->hasConfigFile() ->hasCommands([ Commands\InstallCommand::class, + Commands\MakeDomainCommand::class, Commands\DomainListCommand::class, Commands\DomainModelMakeCommand::class, Commands\DomainFactoryMakeCommand::class, From 86fa8a7458d236403adef6c031a655a11a555835 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 27 Mar 2024 11:41:54 +0100 Subject: [PATCH 2/2] Add missing domain property --- src/Commands/MakeDomainCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Commands/MakeDomainCommand.php b/src/Commands/MakeDomainCommand.php index 7a7169e..9f90995 100644 --- a/src/Commands/MakeDomainCommand.php +++ b/src/Commands/MakeDomainCommand.php @@ -13,6 +13,7 @@ class MakeDomainCommand extends Command implements PromptsForMissingInput protected $description = 'Create a new domain'; + protected string $domain; protected mixed $domainStructure; protected ?string $domainPath; protected ?string $domainRootNamespace;