Slide 1

Slide 1 text

Going Bare Writing the web without a framework Sammy Kaye Powers 2019-05-03

Slide 2

Slide 2 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Slides Get the joind.in/talk/4b6f9

Slide 3

Slide 3 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP frameworks The point is not <3 frameworks! I

Slide 4

Slide 4 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP opinion It’s OK to have an

Slide 5

Slide 5 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP experiment It’s OK to

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Frameworks are a tool

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP 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 11

Slide 11 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Myths about going frameworkless

Slide 12

Slide 12 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Myth #1 You must write everything from scratch

Slide 13

Slide 13 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Myth #2 You’re inherently anti-framework or anti-convention

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Myth #5 You like pain

Slide 17

Slide 17 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros Framework Cons

Slide 18

Slide 18 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros

Slide 19

Slide 19 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros

Slide 20

Slide 20 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros crazy fast You get started

Slide 21

Slide 21 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros First-time install time minutes! 5

Slide 22

Slide 22 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros battle-tested The code is

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros documentation Most frameworks have great

Slide 28

Slide 28 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros marketable It makes you more

Slide 29

Slide 29 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Pros pragmatic They are often

Slide 30

Slide 30 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP cons? What about the

Slide 31

Slide 31 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Cons legacy codebases Useless for

Slide 32

Slide 32 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Cons cruft They fill your project with

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Cons tightly couple Easier to due to convenience

Slide 35

Slide 35 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 36

Slide 36 text

5.3

Slide 37

Slide 37 text

5.7 2018-09-04

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Cons upgrading Framework-specific deps can keep you from

Slide 43

Slide 43 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP The The Cons Pros seem great are weak…

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP When should you go bare?

Slide 46

Slide 46 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 47

Slide 47 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Greenfield Legacy Depends

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP How do you go bare?

Slide 50

Slide 50 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP from scratch You could just write

Slide 51

Slide 51 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP reinventing the wheel But that’s like

Slide 54

Slide 54 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Better Way™ There’s a

Slide 55

Slide 55 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP po- nents Com-

Slide 56

Slide 56 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP components has a healthy ecosystem of

Slide 57

Slide 57 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP using them You’re probably already

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP I like smarties

Slide 61

Slide 61 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Router DBAL/ORM Dep Injection Logger

Slide 62

Slide 62 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP zend-router fast-route php-router phroute Routers Tons of variety

Slide 63

Slide 63 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Easily installed with

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP I don’t like licorice

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP examples Real-world

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Greenfield Let’s start with

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP coding domain in 5 mins Install framework &

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP go micro We could… Slim Silex (et al)

Slide 76

Slide 76 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Silex DEAD!

Slide 77

Slide 77 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP go bare! :) But let’s

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP 1. Make an empty composer.json or… $ composer init

Slide 80

Slide 80 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP 2. Make a folder for domain code $ mkdir src

Slide 81

Slide 81 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP 3. Add autoloading $ composer \ dumpautoload And then run…

Slide 82

Slide 82 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Now just install your components & tie them together

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/Console/Runner.php

Slide 87

Slide 87 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP We get to make our first bootstrap code!

Slide 88

Slide 88 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP run (sans .php extension) $ chmod +x run

Slide 89

Slide 89 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP $ ./run

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/Console/CalculateReportsCommand.php

Slide 92

Slide 92 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP $ ./run

Slide 93

Slide 93 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 94

Slide 94 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Congrats!

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Need to make singleton ready src/Application.php

Slide 97

Slide 97 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Singleton Isn’t that an anti-pattern?

Slide 98

Slide 98 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP https://en.wikipedia.org/wiki/Singleton_pattern

Slide 99

Slide 99 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/Application.php Global access + instantiate on first use

Slide 103

Slide 103 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Get an instance of Application

Slide 104

Slide 104 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Prevent cloning src/Application.php

Slide 105

Slide 105 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/Application.php Prevent unserialization

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP We get to make our final bootstrap code!

Slide 109

Slide 109 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP public/index.php

Slide 110

Slide 110 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Eh?

Slide 111

Slide 111 text

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

Slide 112

Slide 112 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Why not in constructor?

Slide 113

Slide 113 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/Application.php

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP service provider Make a routing Pimple syntax - mileage my vary

Slide 116

Slide 116 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Register in container src/Application.php

Slide 117

Slide 117 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/Router.php

Slide 118

Slide 118 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Ugly A.F. src/routes.php

Slide 119

Slide 119 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Different project, same component src/routes.php

Slide 120

Slide 120 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP public/index.php run We got bootstrap Runs CLI Runs HTTP

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Still not convinced?

Slide 123

Slide 123 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP What about Legacy

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP I’ll just rewrite this in x framework…

Slide 126

Slide 126 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 127

Slide 127 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 128

Slide 128 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP $ git add .

Slide 129

Slide 129 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP $ git commit -m \ “Stuff & things”

Slide 130

Slide 130 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP $ git push \ origin HEAD

Slide 131

Slide 131 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP $ git push \ production \ master

Slide 132

Slide 132 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 133

Slide 133 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 134

Slide 134 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 135

Slide 135 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 136

Slide 136 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 137

Slide 137 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Legacy

Slide 138

Slide 138 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Greenfield

Slide 139

Slide 139 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP I’ll just rewrite this in x framework…

Slide 140

Slide 140 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP We treat Legacy Greenfield & Totes samezies *except routing

Slide 141

Slide 141 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Route exists? Not found Router Yes No Code 404

Slide 142

Slide 142 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Route exists? Legacy router New router Yes No New code

Slide 143

Slide 143 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP app/webroot/index.php Old CakePHP bootstrap file

Slide 144

Slide 144 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/bootstrap.php Same as public/index.php for HTTP

Slide 145

Slide 145 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/Router.php

Slide 146

Slide 146 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP tests? What about

Slide 147

Slide 147 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Install PHPUnit $ composer require phpunit/phpunit --dev

Slide 148

Slide 148 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP phpunit.xml

Slide 149

Slide 149 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP tests/bootstrap.php

Slide 150

Slide 150 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP src/Application.php

Slide 151

Slide 151 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Run the test suite $ ./vendor/bin/phpunit

Slide 152

Slide 152 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP How to choose your components

Slide 153

Slide 153 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Use more than one framework #1

Slide 154

Slide 154 text

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

Slide 155

Slide 155 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP experiments Try some

Slide 156

Slide 156 text

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

Slide 157

Slide 157 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP pros? framework ‘member the

Slide 158

Slide 158 text

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

Slide 159

Slide 159 text

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

Slide 160

Slide 160 text

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

Slide 161

Slide 161 text

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

Slide 162

Slide 162 text

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

Slide 163

Slide 163 text

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

Slide 164

Slide 164 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP myths? Did we bust the

Slide 165

Slide 165 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP #1 You must write everything from scratch Nope, use components

Slide 166

Slide 166 text

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

Slide 167

Slide 167 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP #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 168

Slide 168 text

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

Slide 169

Slide 169 text

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

Slide 170

Slide 170 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP

Slide 171

Slide 171 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Remember: It’s all about tradeoffs.

Slide 172

Slide 172 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Resources

Slide 173

Slide 173 text

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

Slide 174

Slide 174 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP Refactoring Martin Fowler

Slide 175

Slide 175 text

joind.in/talk/4b6f9 @SammyK #LonghornPHP <3 frameworks I I also <3 my skill for Going Bare

Slide 176

Slide 176 text

Sammy Kaye Powers Thanks! /talk/4b6f9 @SammyK SammyK.me Host of @PHPRoundtable Open source @DatadogHQ