The Clean Architecture in PHP
Total Page:16
File Type:pdf, Size:1020Kb
The Clean Architecture in PHP Kristopher Wilson This book is for sale at http://leanpub.com/cleanphp This version was published on 2015-04-24 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2013 - 2015 Kristopher Wilson Dedication First and foremost, I dedicate this book to my wife, Ashley. Thank you for allowing me to spend so much time staring at millions of dots on a screen. Secondly, to my parents, who worked so hard to make sure their children had everything they needed and wanted, and for encouraging me to follow my dreams, however odd they may have been. Contents Introduction .......................................... i Organization ......................................... i The Author ......................................... i A Word about Coding Style ................................ ii The Problem With Code ................................ 1 Writing Good Code is Hard ................................. 2 Writing Bad Code is Easy .................................. 2 We Can’t Test Anything .................................. 3 Change Breaks Everything ................................. 4 We Live or Die by the Framework ............................. 4 We Want to Use All the Libraries ............................. 5 Writing Good Code ..................................... 5 What is Architecture? ..................................... 6 What does Architecture Look Like? ............................ 6 Layers of Software ..................................... 6 Examples of Poor Architecture ............................... 7 Costs of Poor Architecture ................................. 13 Coupling, The Enemy ..................................... 14 Spaghetti Coupling ..................................... 14 OOP Coupling ........................................ 14 Why is Coupling the Enemy? ................................ 15 How do we Reduce Coupling? ............................... 15 Your Decoupling Toolbox ................................ 17 Design Patterns, A Primer .................................. 18 The Factory Patterns .................................... 18 Repository Pattern ..................................... 24 Adapter Pattern ....................................... 26 Strategy Pattern ....................................... 27 Learning More Design Patterns ............................... 30 SOLID Design Principles ................................... 31 CONTENTS Single Responsibility Principle ............................... 31 Open/Closed Principle ................................... 34 Liskov Substitution Principle ................................ 35 Interface Segregation Principle ............................... 37 Dependency Inversion Principle .............................. 40 Applying SOLID Principles ................................. 42 Dependency Injection ..................................... 43 Inversion of Control ..................................... 44 When to use Dependency Injection ............................ 48 Handling Many Dependencies ............................... 50 Are we still coupling? .................................... 50 Defining a Contract with Interfaces ............................. 52 Interfaces in PHP ...................................... 52 Using Interfaces as Type Hints ............................... 54 Using Interfaces as a Contract ............................... 56 Making Third Party Code Conform to Contracts ..................... 56 Abstracting with Adapters .................................. 57 Setting up the Adapter ................................... 57 How does this help? ..................................... 58 The Clean Architecture ................................. 60 MVC, and its Limitations ................................... 61 MVC in a Diagram ..................................... 61 The MVC Components ................................... 62 Routing ........................................... 65 MVC Isn’t Good Enough .................................. 67 Obese Models ........................................ 67 More Layers for All of the Things! ............................. 68 The Clean Architecture .................................... 69 The Clean Architecture ................................... 69 The Onion Architecture ................................... 71 Framework Independence .................................. 75 The Problem with Frameworks ............................... 75 Framework Independence ................................. 76 This is a Lot of Work .................................... 83 Database Independence .................................... 84 Domain Models ....................................... 84 Domain Services ...................................... 85 Database Infrastructure / Persistence ............................ 89 Organizing the Code .................................... 92 CONTENTS Wrapping it Up ....................................... 93 External Agency Independence ............................... 94 Using Interfaces, Adapters and Dependency Injection . 94 Benefits ........................................... 96 A Case Study in Clean Architecture ........................ 97 The Billing System ...................................... 98 Application Workflow ................................... 98 Prerequisites ......................................... 99 Building Our Domain ..................................... 101 Setting up the Project ....................................101 Creating the Entities ....................................102 Domain Services ......................................106 Wrapping it Up .......................................117 Zend Framework 2 Setup ................................... 118 Installing with Composer ..................................118 Cleaning up the Skeleton ..................................119 Setting up Our Database ..................................122 Table Gateway Factory ...................................128 Wrapping it Up .......................................131 Our Application in Zend Framework 2 ........................... 133 Customer Management ...................................133 Order Management .....................................153 Invoice Management ....................................175 Doctrine 2 ........................................... 196 Rebuilding the Persistence Layer ..............................197 Creating Doctrine-based Repositories . 197 Entity Mapping .......................................201 Integrating Zend Framework and Doctrine . 203 Injecting the New Repositories ...............................205 Updating the Hydrators ...................................207 Summary ..........................................210 Switching to Laravel ..................................... 211 Setting up Laravel ......................................211 Configuring Doctrine ....................................213 Setting up the Dashboard ..................................215 Customer Management ...................................218 Order Management .....................................226 Invoice Management ....................................233 Next Steps ..........................................241 CONTENTS Summary ..........................................241 Introduction Figuring out how to architect a brand new application is a big deal. Doing it the wrong way can lead to a huge headache later. Testing can become hard – or maybe even impossible – and refactoring is an absolute nightmare. While the methods outlined in this book aren’t the only way to go about developing an application, they do provide a framework for developing applications that are: 1. Testable 2. Refactorable 3. Easy to work with 4. Easy to maintain This book is for anyone wanting to build a medium to large sized application that must be around for a long time and/or be easily enhanced in the future. The methods outlined in this book aren’t meant for all applications, and they might be downright over kill for some. If your application is small, or an unproven, new product, it might be best to just get it out the door as fast as possible. If it grows, or becomes successful, later applying these principles may be a good idea to create a solid, long lasting product. The principles outlined in this book involve a learning curve. Writing code this way will slow a developer down until the methods become familiar to them. Organization This book begins by discussing common problems with PHP code and why having good, solid, clean code is important to the success and longevity of an application. From there, we move on to discussing some principles and design patterns that allow us to solve problems with poor code. Using these concepts, we’ll then discuss the Clean Architecture and how it further helps solve problems with bad code. Finally, in the second half of the book, we dive into some real code and build an application following this architecture. When we’re done with our case study application, we’ll start swapping out components, libraries, and frameworks with new ones to prove out the principles of the architecture. The Author My name is Kristopher Wilson. I’ve been developing in PHP since around 2000. That sounds impressive on the surface, but most of those years involved writing truly terrible code. I would Introduction ii have benefited greatly