|
1 | 1 | # LaraArchitect |
2 | 2 |
|
3 | | -An architecture toolkit for Laravel. It gives you two things: |
| 3 | +An architecture toolkit for Laravel. It gives you three things: |
4 | 4 |
|
5 | 5 | 1. **Runtime building blocks** — lean base classes for repositories (with full soft-delete support), CRUD services, single-purpose actions, request-driven query filters, data transfer objects, form requests and JSON responses, so business logic stays out of your controllers. |
6 | | -2. **A dynamic module generator** — one `make:module` command that scaffolds an entire feature (model, migration, factory, enums, service/actions, filter, requests, resource, controller) using **configurable architecture presets**. Prefer actions over services? Repository pattern? Your own preset? It is all config. |
| 6 | +2. **A dynamic module generator** — one `make:module` command (plus `architect:feature` for policy/seeder/test, and the `architect:new` interactive wizard) that scaffolds an entire feature using **configurable architecture presets**. Prefer actions over services? Repository pattern? Domain layout? Your own preset? It is all config — versionable per team via `architect.json`. |
| 7 | +3. **Architecture guardrails** — `architect:lint` fails CI when conventions are broken (Eloquent in controllers, inline validation, inverted dependencies) and `architect:analyze` reports layer counts and hotspots. |
7 | 8 |
|
8 | 9 | ```bash |
9 | 10 | php artisan make:module Product --fields="name:string, price:decimal, sku:string:unique, status:enum, notes:text:nullable" |
@@ -53,7 +54,11 @@ Published stubs live in `stubs/lara-architect/` and always win over the package |
53 | 54 | php artisan architect:patterns |
54 | 55 | ``` |
55 | 56 |
|
56 | | -**2.** Generate a module (add `--dry-run` first to preview without writing). Default is **API** (JsonResource + `Http\Controllers\Api`). Use `--ui=web` for Blade: |
| 57 | +**2.** Generate a module (add `--dry-run` first to preview without writing). Default is **API** (JsonResource + `Http\Controllers\Api`). Use `--ui=web` for Blade. Not sure which flags you want? Run the interactive wizard instead: |
| 58 | + |
| 59 | +```bash |
| 60 | +php artisan architect:new |
| 61 | +``` |
57 | 62 |
|
58 | 63 | ```bash |
59 | 64 | # API (default) — controller under App\Http\Controllers\Api + ProductResource |
@@ -120,8 +125,16 @@ php artisan make:module Order --architecture=actions --fields="total:decimal, st |
120 | 125 |
|
121 | 126 | # Or hand-pick patterns — no preset needed |
122 | 127 | php artisan make:module Tag --patterns=model,migration,resource,controller --fields="name:string:unique" |
| 128 | + |
| 129 | +# Complete feature: preset patterns + policy + seeder + feature test |
| 130 | +php artisan architect:feature Product --fields="name:string, price:decimal" |
| 131 | + |
| 132 | +# Interactive wizard — answers a few questions, then generates |
| 133 | +php artisan architect:new |
123 | 134 | ``` |
124 | 135 |
|
| 136 | +`architect:feature` accepts the same flags as `make:module` and appends the patterns listed in `generation.feature_extras` (`policy`, `seeder`, `test` by default), so one command ships a model with a passing test, a seeder wired to the factory, and a policy ready to register. |
| 137 | + |
125 | 138 | Other useful flags: |
126 | 139 |
|
127 | 140 | | Flag | Effect | |
@@ -193,6 +206,57 @@ Every pattern is a class implementing `KarimAshraf\LaraArchitect\Contracts\Gener |
193 | 206 | php artisan make:module Invoice --architecture=my-team-style |
194 | 207 | ``` |
195 | 208 |
|
| 209 | +### Team conventions with `architect.json` |
| 210 | + |
| 211 | +Commit an `architect.json` at your project root to version your team's conventions without publishing the package config. Anything under it deep-merges over `config/lara-architect.php` when the generator runs: |
| 212 | + |
| 213 | +```json |
| 214 | +{ |
| 215 | + "generation": { |
| 216 | + "default_architecture": "actions", |
| 217 | + "default_ui": "api", |
| 218 | + "namespaces": { |
| 219 | + "service": "App\\Domain\\{module}\\Services", |
| 220 | + "repository": "App\\Domain\\{module}\\Repositories" |
| 221 | + } |
| 222 | + } |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +### Domain / modular layouts |
| 227 | + |
| 228 | +Namespace values support a `{module}` placeholder that is replaced with the model name, so you can generate into a domain-oriented structure: |
| 229 | + |
| 230 | +```php |
| 231 | +'namespaces' => [ |
| 232 | + 'service' => 'App\\Domain\\{module}\\Services', // App\Domain\Product\Services\ProductService |
| 233 | + 'repository' => 'App\\Domain\\{module}\\Repositories', // App\Domain\Product\Repositories\ProductRepository |
| 234 | +], |
| 235 | +``` |
| 236 | + |
| 237 | +## Architecture lint & analysis |
| 238 | + |
| 239 | +Generating a clean structure is half the job — keeping it clean is the other half. Two read-only commands help with that: |
| 240 | + |
| 241 | +```bash |
| 242 | +# Fails (exit code 1) when conventions are broken — wire it into CI |
| 243 | +php artisan architect:lint |
| 244 | + |
| 245 | +# Layer counts + hotspots (long classes, fat constructors, God services) |
| 246 | +php artisan architect:analyze |
| 247 | +``` |
| 248 | + |
| 249 | +`architect:lint` ships with four rules, each a class implementing `Contracts\LintRule` (register your own in `lint.rules`): |
| 250 | + |
| 251 | +| Rule | Catches | |
| 252 | +| --- | --- | |
| 253 | +| `no-eloquent-in-controllers` | Controllers calling `Model::create/update/where(...)` or the `DB` facade directly | |
| 254 | +| `no-repositories-in-controllers` | Controllers injecting repositories instead of services/actions | |
| 255 | +| `no-inline-validation-in-controllers` | `$request->validate(...)` / `Validator::make(...)` instead of form requests | |
| 256 | +| `models-do-not-depend-on-http` | Models importing controllers, form requests or services | |
| 257 | + |
| 258 | +`architect:analyze` reports how many controllers/models/services/repositories/actions exist and flags classes above the configurable thresholds (`lint.thresholds`): more than 8 public methods, more than 5 constructor dependencies, or files longer than 300 lines. |
| 259 | + |
196 | 260 | ## Runtime building blocks |
197 | 261 |
|
198 | 262 | ### Repository |
|
0 commit comments