plin-code/laravel-clean-architecture

Laravel package for generating Clean Architecture structure

dev-main 2025-05-30 08:04 UTC

This package is not auto-updated.

Last update: 2025-06-13 17:54:03 UTC


README

A Laravel package to easily implement Clean Architecture in your projects. ๐Ÿš€

๐Ÿงช Tests ๐ŸŽจ Code Style ๐Ÿ” Static Analysis ๐Ÿ“ฆ Latest Stable Version ๐Ÿ“„ License

โœจ Features

  • ๐ŸŽฏ Domain-Driven Design - Organize your code with DDD principles
  • โšก Quick Setup - Get started with Clean Architecture in minutes
  • ๐Ÿงฉ Auto-Generation - Generate complete domains with one command
  • ๐Ÿ›๏ธ Layer Separation - Clear separation between Domain, Application, and Infrastructure
  • ๐Ÿ”ง Customizable - Flexible configuration to fit your project needs
  • ๐Ÿงช Test-Ready - Pre-built test templates for immediate testing
  • ๐Ÿ“š Well-Documented - Comprehensive documentation and examples
  • ๐ŸŽจ Modern PHP - Built for PHP 8.3+ with latest Laravel features

๐Ÿ“‹ Requirements

  • ๐Ÿ˜ PHP 8.3+
  • โšก Laravel 12.x

๐Ÿ“ฆ Installation

composer require plin-code/laravel-clean-architecture

โš™๏ธ Configuration

Publish the configuration files and stubs:

php artisan vendor:publish --provider="PlinCode\LaravelCleanArchitecture\CleanArchitectureServiceProvider"

๐ŸŽฏ Usage

๐Ÿ—๏ธ Installing Clean Architecture structure

php artisan clean-arch:install

This command will create:

  • ๐Ÿ“ Folder structure for Domain, Application and Infrastructure layers
  • ๐Ÿงฉ Base classes (BaseModel, BaseAction, BaseService, etc.)
  • โš™๏ธ Configuration file
  • ๐Ÿ“– Documentation

๐Ÿ†• Creating a new domain

php artisan clean-arch:make-domain User

This command will generate:

  • ๐Ÿ›๏ธ Domain model with events
  • ๐Ÿ“Š Status enums
  • ๐Ÿ”” Domain events (Created, Updated, Deleted)
  • โšก Actions (Create, Update, Delete, GetById)
  • ๐Ÿ”ง Service
  • ๐ŸŒ API Controller
  • ๐Ÿ“ Form Requests (Create, Update)
  • ๐Ÿ“ค API Resource
  • ๐Ÿงช Feature tests

๐Ÿ› ๏ธ Available commands

  • clean-arch:install - ๐Ÿ—๏ธ Install Clean Architecture structure
  • clean-arch:make-domain {name} - ๐Ÿ†• Create a complete new domain
  • clean-arch:make-action {name} {domain} - โšก Create a new action
  • clean-arch:make-service {name} - ๐Ÿ”ง Create a new service
  • clean-arch:make-controller {name} - ๐ŸŒ Create a new controller
  • clean-arch:generate-package {name} {vendor} - ๐Ÿ“ฆ Generate a new package

๐Ÿ“‚ Generated structure

app/
โ”œโ”€โ”€ Application/
โ”‚   โ”œโ”€โ”€ Actions/
โ”‚   โ”‚   โ””โ”€โ”€ Users/
โ”‚   โ”‚       โ”œโ”€โ”€ CreateUserAction.php
โ”‚   โ”‚       โ”œโ”€โ”€ UpdateUserAction.php
โ”‚   โ”‚       โ”œโ”€โ”€ DeleteUserAction.php
โ”‚   โ”‚       โ””โ”€โ”€ GetByIdUserAction.php
โ”‚   โ””โ”€โ”€ Services/
โ”‚       โ””โ”€โ”€ UserService.php
โ”œโ”€โ”€ Domain/
โ”‚   โ””โ”€โ”€ Users/
โ”‚       โ”œโ”€โ”€ User.php
โ”‚       โ”œโ”€โ”€ Enums/
โ”‚   โ”‚   โ””โ”€โ”€ UserStatus.php
โ”‚   โ””โ”€โ”€ Events/
โ”‚       โ”œโ”€โ”€ UserCreated.php
โ”‚       โ”œโ”€โ”€ UserUpdated.php
โ”‚       โ””โ”€โ”€ UserDeleted.php
โ””โ”€โ”€ Infrastructure/
    โ””โ”€โ”€ API/
        โ”œโ”€โ”€ Controllers/
        โ”‚   โ””โ”€โ”€ UsersController.php
        โ”œโ”€โ”€ Requests/
        โ”‚   โ”œโ”€โ”€ CreateUserRequest.php
        โ”‚   โ””โ”€โ”€ UpdateUserRequest.php
        โ””โ”€โ”€ Resources/
            โ””โ”€โ”€ UserResource.php

๐Ÿ›๏ธ Clean Architecture Principles

This package implements Clean Architecture principles:

  1. ๐ŸŽฏ Domain Layer: Contains business logic and entities
  2. โšก Application Layer: Contains use cases and application logic
  3. ๐Ÿ—๏ธ Infrastructure Layer: Contains implementation details (controllers, database, etc.)

๐Ÿ”— Dependencies

  • ๐ŸŽฏ Domain Layer: Does not depend on any other layer
  • โšก Application Layer: Depends only on Domain Layer
  • ๐Ÿ—๏ธ Infrastructure Layer: Depends on Application and Domain Layers

๐Ÿ’ก Examples

๐Ÿ›๏ธ Creating a Product domain

php artisan clean-arch:make-domain Product

๐ŸŽฎ Using in controller

class ProductsController extends Controller
{
    public function __construct(
        private CreateProductAction $createProductAction,
        private ProductService $productService
    ) {}

    public function store(CreateProductRequest $request): JsonResponse
    {
        $product = $this->createProductAction->execute($request);
        
        return response()->json([
            'data' => new ProductResource($product),
            'message' => 'Product created successfully'
        ], 201);
    }
}

โš™๏ธ Configuration

The configuration file config/clean-architecture.php allows you to customize:

  • ๐Ÿท๏ธ Default namespace
  • ๐Ÿ“ Directory paths
  • โœ… Validation options
  • ๐Ÿ“Š Logging settings

๐Ÿ› ๏ธ Development

This package uses several tools to maintain code quality:

๐Ÿ”ง Code Quality Tools

  • ๐ŸŽจ Laravel Pint - Code formatting and style fixing
  • ๐Ÿ” PHPStan - Static analysis for finding bugs
  • ๐Ÿงช PEST - Modern testing framework built on PHPUnit
  • ๐ŸŽญ Orchestra Testbench - Laravel package testing

๐Ÿ“œ Available Scripts

# ๐Ÿงช Run tests
composer test

# ๐Ÿ“Š Run tests with coverage
composer test-coverage

# ๐ŸŽจ Fix code style
composer format

# ๐Ÿ‘€ Check code style without fixing
composer format-test

# ๐Ÿ” Run static analysis
composer analyse

# โœจ Run all quality checks
composer quality

๐Ÿš€ Development Setup

  1. ๐Ÿ“ฅ Clone the repository
  2. ๐Ÿ“ฆ Install dependencies: composer install
  3. โœจ Run quality checks: composer quality

๐Ÿค Contributing

Pull requests are welcome! ๐ŸŽ‰ For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate and follow our Contributing Guidelines. ๐Ÿ“

๐Ÿ“„ License

MIT ๐Ÿ“œ