Slide 1

Slide 1 text

Package development Laracon EU try-out

Slide 2

Slide 2 text

Hi, my name is Hannes.

Slide 3

Slide 3 text

.be madewithlove

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Package development Intermediate and advanced techniques

Slide 6

Slide 6 text

1. Beginner steps 2. Intermediate tips 3. Decouple for stability

Slide 7

Slide 7 text

1. Beginner steps Everyone started out as a newbie.

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

BEGINNER STEPS http:/ /phppackagechecklist.com/

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

BEGINNER STEPS No renaming keep the old one, but create a new

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

BEGINNER STEPS Instructions to install composer require guzzlehttp/guzzle

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

BEGINNER STEPS - VERSIONS /** * Register method. */ public function register() { // Register SDK class. $this->app->singleton('twilio', function () { $config = $this->app['config']; return new Sdk($config['services.twilio']); }); }

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

BEGINNER STEPS - CODE STYLE Use of tooling do it early

Slide 21

Slide 21 text

BEGINNER STEPS - CODE STYLE Recommendations: PSR-2, Symfony

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

BEGINNER STEPS - CODE STYLE $container = new Container; $container = new Container(); $options = [ 'ssl' => true, 'redirects' => false, ];

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

BEGINNER STEPS - TESTS/SPECS Cover your ass multiple times

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

BEGINNER STEPS semver.org release often

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

BEGINNER STEPS - CHANGELOG Document releases keepachangelog.com

Slide 31

Slide 31 text

BEGINNER STEPS - CHANGELOG # Change Log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased][unreleased] ### Changed - Improve argument against commit logs. ## [0.0.8] - 2015-02-17 ### Changed - Update year to match in every README example. - Reluctantly stop making fun of Brits only, since most of the world writes dates in a strange way. ### Fixed - Fix typos in recent README changes. - Update outdated unreleased diff link.

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

BEGINNER STEPS GitHub PC test server packagist

Slide 35

Slide 35 text

BEGINNER STEPS GitHub PC test server packagist git push

Slide 36

Slide 36 text

BEGINNER STEPS GitHub PC test server packagist webhook webhook

Slide 37

Slide 37 text

BEGINNER STEPS GitHub PC test server packagist analyses composer.json /stores version runs tests

Slide 38

Slide 38 text

BEGINNER STEPS GitHub PC test server packagist green!

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

BEGINNER STEPS - README.MD Code samples copy-paste

Slide 41

Slide 41 text

BEGINNER STEPS - README.MD // open an image file $img = Image::make('public/foo.jpg'); // resize image instance $img->resize(320, 240); // insert a watermark $img->insert('public/watermark.png'); // save image in desired format $img->save('public/bar.jpg');

Slide 42

Slide 42 text

BEGINNER STEPS - README.MD Badges stable version - code coverage - …

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

BEGINNER STEPS No license is the worst

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

BEGINNER STEPS - CONTRIBUTING Instructions how to test how to make PR philosophy

Slide 47

Slide 47 text

BEGINNER STEPS - HINT 1 jonathantorres/construct

Slide 48

Slide 48 text

BEGINNER STEPS - HINT 1 $ construct generate --help Arguments: name The vendor/project name Options: --test (-t) Testing framework (default: "phpunit") --license (-l) License (default: "MIT") --namespace (-s) Namespace for project (default: "Vendor\\Pro --git (-g) Initialize an empty Git repo --phpcs (-p) Generate a PHP Coding Standards Fixer config --keywords (-k) Comma separated list of Composer keywords --vagrant Generate a Vagrantfile

Slide 49

Slide 49 text

BEGINNER STEPS - HINT 2 franzl/studio

Slide 50

Slide 50 text

BEGINNER STEPS - HINT 2 $ studio load --help Usage: load Arguments: path The path where the package files are located

Slide 51

Slide 51 text

2. Intermediate tips

Slide 52

Slide 52 text

INTERMEDIATE TIPS - ENVIRONMENTS A package has environments too

Slide 53

Slide 53 text

INTERMEDIATE TIPS - ENVIRONMENTS dev ci production

Slide 54

Slide 54 text

INTERMEDIATE TIPS - ENVIRONMENTS - DEV & CI not in production: require-dev

Slide 55

Slide 55 text

INTERMEDIATE TIPS - ENVIRONMENTS - CI code coverage reporting

Slide 56

Slide 56 text

INTERMEDIATE TIPS - ENVIRONMENTS - CI phpunit --coverage-html=/tmp/coverage/; vs phpunit --coverage-clover=/tmp/coverage.xml; ocular code-coverage:upload /tmp/coverage.xml;

Slide 57

Slide 57 text

INTERMEDIATE TIPS - ENVIRONMENTS - PROD Keep your tests out of other people’s production

Slide 58

Slide 58 text

INTERMEDIATE TIPS - CLEAN DISTS prefer-dist

Slide 59

Slide 59 text

INTERMEDIATE TIPS - CLEAN DISTS .gitattributes file

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

INTERMEDIATE TIPS - CLEAN DISTS autoload-dev

Slide 62

Slide 62 text

INTERMEDIATE TIPS - CLEAN DISTS "autoload": { "psr-4": { "GuzzleHttp\\": "src/" } }, "autoload-dev": { "psr-4": { "GuzzleHttp\\Stubs\\": "tests/stubs/" } }

Slide 63

Slide 63 text

INTERMEDIATE TIPS Composer scripts stop typing vendor/bin/

Slide 64

Slide 64 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS "scripts": { "test": ["phpunit", "phpspec run"], "cs": "php-cs-fixer fix" } $ composer cs

Slide 65

Slide 65 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS #!/usr/bin/env php

Slide 66

Slide 66 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS "bin": ["construct"] vendor/bin/construct

Slide 67

Slide 67 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS not in ci: require --global

Slide 68

Slide 68 text

INTERMEDIATE TIPS Code Style

Slide 69

Slide 69 text

INTERMEDIATE TIPS - CODE STYLE Atomic commits easier to review w/o code style fixes

Slide 70

Slide 70 text

INTERMEDIATE TIPS - CODE STYLE Recommendation: CS options for diffs

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

INTERMEDIATE TIPS - RECAP - separate environments - composer scripts - code style

Slide 75

Slide 75 text

3. Decouple for stability

Slide 76

Slide 76 text

DECOUPLING Imagine… You’re creating an SDK

Slide 77

Slide 77 text

DECOUPLING HTTP

Slide 78

Slide 78 text

DECOUPLING - HTTP EXAMPLE Roll your own when?

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

DECOUPLING - HTTP EXAMPLE Better: Use HTTP Client

Slide 84

Slide 84 text

DECOUPLING - HTTP EXAMPLE Better: Use Guzzle v6

Slide 85

Slide 85 text

DECOUPLING - HTTP EXAMPLE “But I’m already using Buzz HTTP client”

Slide 86

Slide 86 text

DECOUPLING - HTTP EXAMPLE “But I’m already using Guzzle v4 instead of v6”

Slide 87

Slide 87 text

DECOUPLING - HTTP EXAMPLE Decouple from the implementation

Slide 88

Slide 88 text

DECOUPLING - HTTP EXAMPLE php-http/adapter-interface

Slide 89

Slide 89 text

DECOUPLING - HTTP EXAMPLE Uses PSR-7 HTTP Message Interfaces

Slide 90

Slide 90 text

DECOUPLING - HTTP EXAMPLE /** * @param \Psr\Http\Message\RequestInterface $request * @return \Psr\Http\Message\ResponseInterface */ private function send(RequestInterface $request) { return $this->adapter->send($request); }

Slide 91

Slide 91 text

DECOUPLING - HTTP EXAMPLE /** * @param \Http\Adapter\HttpAdapter $adapter */ public function __construct(HttpAdapter $adapter) { $this->adapter = $adapter; }

Slide 92

Slide 92 text

DECOUPLING - HTTP EXAMPLE (optional) $client = new \Http\Adapter\Client($adapter) $psrResponse = $client->get($url);

Slide 93

Slide 93 text

DECOUPLING - HTTP EXAMPLE Adapters: Guzzle5 Guzzle6 Mock Buzz Zend2 Zend1 Cake

Slide 94

Slide 94 text

DECOUPLING - HTTP EXAMPLE Used by friendsofsymfony/http-cache

Slide 95

Slide 95 text

DECOUPLING - HTTP EXAMPLE egeloen/http-adapter Used by geocoder-php/geocoder florianv/swap lstrojny/fxmlrpc

Slide 96

Slide 96 text

DECOUPLING - HTTP EXAMPLE Could be used by league/oauth2-client toin0u/digitalocean-v2 …

Slide 97

Slide 97 text

DECOUPLING - OTHER CASES - HTTP - Logging - File system

Slide 98

Slide 98 text

DECOUPLING - OTHER CASES Sadly not for caching

Slide 99

Slide 99 text

DECOUPLING - INTERFACES Decoupling requires interfaces

Slide 100

Slide 100 text

DECOUPLING - INTERFACES interfaces have implementations

Slide 101

Slide 101 text

DECOUPLING - INTERFACES concept virtual package is a placeholder to be able to define a dependency on some implementation

Slide 102

Slide 102 text

DECOUPLING - INTERFACES company/sdk requires (1.0.0) company/sdk - composer.json php-http/adapter-implementation

Slide 103

Slide 103 text

DECOUPLING - INTERFACES php-http/adapter php-http/adapter-implementation provides (1.0.0) php-http/guzzle6-adapter requires (1.0.0) php-http/guzzle6-adapter - composer.json guzzlehttp/guzzle requires (^6.0)

Slide 104

Slide 104 text

DECOUPLING - INTERFACES application - composer.json company/sdk requires (x.y.z) application

Slide 105

Slide 105 text

DECOUPLING - INTERFACES php-http/guzzle6-adapter application - composer.json company/sdk requires (x.y.z) application requires (1.0.0)

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

DECOUPLING - RECAP Prefer requirements on stable packages

Slide 108

Slide 108 text

DECOUPLING - RECAP Prefer requirements on virtual packages

Slide 109

Slide 109 text

DECOUPLING - BENEFITS Added benefits As package maintainer: more stable deps

Slide 110

Slide 110 text

DECOUPLING - BENEFITS Added benefits let people use implementations they want

Slide 111

Slide 111 text

DECOUPLING - BENEFITS Added benefits No lock-in to a major version

Slide 112

Slide 112 text

DECOUPLING - RECAP - Single responsibility - Try to decouple from implementation - Depend on virtual packages

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

RECAP 1. Beginner steps 2. Intermediate tips 3. Decouple for stability

Slide 115

Slide 115 text

1 November 2015

Slide 116

Slide 116 text

gofundme.com/tcsnycmarathon

Slide 117

Slide 117 text

Thank you! @hannesvdvreken

Slide 118

Slide 118 text

Time for questions. @hannesvdvreken

Slide 119

Slide 119 text

• http:/ /mwl.be REFERENCES