This week, the php|architect magazine has published an article of mine about “Hexagonal Architecture with PHP”. Here’s the introduction:
“With the rise of DDD (domain-driven development), architectures promoting domain-centric designs are becoming more popular. This is the case with Hexagonal Architecture, also known as Ports and Adapters, that seems to have been rediscovered recently by PHP developers. Invented in 2005 by Alistair Cockburn, one of the Agile Manifesto authors, the Hexagonal Architecture allows an application to be equally driven by users, programs, automated tests, or batch scripts, while being developed and tested in isolation from its eventual run-time devices and databases. This results in infrastructure-agnostic web applications that are easier to test, write, and maintain. Let’s see how to apply it using real PHP examples.”
I would like to share with you some thought, the cover and the code repository.
My main goal was to show how to apply hexagonal architecture in the real world migrating from a legacy and coupled Zend Framework v1 application using MySQL to a decoupled domain-centric application using Use Cases and abstractions. I show how to refactor the Zend Framework web app to use UseCases, add Silex support as a REST application and Symfony Console for command line application.
Everything is unit tested with 100% coverage showing when to mock and how to w/o mockery. I also show how to easily switch persistence strategy from MySQL to Redis when everything is decoupled. At last, I cover how to use Symfony Service Container component to make easier the process to generate the dependence chain. 14 pages of full refactoring. If you are interested in the whole article and a bunch of greater ones, you can buy July Issue of the magazine. The code repository with fixed typos can be found here.
If you have any comment, please send me an email or tweet. Hope you like it. I would like to thank everyone in the phparch team for asking me to write this article and giving support during the process.