Slide 1

Slide 1 text

Going Bare Writing the web without a framework Sammy Kaye Powers 2018-11-15

Slide 2

Slide 2 text

Thanks to Our Sponsors 2018

Slide 3

Slide 3 text

joind.in/talk/c3f16 @SammyK #phpworld Slides Get the joind.in/talk/c3f16

Slide 4

Slide 4 text

joind.in/talk/c3f16 @SammyK #phpworld frameworks The point is not <3 frameworks! I

Slide 5

Slide 5 text

joind.in/talk/c3f16 @SammyK #phpworld opinion It’s OK to have an

Slide 6

Slide 6 text

joind.in/talk/c3f16 @SammyK #phpworld experiment It’s OK to

Slide 7

Slide 7 text

joind.in/talk/c3f16 @SammyK #phpworld Bootstrapping / ˈbo ͞ otˌstraˌpiNG / Define: (…at least for this talk) Code that acts as an entry point into an app.

Slide 8

Slide 8 text

joind.in/talk/c3f16 @SammyK #phpworld Boilerplate / ˈboilərˌplāt / Define: The “glue” code that ties components together. (…at least for this talk)

Slide 9

Slide 9 text

joind.in/talk/c3f16 @SammyK #phpworld Frameworks are a tool

Slide 10

Slide 10 text

joind.in/talk/c3f16 @SammyK #phpworld Going bare is a skill (that you don’t get with a framework)

Slide 11

Slide 11 text

joind.in/talk/c3f16 @SammyK #phpworld The of going bare skill • Writing bootstrap & boilerplate • Choosing exact components • Use different paradigms • Use those skills on domain code (Get to know the component ecosystem) (Like singleton, et al) (Gives you more control over how components interact) (Makes you better at writing your domain code)

Slide 12

Slide 12 text

joind.in/talk/c3f16 @SammyK #phpworld Myths about going frameworkless

Slide 13

Slide 13 text

joind.in/talk/c3f16 @SammyK #phpworld Myth #1 You must write everything from scratch

Slide 14

Slide 14 text

joind.in/talk/c3f16 @SammyK #phpworld Myth #2 You’re inherently anti-framework or anti-convention

Slide 15

Slide 15 text

joind.in/talk/c3f16 @SammyK #phpworld Myth #3 You’ll spend most of your time on boilerplate instead of on domain code

Slide 16

Slide 16 text

joind.in/talk/c3f16 @SammyK #phpworld Myth #4 You must be an architecture astronaut

Slide 17

Slide 17 text

joind.in/talk/c3f16 @SammyK #phpworld Myth #5 You like pain

Slide 18

Slide 18 text

joind.in/talk/c3f16 @SammyK #phpworld Pros Framework Cons

Slide 19

Slide 19 text

joind.in/talk/c3f16 @SammyK #phpworld Pros

Slide 20

Slide 20 text

joind.in/talk/c3f16 @SammyK #phpworld Pros

Slide 21

Slide 21 text

joind.in/talk/c3f16 @SammyK #phpworld Pros crazy fast You get started

Slide 22

Slide 22 text

joind.in/talk/c3f16 @SammyK #phpworld Pros First-time install time minutes! 5

Slide 23

Slide 23 text

joind.in/talk/c3f16 @SammyK #phpworld Pros

Slide 24

Slide 24 text

joind.in/talk/c3f16 @SammyK #phpworld Pros boilerplate all the things You don’t have to Con but it can be a

Slide 25

Slide 25 text

joind.in/talk/c3f16 @SammyK #phpworld Pros bootstrap & boilerplate code You don’t have to maintain

Slide 26

Slide 26 text

joind.in/talk/c3f16 @SammyK #phpworld Pros battle-tested The code is

Slide 27

Slide 27 text

joind.in/talk/c3f16 @SammyK #phpworld Pros conventions Ready-to-follow Con but it can be a

Slide 28

Slide 28 text

joind.in/talk/c3f16 @SammyK #phpworld Pros documentation Most frameworks have great

Slide 29

Slide 29 text

joind.in/talk/c3f16 @SammyK #phpworld Pros marketable It makes you more

Slide 30

Slide 30 text

joind.in/talk/c3f16 @SammyK #phpworld Pros pragmatic They are often

Slide 31

Slide 31 text

joind.in/talk/c3f16 @SammyK #phpworld cons? What about the

Slide 32

Slide 32 text

joind.in/talk/c3f16 @SammyK #phpworld Cons legacy codebases Useless for

Slide 33

Slide 33 text

joind.in/talk/c3f16 @SammyK #phpworld Cons cruft They fill your project with

Slide 34

Slide 34 text

joind.in/talk/c3f16 @SammyK #phpworld Cons Cruft • Deployment takes longer • Upgrading pulls in all the new features

Slide 35

Slide 35 text

joind.in/talk/c3f16 @SammyK #phpworld Cons tightly couple Easier to due to convenience

Slide 36

Slide 36 text

joind.in/talk/c3f16 @SammyK #phpworld

Slide 37

Slide 37 text

5.3

Slide 38

Slide 38 text

5.7 2018-09-04

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

5.7 Did not support laravelcollective/html mccool/laravel-auto-presenter

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

joind.in/talk/c3f16 @SammyK #phpworld Cons upgrading Framework-specific deps can keep you from

Slide 44

Slide 44 text

joind.in/talk/c3f16 @SammyK #phpworld The The Cons Pros seem great are weak…

Slide 45

Slide 45 text

joind.in/talk/c3f16 @SammyK #phpworld Why would you go bare? (To answer we must ask…)

Slide 46

Slide 46 text

joind.in/talk/c3f16 @SammyK #phpworld When should you go bare?

Slide 47

Slide 47 text

joind.in/talk/c3f16 @SammyK #phpworld

Slide 48

Slide 48 text

joind.in/talk/c3f16 @SammyK #phpworld Greenfield Legacy Depends

Slide 49

Slide 49 text

joind.in/talk/c3f16 @SammyK #phpworld 1% 99% 90% 10% Go Bare Framwork Greenfield Legacy Going bare vs framework

Slide 50

Slide 50 text

joind.in/talk/c3f16 @SammyK #phpworld How do you go bare?

Slide 51

Slide 51 text

joind.in/talk/c3f16 @SammyK #phpworld from scratch You could just write

Slide 52

Slide 52 text

joind.in/talk/c3f16 @SammyK #phpworld

Slide 53

Slide 53 text

joind.in/talk/c3f16 @SammyK #phpworld That means writing all the things Router DBAL/ORM Dep Injection Logger

Slide 54

Slide 54 text

joind.in/talk/c3f16 @SammyK #phpworld reinventing the wheel But that’s like

Slide 55

Slide 55 text

joind.in/talk/c3f16 @SammyK #phpworld Better Way™ There’s a

Slide 56

Slide 56 text

joind.in/talk/c3f16 @SammyK #phpworld po- nents Com-

Slide 57

Slide 57 text

joind.in/talk/c3f16 @SammyK #phpworld components has a healthy ecosystem of

Slide 58

Slide 58 text

joind.in/talk/c3f16 @SammyK #phpworld using them You’re probably already

Slide 59

Slide 59 text

joind.in/talk/c3f16 @SammyK #phpworld built on them Most frameworks are zend-router zend-db zend-di zend-log

Slide 60

Slide 60 text

joind.in/talk/c3f16 @SammyK #phpworld choose ur candy Going bare lets you Look at all those smarties! Yum!

Slide 61

Slide 61 text

joind.in/talk/c3f16 @SammyK #phpworld I like smarties

Slide 62

Slide 62 text

joind.in/talk/c3f16 @SammyK #phpworld Router DBAL/ORM Dep Injection Logger

Slide 63

Slide 63 text

joind.in/talk/c3f16 @SammyK #phpworld zend-router fast-route php-router phroute Routers Tons of variety

Slide 64

Slide 64 text

joind.in/talk/c3f16 @SammyK #phpworld Easily installed with

Slide 65

Slide 65 text

joind.in/talk/c3f16 @SammyK #phpworld bag o’ candy A framework is like a Comes with licorice? Yuck!

Slide 66

Slide 66 text

joind.in/talk/c3f16 @SammyK #phpworld I don’t like licorice

Slide 67

Slide 67 text

joind.in/talk/c3f16 @SammyK #phpworld components When you go bare don’t just magically start working together

Slide 68

Slide 68 text

joind.in/talk/c3f16 @SammyK #phpworld Key: Get comfy with bootstrapping & boilerplate code It’s really not that bad

Slide 69

Slide 69 text

joind.in/talk/c3f16 @SammyK #phpworld examples Real-world

Slide 70

Slide 70 text

joind.in/talk/c3f16 @SammyK #phpworld conventions When going bare, there aren’t any …so this is a way, not the way

Slide 71

Slide 71 text

joind.in/talk/c3f16 @SammyK #phpworld Greenfield Let’s start with

Slide 72

Slide 72 text

joind.in/talk/c3f16 @SammyK #phpworld Case study Raw CSV Calculate
 & save to DB Show report Client’s server Our server

Slide 73

Slide 73 text

joind.in/talk/c3f16 @SammyK #phpworld Case study Download CSV & calculate CLI View the report HTTP Endpoint

Slide 74

Slide 74 text

joind.in/talk/c3f16 @SammyK #phpworld coding domain in 5 mins Install framework &

Slide 75

Slide 75 text

joind.in/talk/c3f16 @SammyK #phpworld 61components installed before we code https://framework.zend.com/downloads

Slide 76

Slide 76 text

joind.in/talk/c3f16 @SammyK #phpworld go micro We could… Slim Silex (et al)

Slide 77

Slide 77 text

joind.in/talk/c3f16 @SammyK #phpworld Silex DEAD!

Slide 78

Slide 78 text

joind.in/talk/c3f16 @SammyK #phpworld go bare! :) But let’s

Slide 79

Slide 79 text

joind.in/talk/c3f16 @SammyK #phpworld Start in the CLI (Just like when you use a framework!)

Slide 80

Slide 80 text

joind.in/talk/c3f16 @SammyK #phpworld 1. Make an empty composer.json or… $ composer init

Slide 81

Slide 81 text

joind.in/talk/c3f16 @SammyK #phpworld 2. Make a folder for domain code $ mkdir src

Slide 82

Slide 82 text

joind.in/talk/c3f16 @SammyK #phpworld 3. Add autoloading $ composer \ dumpautoload And then run…

Slide 83

Slide 83 text

joind.in/talk/c3f16 @SammyK #phpworld Now just install your components & tie them together

Slide 84

Slide 84 text

joind.in/talk/c3f16 @SammyK #phpworld Key: Get comfy with bootstrapping & boilerplate code It’s really not that bad

Slide 85

Slide 85 text

joind.in/talk/c3f16 @SammyK #phpworld Case study Download CSV & calculate CLI View the report HTTP Endpoint

Slide 86

Slide 86 text

joind.in/talk/c3f16 @SammyK #phpworld Install a console component $ composer require symfony/console (or maybe you like zendframework/zend-console)

Slide 87

Slide 87 text

joind.in/talk/c3f16 @SammyK #phpworld src/Console/Runner.php

Slide 88

Slide 88 text

joind.in/talk/c3f16 @SammyK #phpworld We get to make our first bootstrap code!

Slide 89

Slide 89 text

joind.in/talk/c3f16 @SammyK #phpworld run (sans .php extension) $ chmod +x run

Slide 90

Slide 90 text

joind.in/talk/c3f16 @SammyK #phpworld $ ./run

Slide 91

Slide 91 text

joind.in/talk/c3f16 @SammyK #phpworld B.A.U. Now it’s just src/Console/Runner.php

Slide 92

Slide 92 text

joind.in/talk/c3f16 @SammyK #phpworld src/Console/CalculateReportsCommand.php

Slide 93

Slide 93 text

joind.in/talk/c3f16 @SammyK #phpworld $ ./run

Slide 94

Slide 94 text

joind.in/talk/c3f16 @SammyK #phpworld

Slide 95

Slide 95 text

joind.in/talk/c3f16 @SammyK #phpworld Congrats!

Slide 96

Slide 96 text

joind.in/talk/c3f16 @SammyK #phpworld Install the DI container $ composer require pimple/pimple (Choose your poison)

Slide 97

Slide 97 text

joind.in/talk/c3f16 @SammyK #phpworld Need to make singleton ready src/Application.php

Slide 98

Slide 98 text

joind.in/talk/c3f16 @SammyK #phpworld Singleton Isn’t that an anti-pattern?

Slide 99

Slide 99 text

joind.in/talk/c3f16 @SammyK #phpworld https://en.wikipedia.org/wiki/Singleton_pattern

Slide 100

Slide 100 text

joind.in/talk/c3f16 @SammyK #phpworld

Slide 101

Slide 101 text

joind.in/talk/c3f16 @SammyK #phpworld Singleton •Ensures one & only one instance •Global access (static function) •Initialization on first access

Slide 102

Slide 102 text

joind.in/talk/c3f16 @SammyK #phpworld Fatal error: Call to private App\Application::__construct() src/Application.php

Slide 103

Slide 103 text

joind.in/talk/c3f16 @SammyK #phpworld src/Application.php Global access + instantiate on first use

Slide 104

Slide 104 text

joind.in/talk/c3f16 @SammyK #phpworld Get an instance of Application

Slide 105

Slide 105 text

joind.in/talk/c3f16 @SammyK #phpworld Prevent cloning src/Application.php

Slide 106

Slide 106 text

joind.in/talk/c3f16 @SammyK #phpworld src/Application.php Prevent unserialization

Slide 107

Slide 107 text

joind.in/talk/c3f16 @SammyK #phpworld •Ensures one & only one instance •Global access (static function) •Initialization on first access

Slide 108

Slide 108 text

joind.in/talk/c3f16 @SammyK #phpworld Case study Download CSV & calculate CLI View the report HTTP Endpoint

Slide 109

Slide 109 text

joind.in/talk/c3f16 @SammyK #phpworld We get to make our final bootstrap code!

Slide 110

Slide 110 text

joind.in/talk/c3f16 @SammyK #phpworld public/index.php

Slide 111

Slide 111 text

joind.in/talk/c3f16 @SammyK #phpworld Eh?

Slide 112

Slide 112 text

joind.in/talk/c3f16 @SammyK #phpworld src/Application.php However you register service providers into the container (different DI containers have their own API)

Slide 113

Slide 113 text

joind.in/talk/c3f16 @SammyK #phpworld Why not in constructor?

Slide 114

Slide 114 text

joind.in/talk/c3f16 @SammyK #phpworld src/Application.php

Slide 115

Slide 115 text

joind.in/talk/c3f16 @SammyK #phpworld Install a router $ composer require nikic/fast-route (Choose your poison)

Slide 116

Slide 116 text

joind.in/talk/c3f16 @SammyK #phpworld service provider Make a routing Pimple syntax - mileage my vary

Slide 117

Slide 117 text

joind.in/talk/c3f16 @SammyK #phpworld Register in container src/Application.php

Slide 118

Slide 118 text

joind.in/talk/c3f16 @SammyK #phpworld src/Router.php

Slide 119

Slide 119 text

joind.in/talk/c3f16 @SammyK #phpworld Ugly A.F. src/routes.php

Slide 120

Slide 120 text

joind.in/talk/c3f16 @SammyK #phpworld Different project, same component src/routes.php

Slide 121

Slide 121 text

joind.in/talk/c3f16 @SammyK #phpworld public/index.php run We got bootstrap Runs CLI Runs HTTP

Slide 122

Slide 122 text

joind.in/talk/c3f16 @SammyK #phpworld We got boilerplate src/Router.php src/Console/Runner.php symfony/console nikic/fast-route

Slide 123

Slide 123 text

joind.in/talk/c3f16 @SammyK #phpworld Still not convinced?

Slide 124

Slide 124 text

joind.in/talk/c3f16 @SammyK #phpworld What about Legacy

Slide 125

Slide 125 text

joind.in/talk/c3f16 @SammyK #phpworld Why would you go bare? It’s a skill you can use for legacy apps

Slide 126

Slide 126 text

joind.in/talk/c3f16 @SammyK #phpworld I’ll just rewrite this in x framework…

Slide 127

Slide 127 text

joind.in/talk/c3f16 @SammyK #phpworld Totes samezies except routing

Slide 128

Slide 128 text

joind.in/talk/c3f16 @SammyK #phpworld Route exists? Not found Router Yes No Code 404

Slide 129

Slide 129 text

joind.in/talk/c3f16 @SammyK #phpworld Route exists? Legacy router New router Yes No New code

Slide 130

Slide 130 text

joind.in/talk/c3f16 @SammyK #phpworld app/webroot/index.php Old CakePHP bootstrap file

Slide 131

Slide 131 text

joind.in/talk/c3f16 @SammyK #phpworld src/bootstrap.php Same as public/index.php for HTTP

Slide 132

Slide 132 text

joind.in/talk/c3f16 @SammyK #phpworld src/Router.php

Slide 133

Slide 133 text

joind.in/talk/c3f16 @SammyK #phpworld tests? What about

Slide 134

Slide 134 text

joind.in/talk/c3f16 @SammyK #phpworld Install PHPUnit $ composer require phpunit/phpunit --dev

Slide 135

Slide 135 text

joind.in/talk/c3f16 @SammyK #phpworld phpunit.xml

Slide 136

Slide 136 text

joind.in/talk/c3f16 @SammyK #phpworld tests/bootstrap.php

Slide 137

Slide 137 text

joind.in/talk/c3f16 @SammyK #phpworld src/Application.php

Slide 138

Slide 138 text

joind.in/talk/c3f16 @SammyK #phpworld Run the test suite $ ./vendor/bin/phpunit

Slide 139

Slide 139 text

joind.in/talk/c3f16 @SammyK #phpworld How to choose your components

Slide 140

Slide 140 text

joind.in/talk/c3f16 @SammyK #phpworld Use more than one framework #1

Slide 141

Slide 141 text

joind.in/talk/c3f16 @SammyK #phpworld Search packagist.org #2 (Make sure it has good docs & is actively maintained)

Slide 142

Slide 142 text

joind.in/talk/c3f16 @SammyK #phpworld experiments Try some

Slide 143

Slide 143 text

joind.in/talk/c3f16 @SammyK #phpworld 100% middleware My next experiment: (I could totes use Zend Expressive or just go bare)

Slide 144

Slide 144 text

joind.in/talk/c3f16 @SammyK #phpworld pros? framework ‘member the

Slide 145

Slide 145 text

joind.in/talk/c3f16 @SammyK #phpworld Framework pros Crazy fast to get started …but you’ll get faster at going bare

Slide 146

Slide 146 text

joind.in/talk/c3f16 @SammyK #phpworld Framework pros to maintain …most the time it’s really not that bad No bootstrap & boilerplate code

Slide 147

Slide 147 text

joind.in/talk/c3f16 @SammyK #phpworld Framework pros The code is …so are the components you use when going bare battle tested

Slide 148

Slide 148 text

joind.in/talk/c3f16 @SammyK #phpworld Framework pros Great …so are the components you use when going bare documentation

Slide 149

Slide 149 text

joind.in/talk/c3f16 @SammyK #phpworld Framework pros Makes you more …going bare is a marketable skill marketable

Slide 150

Slide 150 text

joind.in/talk/c3f16 @SammyK #phpworld Framework pros They are often …in many contexts going bare is more pragmatic pragmatic

Slide 151

Slide 151 text

joind.in/talk/c3f16 @SammyK #phpworld myths? Did we bust the

Slide 152

Slide 152 text

joind.in/talk/c3f16 @SammyK #phpworld #1 You must write everything from scratch Nope, use components

Slide 153

Slide 153 text

joind.in/talk/c3f16 @SammyK #phpworld #2 You’re inherently anti- framework or anti-convention Nope, frameworks are great for most greenfield projects

Slide 154

Slide 154 text

joind.in/talk/c3f16 @SammyK #phpworld #3 You’ll spend most of your time on bootstrapping instead of on domain code It takes a little longer at first but you’ll get faster with practice

Slide 155

Slide 155 text

joind.in/talk/c3f16 @SammyK #phpworld #4 You must be an architecture astronaut Going bare is more pragmatic for legacy codebases

Slide 156

Slide 156 text

joind.in/talk/c3f16 @SammyK #phpworld #5 You like pain No, we just like to improve our skills

Slide 157

Slide 157 text

joind.in/talk/c3f16 @SammyK #phpworld

Slide 158

Slide 158 text

joind.in/talk/c3f16 @SammyK #phpworld Remember: It’s all about tradeoffs.

Slide 159

Slide 159 text

joind.in/talk/c3f16 @SammyK #phpworld Resources

Slide 160

Slide 160 text

joind.in/talk/c3f16 @SammyK #phpworld Create a PHP application without a framework Patrick Louys https://github.com/PatrickLouys/no- framework-tutorial

Slide 161

Slide 161 text

joind.in/talk/c3f16 @SammyK #phpworld Refactoring Martin Fowler

Slide 162

Slide 162 text

joind.in/talk/c3f16 @SammyK #phpworld <3 frameworks I I also <3 my skill for Going Bare

Slide 163

Slide 163 text

Sammy Kaye Powers Thanks! /talk/c3f16 @SammyK SammyK.me Host of @PHPRoundtable