Skip to content

Simple PHP application following Clean Architecture principles

Notifications You must be signed in to change notification settings

gushakov/clean-php

Repository files navigation

Clean Architecture in a PHP application

COPYRIGHT DISCLAIMER

This application is based on the example application by Kevin Smith. Here is the related GitHub repository containing the original source code. Please, consult the copyright and licence notices distributed with the orignal code by Kevin Smith.

General

There is an article on Medium: "Clean Architecture with PHP", which references this repository.

The idea is to build a relatively simple PHP application but without using any framework. I followed closely the recipe given in a wonderful and very detailed article by Kevin Smith.

In addition, the application is so structured as to illustrate the most important principles of Clean Architecture and Use Case driven development. The application itself is a variation of a Hello World, but with a following additions:

  • greetings are displayed randomly
  • greeting messages are persisted in a local database
  • each greeting has an author associated with it
  • an authenticated user can edit the author of any greeting

There is an excellent video by Nicolas De Boose explaining benefits of using Clean Architecture specifically for PHP development. I highly recommend it.

Illustrated principles:

  1. Hexagonal or "Ports and Adapters" architecture
  2. All dependencies are directed towards the core, "outside-in"
  3. Core is completely independent of the infrastructure
  4. Clean Architecture based on explicit use cases
  5. DDD modeling: Entities, Value Objects
  6. Immutable objects
  7. Repository pattern
  8. Testable core
  9. Domain exceptions hierarchy

Running:

  • Requires PHP 8.1 or later
  • See \ExampleApp\Infrastructure\Application\AppConfig::$props global array for configurations
  • Run a development server from the IDE:
> php.exe -S localhost:8080 -t public/
  • Application should be available at localhost:8080

Clean Architecture metrics

There is a wonderful project by Valeriy Chetkov on GitHib: php-clean-architecture. It allows to measure how "clean" the architecture of an application is.

You can try to run this script to check if all dependencies are going from outside ("infrastructure") in ("core"):

> php.exe .\vendor\bin\phpca-check .\phpca-config.php

You can also check out other interesting metrics by running either of the following scripts:

> php.exe .\vendor\bin\phpca-build-reports .\phpca-config.php

or

> php.exe .\vendor\bin\phpca-build-reports .\phpca-config-all.php

To visualize reports, simply open the generated phpca-reports/index.html in a browser.

References:

  1. Kevin Smith, "Modern PHP Without a Framework"
  2. Kevin Smith, GitHub
  3. La clean architecture : pourquoi ? Comment ? Pour qui ? - Nicolas DE BOOSE - AFUP Day 2020 Lille
  4. Robert. C. Martin, Clean Architecture
  5. Herberto Graça, Ports and Adapters Architecture
  6. Valeriy Chetkov, PHP Clean Architecture
  7. Valeriy Chetkov, article on habr.com, only available in Russian

About

Simple PHP application following Clean Architecture principles

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published