|
| 1 | +# Design patterns & architecture presets |
| 2 | + |
| 3 | +Lara Architect generates **architecture presets** (named stacks) and **individual patterns** you can mix with `--patterns=…`. |
| 4 | + |
| 5 | +List everything registered in your app: |
| 6 | + |
| 7 | +```bash |
| 8 | +php artisan architect:patterns |
| 9 | +``` |
| 10 | + |
| 11 | +> **Note:** Pattern `factory` is the Eloquent model factory (`Database\Factories`). The GoF **Abstract Factory** is `abstract-factory` — a different generator. |
| 12 | +
|
| 13 | +--- |
| 14 | + |
| 15 | +## Architecture presets |
| 16 | + |
| 17 | +| Preset | Intent | Patterns included | |
| 18 | +| --- | --- | --- | |
| 19 | +| `service-repository` (default) | Classic layered CRUD | model, migration, factory, enum, repository, service, filter, requests, resource, controller | |
| 20 | +| `actions` | One class per use-case + DTO | model, migration, factory, enum, dto, actions, filter, requests, resource, controller | |
| 21 | +| `adr` | Action–Domain–Responder | Same scaffold as `actions` | |
| 22 | +| `ddd` | Domain folders under `App\Domain\{Module}` | model, migration, factory, enum, dto, repository, service, filter, requests, resource, controller (+ namespace overlays) | |
| 23 | +| `cqrs` | Separate writes (commands) and reads (queries) | model, migration, factory, enum, dto, query, command, filter, requests, resource, controller | |
| 24 | +| `pipeline` | Illuminate Pipeline pipes | model, migration, factory, enum, pipeline, requests, resource, controller | |
| 25 | +| `lean` | Minimal scaffold | model, migration, requests, resource, controller | |
| 26 | + |
| 27 | +```bash |
| 28 | +php artisan make:module Product --architecture=service-repository --fields="name:string, price:decimal" |
| 29 | +php artisan make:module Order --architecture=actions --fields="total:decimal, status:enum" |
| 30 | +php artisan make:module Invoice --architecture=ddd --fields="total:decimal" |
| 31 | +php artisan make:module Report --architecture=cqrs --fields="title:string" |
| 32 | +php artisan make:module Checkout --architecture=pipeline --fields="total:decimal" |
| 33 | +php artisan make:module Tag --architecture=lean --fields="name:string:unique" |
| 34 | +``` |
| 35 | + |
| 36 | +Set a project default: |
| 37 | + |
| 38 | +```env |
| 39 | +LARA_ARCHITECT_ARCHITECTURE=ddd |
| 40 | +``` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## GoF / behavioral & creational patterns |
| 45 | + |
| 46 | +Add with `--patterns=…` (alone or on top of a preset via hand-picked lists): |
| 47 | + |
| 48 | +| Pattern | Generates | Default namespace | |
| 49 | +| --- | --- | --- | |
| 50 | +| `strategy` | Interface + default/alternative strategies + context | `App\Strategies` | |
| 51 | +| `state` | Interface + draft / published / archived states + context | `App\States` | |
| 52 | +| `singleton` | `{Model}Registry` (classic singleton) | `App\Singletons` | |
| 53 | +| `abstract-factory` | Family of factories + notifier/serializer products + client | `App\Factories` | |
| 54 | + |
| 55 | +```bash |
| 56 | +php artisan make:module Order \ |
| 57 | + --patterns=model,migration,strategy,state,singleton,abstract-factory \ |
| 58 | + --fields="total:decimal, status:string" |
| 59 | +``` |
| 60 | + |
| 61 | +### Strategy — interchangeable algorithms |
| 62 | + |
| 63 | +```php |
| 64 | +use App\Strategies\DefaultOrderStrategy; |
| 65 | +use App\Strategies\AlternativeOrderStrategy; |
| 66 | +use App\Strategies\OrderStrategyContext; |
| 67 | + |
| 68 | +$context = new OrderStrategyContext(new DefaultOrderStrategy()); |
| 69 | +$context->execute($order, ['total' => 99.5]); |
| 70 | + |
| 71 | +$context->use(new AlternativeOrderStrategy()); |
| 72 | +$context->execute($order, ['total' => 120]); |
| 73 | + |
| 74 | +// or by registered name: |
| 75 | +$context->use('alternative'); |
| 76 | +``` |
| 77 | + |
| 78 | +**When to use:** shipping calculators, pricing rules, export formats — same interface, swap behaviour. |
| 79 | + |
| 80 | +### State — lifecycle transitions |
| 81 | + |
| 82 | +```php |
| 83 | +use App\States\DraftOrderState; |
| 84 | +use App\States\OrderStateContext; |
| 85 | + |
| 86 | +$context = new OrderStateContext(new DraftOrderState()); |
| 87 | +$context->name(); // 'draft' |
| 88 | +$context->publish(); // → PublishedOrderState |
| 89 | +$context->archive(); // → ArchivedOrderState |
| 90 | +``` |
| 91 | + |
| 92 | +**When to use:** draft → published → archived workflows; gate actions by current state. |
| 93 | + |
| 94 | +### Singleton — one shared registry |
| 95 | + |
| 96 | +```php |
| 97 | +use App\Singletons\OrderRegistry; |
| 98 | + |
| 99 | +$registry = OrderRegistry::getInstance(); |
| 100 | +$registry->set('last_id', $order->id); |
| 101 | +$registry->get('last_id'); |
| 102 | + |
| 103 | +OrderRegistry::reset(); // tests / CLI only |
| 104 | +``` |
| 105 | + |
| 106 | +In Laravel apps prefer the container when you can: |
| 107 | + |
| 108 | +```php |
| 109 | +$this->app->singleton(OrderRegistry::class); |
| 110 | +``` |
| 111 | + |
| 112 | +The generated class is for **explicit Singleton semantics** (teaching, libraries, CLI). |
| 113 | + |
| 114 | +### Abstract Factory — families of related objects |
| 115 | + |
| 116 | +Not the Eloquent `factory` pattern. Generates Standard/Premium factories, notifiers, serializers, and a client that depends only on the abstract factory: |
| 117 | + |
| 118 | +```php |
| 119 | +use App\Factories\Orders\OrderComponentClient; |
| 120 | +use App\Factories\Orders\StandardOrderComponentFactory; |
| 121 | +use App\Factories\Orders\PremiumOrderComponentFactory; |
| 122 | + |
| 123 | +$client = new OrderComponentClient(new StandardOrderComponentFactory()); |
| 124 | +$client->dispatch(['id' => 1, 'total' => 50]); |
| 125 | + |
| 126 | +// swap the whole product family: |
| 127 | +$client = new OrderComponentClient(new PremiumOrderComponentFactory()); |
| 128 | +$client->dispatch(['id' => 1, 'total' => 50]); |
| 129 | +``` |
| 130 | + |
| 131 | +**When to use:** themed/partner integrations where notifier + serializer must stay consistent as a set. |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## DDD preset — domain layout |
| 136 | + |
| 137 | +```bash |
| 138 | +php artisan make:module Invoice --architecture=ddd --fields="total:decimal, status:enum" |
| 139 | +``` |
| 140 | + |
| 141 | +Typical layout (namespaces from `architecture_namespaces.ddd`): |
| 142 | + |
| 143 | +``` |
| 144 | +app/ |
| 145 | +├── Domain/Invoice/ |
| 146 | +│ ├── Models/Invoice.php |
| 147 | +│ ├── Services/InvoiceService.php |
| 148 | +│ ├── Data/InvoiceData.php |
| 149 | +│ ├── Enums/InvoiceStatus.php |
| 150 | +│ └── Filters/InvoiceFilter.php |
| 151 | +└── Infrastructure/Invoice/InvoiceRepository.php |
| 152 | +``` |
| 153 | + |
| 154 | +Use the service the same way as service-repository; only the folders change. |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## CQRS preset — commands & queries |
| 159 | + |
| 160 | +```bash |
| 161 | +php artisan make:module Report --architecture=cqrs --fields="title:string, body:text" |
| 162 | +``` |
| 163 | + |
| 164 | +``` |
| 165 | +app/ |
| 166 | +├── Commands/Reports/CreateReportCommand.php |
| 167 | +├── Commands/Reports/UpdateReportCommand.php |
| 168 | +├── Commands/Reports/DeleteReportCommand.php |
| 169 | +├── Queries/Reports/ListReportsQuery.php |
| 170 | +├── Queries/Reports/GetReportQuery.php |
| 171 | +└── DTOs/ReportData.php |
| 172 | +``` |
| 173 | + |
| 174 | +```php |
| 175 | +// writes |
| 176 | +CreateReportCommand::run(ReportData::fromRequest($request)); |
| 177 | + |
| 178 | +// reads |
| 179 | +$reports = ListReportsQuery::run($filter); |
| 180 | +$report = GetReportQuery::run($id); |
| 181 | +``` |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +## Pipeline preset |
| 186 | + |
| 187 | +```bash |
| 188 | +php artisan make:module Checkout --architecture=pipeline --fields="total:decimal" |
| 189 | +``` |
| 190 | + |
| 191 | +```php |
| 192 | +use App\Pipelines\Checkouts\CheckoutPipeline; |
| 193 | + |
| 194 | +$result = app(CheckoutPipeline::class)->send([ |
| 195 | + 'total' => 149.99, |
| 196 | + // … |
| 197 | +]); |
| 198 | +``` |
| 199 | + |
| 200 | +Pipes default to validate → persist; add your own pipes in `through([...])`. |
| 201 | + |
| 202 | +--- |
| 203 | + |
| 204 | +## Actions / ADR |
| 205 | + |
| 206 | +```bash |
| 207 | +php artisan make:module Order --architecture=actions --fields="total:decimal, status:enum" |
| 208 | +# same scaffold: |
| 209 | +php artisan make:module Order --architecture=adr --fields="total:decimal, status:enum" |
| 210 | +``` |
| 211 | + |
| 212 | +```php |
| 213 | +CreateOrder::run(OrderData::fromRequest($request)); |
| 214 | +UpdateOrder::run($order, OrderData::fromRequest($request)); |
| 215 | +DeleteOrder::run($order); |
| 216 | +``` |
| 217 | + |
| 218 | +--- |
| 219 | + |
| 220 | +## Compose your own |
| 221 | + |
| 222 | +```bash |
| 223 | +# Model + service, no repository |
| 224 | +php artisan make:module Invoice \ |
| 225 | + --patterns=model,migration,service,requests,resource,controller \ |
| 226 | + --fields="number:string:unique, total:decimal" |
| 227 | + |
| 228 | +# CRUD + Strategy + State |
| 229 | +php artisan make:module Ticket \ |
| 230 | + --patterns=model,migration,factory,service,repository,filter,requests,resource,controller,strategy,state \ |
| 231 | + --fields="title:string, status:enum" |
| 232 | +``` |
| 233 | + |
| 234 | +Register custom presets under `architectures` in `config/lara-architect.php` — see [Getting started §6.5](../getting-started.md#65-project-default-and-custom-presets). |
0 commit comments