Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Make Domain command #51

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions config/ddd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
],
]
];
99 changes: 99 additions & 0 deletions src/Commands/MakeDomainCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Lunarstorm\LaravelDDD\Support\DomainResolver;
use Symfony\Component\Process\Process;

class MakeDomainCommand extends Command implements PromptsForMissingInput
{
public $signature = 'ddd:make-domain {domain}';

protected $description = 'Create a new domain';

protected string $domain;
protected mixed $domainStructure;
protected ?string $domainPath;
protected ?string $domainRootNamespace;

protected function promptForMissingArgumentsUsing(): array
{
return [
'domain' => ['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);
}
}
}
1 change: 1 addition & 0 deletions src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down