Slide 1

Slide 1 text

How Fervo builds web apps Magnus Nordlander

Slide 2

Slide 2 text

Magnus Nordlander • PHP consultant and CEO with Fervo • Worked with PHP for well over 10 year, Symfony for over 9 years.

Slide 3

Slide 3 text

• Small consultancy specialized in 
 modern PHP, Symfony in particular • Three PHP consultants, two
 Symfony certed • Read more at fervo.se

Slide 4

Slide 4 text

Index • Technology radar • Tech Stack • Development methodology • Software architecture

Slide 5

Slide 5 text

Index • Demo of our code structure in practice • Questions

Slide 6

Slide 6 text

Preface • I’m gonna introduce a lot of concepts • I could easily spend 45 minutes per concept or do a multi-day workshop on this • My goal is to pique your interest, so that you want to learn more

Slide 7

Slide 7 text

Preface • Small projects require less structure • Some of the advice within this presentation is more appropriate for large projects • Your mileage may vary

Slide 8

Slide 8 text

Technology Radar

Slide 9

Slide 9 text

Maturity • Generally recommended • Under trial • Promising • Interesting

Slide 10

Slide 10 text

Tech stack

Slide 11

Slide 11 text

Tech Stack • PHP 7 is generally recommended, subsetphp is interesting • HHVM has been promising for a long time, but as PHP7 has become better, the need has diminished • Linux is generally recommended for servers • We prefer Postgres, but MySQL is fine too

Slide 12

Slide 12 text

Tech Stack • Redis is great • Several mature Document oriented databases, but which one to choose depends • ElasticSearch is awesome for search • We love Heroku, and we're fond of AWS.

Slide 13

Slide 13 text

Tech Stack • We're all using Docker for our dev environments • Docker for Mac is interesting, but not good enough yet • Docker in production is very interesting, and we hope to use it in a client project soon

Slide 14

Slide 14 text

Tech Stack • We generally recommend php-fpm • There have been interesting developments for a long time in serving requests from within PHP • Leach (0MQ) • ReactPHP / PHP-PM • PHPFastCGI

Slide 15

Slide 15 text

Development methodology

Slide 16

Slide 16 text

Development methodology • We don't believe in waterfall • We generally recommend BDD, both Story BDD and Spec BDD • Communication and collaboration is key

Slide 17

Slide 17 text

Communication

Slide 18

Slide 18 text

BDD • Behavior driven development • TDD “done right” • TDD + DDD + OOA&D => BDD • Many pay lip service to testing, BDD gives immediate rewards, encouraging adoption

Slide 19

Slide 19 text

Story BDD • Capture business requirements in a testable format • Written in business readable, ubiquitous language • Behat is a great tool for Story BDD, just make sure you run Behat 3

Slide 20

Slide 20 text

Story BDD

Slide 21

Slide 21 text

Spec BDD • Specifies what a class should do and how it should interact with other classes • PHPSpec is excellent • Makes it painfully obvious when you’re violating SOLID

Slide 22

Slide 22 text

Software architecture

Slide 23

Slide 23 text

Software architecture • Your domain code is at the center of your application • Web framework for integrating your domain code with the web • CLI framework for giving your domain code a CLI

Slide 24

Slide 24 text

Domain code • Written like a library • We generally recommend Command-Query Separation • We also generally recommend using events • Commands and events are messages

Slide 25

Slide 25 text

Domain code • Take a DDD approach to modeling your domain • Analyze your problem domain, create a model • Entities, Value objects, Aggregates, Domain events, Services, Repositories and Factories • Event storming looks cool, but we’ve never had the opportunity to do it

Slide 26

Slide 26 text

Messages • Imagine you’re the emperor of Rome. You like to sit in your palace. • When your army has conquered new territory, you are passed a scroll with the details of the event • When you need to quash a rebellion to maintain pax romana, you send a scroll with your command to the governor

Slide 27

Slide 27 text

Messages • Messages differ mainly in intent • Events are informative in nature, and they are passed to anyone whom they concern • Commands are imperative in nature, and they are only passed to their respective command handler

Slide 28

Slide 28 text

–The Office “Look, it doesn't take a genius to know that any organization thrives when it has two leaders. Go ahead, name a country that doesn't have two presidents. A boat that sets sail without two captains. Where would Catholicism be without the popes?

Slide 29

Slide 29 text

Messages • Messages are just small PHP class instances that can be passed around • Immutability is king • Messages do not have return values

Slide 30

Slide 30 text

Messages • To handle a message, we use a message bus • Commands are handled by a command bus • Exactly one handler per command • Events are handled by an event bus • Zero or more event listeners

Slide 31

Slide 31 text

SimpleBus • Message bus library written by Matthias Noback • Messages and handlers are just your own PHP classes • Also supports asynchronous message handling

Slide 32

Slide 32 text

SimpleBus

Slide 33

Slide 33 text

SimpleBus

Slide 34

Slide 34 text

SimpleBus

Slide 35

Slide 35 text

Domain Code • Another interesting pattern is the Specification pattern • RulerZ • Under trial

Slide 36

Slide 36 text

Specification Pattern • All members who have the gold status • SELECT * FROM members WHERE status = ‘gold’

Slide 37

Slide 37 text

Specification Pattern • Not very object oriented • Ties your query builder to both your database engine and your database scheme • Imagine having to implement a rules engine

Slide 38

Slide 38 text

Specification Pattern

Slide 39

Slide 39 text

Specification Pattern

Slide 40

Slide 40 text

Web frameworks • Integrate your domain code into the web • A controller takes an HTTP request, returns an HTTP response • Thin glue to your domain • Your domain code does not have any dependencies on your web framework

Slide 41

Slide 41 text

Web frameworks • Modern frameworks are many things • In Symfony, the web framework is really only HttpKernel and HttpFoundation • Web related components (security, form) • Glue components (dependency injection) • Misc (yaml, ldap)

Slide 42

Slide 42 text

Frameworks • Depending on one of the miscellaneous components is fine if it makes sense. • In Symfony, we have the concept of a bundle, which integrates a library with the framework • Your domain code is a library

Slide 43

Slide 43 text

Dependency Injection • Do not use the Dependency Injection Container as a service locator.

Slide 44

Slide 44 text

Dependency Injection • I’m serious.

Slide 45

Slide 45 text

Dependency Injection • Separate your domain code and your bundle • Your domain code should know nothing of your DI container • Most of your bundle should know nothing of your DI container • Bundle extension and compiler passes mostly

Slide 46

Slide 46 text

Dependency Injection • Symfony controller base class is not a good role model • Luckily, it’s optional

Slide 47

Slide 47 text

Demo

Slide 48

Slide 48 text

Questions?

Slide 49

Slide 49 text

Contact • @drrotmos on Twitter • [email protected] • Want help? We’re a consultancy, so give me a call • https://www.fervo.se