Skip to content

Commit

Permalink
Improved config management plus test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Nov 13, 2024
1 parent 1dab1f5 commit 4661c3a
Show file tree
Hide file tree
Showing 20 changed files with 682 additions and 217 deletions.
17 changes: 5 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Domain Driven Design toolkit for Laravel
# Domain Driven Design Toolkit for Laravel

[![Latest Version on Packagist](https://img.shields.io/packagist/v/lunarstorm/laravel-ddd.svg?style=flat-square)](https://packagist.org/packages/lunarstorm/laravel-ddd)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/lunarstorm/laravel-ddd/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/lunarstorm/laravel-ddd/actions?query=workflow%3Arun-tests+branch%3Amain)
Expand All @@ -21,24 +21,17 @@ php artisan ddd:install

### Peer Dependencies
The following additional packages are suggested (but not required) while working with this package.
- Data Transfer Objects: [spatie/laravel-data](https://github.com/spatie/laravel-data)
- Actions: [lorisleiva/laravel-actions](https://github.com/lorisleiva/laravel-actions)

Data Transfer Objects: [spatie/laravel-data](https://github.com/spatie/laravel-data)
```bash
composer require spatie/laravel-data
```

Actions: [lorisleiva/laravel-actions](https://github.com/lorisleiva/laravel-actions)
```bash
composer require lorisleiva/laravel-actions
```
The default DTO and Action stubs of this package reference classes from these packages. If this doesn't apply to your application, you may [customize the stubs](#publishing-stubs-advanced) accordingly.

### Deployment
In production, run `ddd:optimize` during the deployment process to [optimize autoloading](#autoloading-in-production).
```bash
php artisan ddd:optimize
```
Since Laravel 11.27.1, `php artisan optimize` automatically invokes `ddd:optimize`. If you already run `optimize` in production, a separate `ddd:optimize` is no longer necessary.
Since Laravel 11.27.1, `php artisan optimize` automatically invokes `ddd:optimize`. If you already run `optimize` in production, a separate `ddd:optimize` is no longer necessary. In previous versions of this package, this command was named `ddd:cache`, which will continue to work as an alias.

### Version Compatibility
Laravel | LaravelDDD | |
Expand Down Expand Up @@ -393,8 +386,8 @@ return [
|
*/
'application' => [
'namespace' => 'App\Modules',
'path' => 'app/Modules',
'namespace' => 'App\Modules',
'objects' => [
'controller',
'request',
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
"require": {
"php": "^8.1|^8.2|^8.3",
"illuminate/contracts": "^10.25|^11.0",
"laravel/pint": "^1.18",
"laravel/prompts": "^0.1.16|^0.3.1",
"lorisleiva/lody": "^0.5.0",
"spatie/laravel-package-tools": "^1.13.0"
"spatie/laravel-package-tools": "^1.13.0",
"symfony/var-exporter": "^7.1"
},
"require-dev": {
"larastan/larastan": "^2.0.1",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.0|^8.1",
"orchestra/testbench": "^8|^9.0",
"pestphp/pest": "^2.34",
Expand Down
9 changes: 3 additions & 6 deletions config/ddd.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
|
*/
'application' => [
'namespace' => 'App\Modules',
'path' => 'app/Modules',
'namespace' => 'App\Modules',
'objects' => [
'controller',
'request',
Expand All @@ -45,14 +45,11 @@
| Custom Layers
|--------------------------------------------------------------------------
|
| Mapping of additional top-level namespaces and paths that should
| be recognized as layers when generating ddd:* objects.
| Additional top-level namespaces and paths that should be recognized as
| layers when generating ddd:* objects.
|
| e.g., 'Infrastructure' => 'src/Infrastructure',
|
| When using ddd:* generators, specifying a domain matching a key in
| this array will generate objects in that corresponding layer.
|
*/
'layers' => [
'Infrastructure' => 'src/Infrastructure',
Expand Down
78 changes: 21 additions & 57 deletions config/ddd.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,27 @@ return [
| Application Layer
|--------------------------------------------------------------------------
|
| Configure objects that belong in the application layer.
| Configure domain objects in the application layer.
|
| e.g., App\Modules\Invoicing\Controllers\*
| App\Modules\Invoicing\Requests\*
*/
'application' => {{application}},

/*
|--------------------------------------------------------------------------
| Custom Layers
|--------------------------------------------------------------------------
|
| Additional top-level namespaces and paths that should
| be recognized as layers when generating ddd:* objects.
|
| e.g., 'Infrastructure' => 'src/Infrastructure',
|
| When using ddd:* generators, specifying a domain matching a key in
| this array will generate objects in that corresponding layer.
|
*/
'application' => [
'path' => 'app/Modules',
'namespace' => 'App\Modules',
'layers' => {{layers}},

// Specify which ddd:* objects belong in the application layer
'objects' => [
'controller',
'request',
'middleware',
],
],

/*
|--------------------------------------------------------------------------
Expand All @@ -61,38 +65,7 @@ return [
| Domain\Invoicing\Actions\*
|
*/
'namespaces' => [
'model' => {{namespaces.model}},
'data_transfer_object' => {{namespaces.data_transfer_object}},
'view_model' => {{namespaces.view_model}},
'value_object' => {{namespaces.value_object}},
'action' => {{namespaces.action}},
'cast' => 'Casts',
'class' => '',
'channel' => 'Channels',
'command' => 'Commands',
'controller' => 'Controllers',
'enum' => 'Enums',
'event' => 'Events',
'exception' => 'Exceptions',
'factory' => 'Database\Factories',
'interface' => '',
'job' => 'Jobs',
'listener' => 'Listeners',
'mail' => 'Mail',
'middleware' => 'Middleware',
'migration' => 'Database\Migrations',
'notification' => 'Notifications',
'observer' => 'Observers',
'policy' => 'Policies',
'provider' => 'Providers',
'resource' => 'Resources',
'request' => 'Requests',
'rule' => 'Rules',
'scope' => 'Scopes',
'seeder' => 'Database\Seeders',
'trait' => '',
],
'namespaces' => {{namespaces}},

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -152,13 +125,7 @@ return [
| should be auto-discovered and registered.
|
*/
'autoload' => [
'providers' => true,
'commands' => true,
'policies' => true,
'factories' => true,
'migrations' => true,
],
'autoload' => {{autoload}},

/*
|--------------------------------------------------------------------------
Expand All @@ -175,10 +142,7 @@ return [
| the AppServiceProvider's boot method.
|
*/
'autoload_ignore' => [
'Tests',
'Database/Migrations',
],
'autoload_ignore' => {{autoload_ignore}},

/*
|--------------------------------------------------------------------------
Expand All @@ -189,5 +153,5 @@ return [
| autoloading.
|
*/
'cache_directory' => 'bootstrap/cache/ddd',
'cache_directory' => {{cache_directory}},
];
60 changes: 40 additions & 20 deletions src/Commands/ConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Composer;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\ComposerManager;
Expand All @@ -13,6 +11,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\form;
use function Laravel\Prompts\info;
use function Laravel\Prompts\multiselect;
Expand Down Expand Up @@ -54,6 +53,7 @@ public function handle(): int

return match ($action) {
'wizard' => $this->wizard(),
'update' => $this->update(),
'detect' => $this->detect(),
'composer' => $this->syncComposer(),
'layers' => $this->layers(),
Expand All @@ -65,13 +65,15 @@ protected function home(): int
{
$action = select('Laravel-DDD Config Utility', [
'wizard' => 'Run the configuration wizard',
'update' => 'Update and merge ddd.php with latest package version',
'detect' => 'Detect domain namespace from composer.json',
'composer' => 'Sync composer.json from ddd.php',
'exit' => 'Exit',
], scroll: 10);

return match ($action) {
'wizard' => $this->wizard(),
'update' => $this->update(),
'detect' => $this->detect(),
'composer' => $this->syncComposer(),
'exit' => $this->exit(),
Expand Down Expand Up @@ -114,18 +116,16 @@ protected function wizard(): int
$applicationLayer = $possibleApplicationLayers->first();

$detected = collect([
'domain_namespace' => $domainLayer?->namespace,
'domain_path' => $domainLayer?->path,
'domain_namespace' => $domainLayer?->namespace,
'application' => [
'namespace' => $applicationLayer?->namespace,
'path' => $applicationLayer?->path,
'namespace' => $applicationLayer?->namespace,
],
]);

$config = $detected->merge(Config::get('ddd'));

// dd($config);

info('Detected DDD configuration:');

table(
Expand Down Expand Up @@ -185,8 +185,6 @@ protected function wizard(): int
],
];

// dd($choices['application_namespace']);

$form = form()
->add(
function ($responses) use ($choices, $detected, $config) {
Expand Down Expand Up @@ -218,13 +216,14 @@ function ($responses) use ($choices) {
label: 'Path to Application Layer',
options: $choices['application_path'],
hint: "For objects that don't belong in the domain layer (controllers, form requests, etc.)",
placeholder: 'Leave blank to skip and configure later',
placeholder: 'Leave blank to skip and use defaults',
scroll: 10,
);
},
name: 'application_path'
)
->add(
->addIf(
fn ($responses) => filled($responses['application_path']),
function ($responses) use ($choices, $laravelAppLayer) {
$applicationPath = $responses['application_path'];
$laravelAppPath = $laravelAppLayer->path;
Expand All @@ -240,14 +239,15 @@ function ($responses) use ($choices, $laravelAppLayer) {
options: $choices['application_namespace'],
default: $namespace,
hint: 'The root application namespace.',
placeholder: 'Leave blank to use defaults',
);
},
name: 'application_namespace'
)
->add(
function ($responses) use ($choices) {
return multiselect(
label: 'Additional Layers (Optional)',
label: 'Custom Layers (Optional)',
options: $choices['layers'],
hint: 'Layers can be customized in the ddd.php config file at any time.',
);
Expand All @@ -257,7 +257,11 @@ function ($responses) use ($choices) {

$responses = $form->submit();

// dd($responses);
$this->info('Building configuration...');

DDD::config()->fill($responses)->save();

$this->info('Configuration updated: '.config_path('ddd.php'));

return self::SUCCESS;
}
Expand All @@ -270,8 +274,8 @@ protected function detect(): int

foreach ($search as $namespace) {
if ($path = $this->composer->getAutoloadPath($namespace)) {
$detected['domain_namespace'] = $namespace;
$detected['domain_path'] = $path;
$detected['domain_namespace'] = $namespace;
break;
}
}
Expand All @@ -285,17 +289,33 @@ protected function detect(): int
->all()
);

if (confirm('Update configuration with these values?', true)) {
DDD::config()->fill($detected)->save();

$this->info('Configuration updated: '.config_path('ddd.php'));
}

return self::SUCCESS;
}

protected function applyConfig(Collection $config)
protected function update(): int
{
// $this->composer->update([
// ['domain_namespace', $config['domain_namespace']],
// ['domain_path', $config['domain_path']],
// ['application.namespace', $config['application']['namespace']],
// ['application.path', $config['application']['path']],
// ]);
$config = DDD::config();

$confirmed = confirm('Are you sure you want to update ddd.php and merge with latest copy from the package?');

if (! $confirmed) {
$this->info('Configuration update aborted.');

return self::SUCCESS;
}

$this->info('Merging ddd.php...');

$config->syncWithLatest()->save();

$this->info('Configuration updated: '.config_path('ddd.php'));
$this->warn('Note: Some values may require manual adjustment.');

return self::SUCCESS;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/DomainListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Lunarstorm\LaravelDDD\Support\DomainResolver;
use Lunarstorm\LaravelDDD\Support\Path;

use function Laravel\Prompts\table;

class DomainListCommand extends Command
{
protected $name = 'ddd:list';
Expand All @@ -29,7 +31,7 @@ public function handle()
})
->toArray();

$this->table($headings, $table);
table($headings, $table);

$countDomains = count($table);

Expand Down
Loading

0 comments on commit 4661c3a

Please sign in to comment.