Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed May 18, 2024
1 parent 95de399 commit 0ade9ef
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/ddd.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@
'job' => 'Jobs',
'listener' => 'Listeners',
'mail' => 'Mail',
'migration' => 'Database\Migrations',
'notification' => 'Notifications',
'observer' => 'Observers',
'policy' => 'Policies',
'provider' => 'Providers',
'resource' => 'Resources',
'rule' => 'Rules',
'scope' => 'Scopes',
'seeder' => 'Database\Seeders',
'trait' => '',
],

Expand Down
36 changes: 36 additions & 0 deletions src/Commands/DomainControllerMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Routing\Console\ControllerMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainControllerMakeCommand extends ControllerMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:controller';

protected function buildModelReplacements(array $replace)
{
$modelClass = $this->parseModel($this->option('model'));

// if (! class_exists($modelClass) && confirm("A {$modelClass} model does not exist. Do you want to generate it?", default: true)) {
// $this->call('make:model', ['name' => $modelClass]);
// }

$replace = $this->buildFormRequestReplacements($replace, $modelClass);

return array_merge($replace, [
'DummyFullModelClass' => $modelClass,
'{{ namespacedModel }}' => $modelClass,
'{{namespacedModel}}' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
'{{ model }}' => class_basename($modelClass),
'{{model}}' => class_basename($modelClass),
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
'{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
'{{modelVariable}}' => lcfirst(class_basename($modelClass)),
]);
}
}
13 changes: 13 additions & 0 deletions src/Commands/DomainMigrateMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainMigrateMakeCommand extends MigrateMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:migration';
}
71 changes: 70 additions & 1 deletion src/Commands/DomainModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ModelMakeCommand;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainModelMakeCommand extends ModelMakeCommand
Expand All @@ -13,8 +14,76 @@ class DomainModelMakeCommand extends ModelMakeCommand

protected function createFactory()
{
$factory = Str::studly($this->argument('name'));

$this->call(DomainFactoryMakeCommand::class, [
'name' => $this->getNameInput().'Factory',
'name' => $factory.'Factory',
'--domain' => $this->domain->dotName,
'--model' => $this->qualifyClass($this->getNameInput()),
]);
}

// protected function createMigration()
// {
// $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name'))));

// if ($this->option('pivot')) {
// $table = Str::singular($table);
// }

// $this->call('make:migration', [
// 'name' => "create_{$table}_table",
// '--create' => $table,
// ]);
// }

/**
* Create a seeder file for the model.
*
* @return void
*/
protected function createSeeder()
{
$seeder = Str::studly(class_basename($this->argument('name')));

$this->call(DomainSeederMakeCommand::class, [
'name' => "{$seeder}Seeder",
'--domain' => $this->domain->dotName,
]);
}

/**
* Create a controller for the model.
*
* @return void
*/
protected function createController()
{
$controller = Str::studly(class_basename($this->argument('name')));

$modelName = $this->qualifyClass($this->getNameInput());

$this->call(DomainControllerMakeCommand::class, array_filter([
'name' => "{$controller}Controller",
'--model' => $this->option('resource') || $this->option('api') ? $modelName : null,
'--api' => $this->option('api'),
'--requests' => $this->option('requests') || $this->option('all'),
'--test' => $this->option('test'),
'--pest' => $this->option('pest'),
]));
}

/**
* Create a policy file for the model.
*
* @return void
*/
protected function createPolicy()
{
$policy = Str::studly(class_basename($this->argument('name')));

$this->call(DomainPolicyMakeCommand::class, [
'name' => "{$policy}Policy",
'--domain' => $this->domain->dotName,
'--model' => $this->qualifyClass($this->getNameInput()),
]);
Expand Down
13 changes: 13 additions & 0 deletions src/Commands/DomainSeederMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Database\Console\Seeds\SeederMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainSeederMakeCommand extends SeederMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:seeder';
}
3 changes: 3 additions & 0 deletions src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@ public function configurePackage(Package $package): void
Commands\DomainCastMakeCommand::class,
Commands\DomainChannelMakeCommand::class,
Commands\DomainConsoleMakeCommand::class,
Commands\DomainControllerMakeCommand::class,
Commands\DomainEventMakeCommand::class,
Commands\DomainExceptionMakeCommand::class,
Commands\DomainJobMakeCommand::class,
Commands\DomainListenerMakeCommand::class,
Commands\DomainMailMakeCommand::class,
// Commands\DomainMigrateMakeCommand::class,
Commands\DomainNotificationMakeCommand::class,
Commands\DomainObserverMakeCommand::class,
Commands\DomainPolicyMakeCommand::class,
Commands\DomainProviderMakeCommand::class,
Commands\DomainResourceMakeCommand::class,
Commands\DomainRuleMakeCommand::class,
Commands\DomainScopeMakeCommand::class,
Commands\DomainSeederMakeCommand::class,
]);

if (app()->version() >= 11) {
Expand Down
File renamed without changes.
174 changes: 174 additions & 0 deletions tests/Generator/Model/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Lunarstorm\LaravelDDD\Support\Domain;
use Lunarstorm\LaravelDDD\Support\Path;

beforeEach(function () {
Config::set('ddd.domain_path', 'src/Domain');
Config::set('ddd.domain_namespace', 'Domain');
});

// it('can generate a domain model with factory', function () {
// $domainName = 'World';
// $modelName = 'Record';

// $domain = new Domain($domainName);

// $factoryName = "{$modelName}Factory";

// $domainModel = $domain->model($modelName);

// $domainFactory = $domain->factory($factoryName);

// $expectedModelPath = base_path($domainModel->path);

// if (file_exists($expectedModelPath)) {
// unlink($expectedModelPath);
// }

// $expectedFactoryPath = base_path($domainFactory->path);

// if (file_exists($expectedFactoryPath)) {
// unlink($expectedFactoryPath);
// }

// Artisan::call('ddd:model', [
// 'name' => $modelName,
// '--domain' => $domain->dotName,
// '--factory' => true,
// ]);

// $output = Artisan::output();

// expect($output)->toContainFilepath($domainModel->path);

// expect(file_exists($expectedModelPath))->toBeTrue("Expecting model file to be generated at {$expectedModelPath}");
// expect(file_exists($expectedFactoryPath))->toBeTrue("Expecting factory file to be generated at {$expectedFactoryPath}");

// expect(file_get_contents($expectedFactoryPath))
// ->toContain("use {$domainModel->fullyQualifiedName};")
// ->toContain("protected \$model = {$modelName}::class;");
// });

it('can generate domain model with options', function ($options, $objectType, $objectName, $expectedObjectPath) {
$domainName = 'World';
$modelName = 'Record';

$domain = new Domain($domainName);

$domainModel = $domain->model($modelName);

$expectedModelPath = base_path($domainModel->path);

if (file_exists($expectedModelPath)) {
unlink($expectedModelPath);
}

if (file_exists($expectedObjectPath)) {
unlink($expectedObjectPath);
}

$command = [
'ddd:model', [
'name' => $modelName,
'--domain' => $domain->dotName,
...$options,
],
];

$this->artisan(...$command)
->expectsOutputToContain(Path::normalize($domainModel->path))
->assertExitCode(0);

$path = base_path($expectedObjectPath);

expect(file_exists($path))->toBeTrue("Expecting {$objectType} to be generated at {$path}");
})->with([
'--factory' => [
['--factory' => true],
'factory',
'RecordFactory',
'src/Domain/World/Database/Factories/RecordFactory.php',
],

'--seed' => [
['--seed' => true],
'seeder',
'RecordSeeder',
'src/Domain/World/Database/Seeders/RecordSeeder.php',
],

'--policy' => [
['--policy' => true],
'policy',
'RecordPolicy',
'src/Domain/World/Policies/RecordPolicy.php',
],
]);

it('can generate domain model with controller', function ($options, $objectType, $objectName, $expectedObjectPath) {
$domainName = 'World';
$modelName = 'Record';

$domain = new Domain($domainName);

$domainModel = $domain->model($modelName);

$expectedModelPath = base_path($domainModel->path);

if (file_exists($expectedModelPath)) {
unlink($expectedModelPath);
}

if (file_exists($expectedObjectPath)) {
unlink($expectedObjectPath);
}

$command = [
'ddd:model', [
'name' => $modelName,
'--domain' => $domain->dotName,
...$options,
],
];

$this->artisan(...$command)
->expectsOutputToContain(Path::normalize($domainModel->path))
->assertExitCode(0);

// $output = Artisan::output();

// $outputPath = Path::normalize($domainModel->path);

// expect($output)->toContainFilepath($domainModel->path);

$path = base_path($expectedObjectPath);

expect(file_exists($path))->toBeTrue("Expecting {$objectType} to be generated at {$path}");

$contents = file_get_contents($path);
dump($contents);
})->with([
'--controller' => [
['--controller' => true],
'controller',
'RecordController',
'app/Http/Controllers/RecordController.php',
],

'--controller --api' => [
['--controller' => true, '--api' => true],
'controller',
'RecordController',
'app/Http/Controllers/RecordController.php',
],

'--controller --requests' => [
['--controller' => true, '--requests' => true],
'controller',
'RecordController',
'app/Http/Controllers/RecordController.php',
],
]);

0 comments on commit 0ade9ef

Please sign in to comment.