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.
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.
- Hexagonal or "Ports and Adapters" architecture
- All dependencies are directed towards the core, "outside-in"
- Core is completely independent of the infrastructure
- Clean Architecture based on explicit use cases
- DDD modeling: Entities, Value Objects
- Immutable objects
- Repository pattern
- Testable core
- Domain exceptions hierarchy
- 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
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.
- Kevin Smith, "Modern PHP Without a Framework"
- Kevin Smith, GitHub
- La clean architecture : pourquoi ? Comment ? Pour qui ? - Nicolas DE BOOSE - AFUP Day 2020 Lille
- Robert. C. Martin, Clean Architecture
- Herberto Graça, Ports and Adapters Architecture
- Valeriy Chetkov, PHP Clean Architecture
- Valeriy Chetkov, article on habr.com, only available in Russian