Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Package Development PHP Antwerp

Package Development PHP Antwerp

Intermediate and advanced tips for PHP package development. Laracon EU try-out.

Rates on https://joind.in/14996

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

August 18, 2015
Tweet

Transcript

  1. Package development Laracon EU try-out

  2. Hi, my name is Hannes.

  3. .be madewithlove

  4. None
  5. Package development Intermediate and advanced techniques

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

  7. 1. Beginner steps Everyone started out as a newbie.

  8. None
  9. BEGINNER STEPS http:/ /phppackagechecklist.com/

  10. None
  11. None
  12. BEGINNER STEPS No renaming keep the old one, but create

    a new
  13. None
  14. None
  15. None
  16. BEGINNER STEPS Instructions to install composer require guzzlehttp/guzzle

  17. None
  18. 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']); }); }
  19. None
  20. BEGINNER STEPS - CODE STYLE Use of tooling do it

    early
  21. BEGINNER STEPS - CODE STYLE Recommendations: PSR-2, Symfony

  22. None
  23. BEGINNER STEPS - CODE STYLE $container = new Container; $container

    = new Container(); $options = [ 'ssl' => true, 'redirects' => false, ];
  24. None
  25. BEGINNER STEPS - TESTS/SPECS Cover your ass multiple times

  26. None
  27. None
  28. BEGINNER STEPS semver.org release often

  29. None
  30. BEGINNER STEPS - CHANGELOG Document releases keepachangelog.com

  31. 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.
  32. None
  33. None
  34. BEGINNER STEPS GitHub PC test server packagist

  35. BEGINNER STEPS GitHub PC test server packagist git push

  36. BEGINNER STEPS GitHub PC test server packagist webhook webhook

  37. BEGINNER STEPS GitHub PC test server packagist analyses composer.json /stores

    version runs tests
  38. BEGINNER STEPS GitHub PC test server packagist green!

  39. None
  40. BEGINNER STEPS - README.MD Code samples copy-paste

  41. 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');
  42. BEGINNER STEPS - README.MD Badges stable version - code coverage

    - …
  43. None
  44. BEGINNER STEPS No license is the worst

  45. None
  46. BEGINNER STEPS - CONTRIBUTING Instructions how to test how to

    make PR philosophy
  47. BEGINNER STEPS - HINT 1 jonathantorres/construct

  48. 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
  49. BEGINNER STEPS - HINT 2 franzl/studio

  50. BEGINNER STEPS - HINT 2 $ studio load --help Usage:

    load <path> Arguments: path The path where the package files are located
  51. 2. Intermediate tips

  52. INTERMEDIATE TIPS - ENVIRONMENTS A package has environments too

  53. INTERMEDIATE TIPS - ENVIRONMENTS dev ci production

  54. INTERMEDIATE TIPS - ENVIRONMENTS - DEV & CI not in

    production: require-dev
  55. INTERMEDIATE TIPS - ENVIRONMENTS - CI code coverage reporting

  56. INTERMEDIATE TIPS - ENVIRONMENTS - CI phpunit --coverage-html=/tmp/coverage/; vs phpunit

    --coverage-clover=/tmp/coverage.xml; ocular code-coverage:upload /tmp/coverage.xml;
  57. INTERMEDIATE TIPS - ENVIRONMENTS - PROD Keep your tests out

    of other people’s production
  58. INTERMEDIATE TIPS - CLEAN DISTS prefer-dist

  59. INTERMEDIATE TIPS - CLEAN DISTS .gitattributes file

  60. None
  61. INTERMEDIATE TIPS - CLEAN DISTS autoload-dev

  62. INTERMEDIATE TIPS - CLEAN DISTS "autoload": { "psr-4": { "GuzzleHttp\\":

    "src/" } }, "autoload-dev": { "psr-4": { "GuzzleHttp\\Stubs\\": "tests/stubs/" } }
  63. INTERMEDIATE TIPS Composer scripts stop typing vendor/bin/

  64. INTERMEDIATE TIPS - COMPOSER SCRIPTS "scripts": { "test": ["phpunit", "phpspec

    run"], "cs": "php-cs-fixer fix" } $ composer cs
  65. INTERMEDIATE TIPS - COMPOSER SCRIPTS #!/usr/bin/env php <?php

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

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

  68. INTERMEDIATE TIPS Code Style

  69. INTERMEDIATE TIPS - CODE STYLE Atomic commits easier to review

    w/o code style fixes
  70. INTERMEDIATE TIPS - CODE STYLE Recommendation: CS options for diffs

  71. None
  72. None
  73. None
  74. INTERMEDIATE TIPS - RECAP - separate environments - composer scripts

    - code style
  75. 3. Decouple for stability

  76. DECOUPLING Imagine… You’re creating an SDK

  77. DECOUPLING HTTP

  78. DECOUPLING - HTTP EXAMPLE Roll your own when?

  79. None
  80. None
  81. None
  82. None
  83. DECOUPLING - HTTP EXAMPLE Better: Use HTTP Client

  84. DECOUPLING - HTTP EXAMPLE Better: Use Guzzle v6

  85. DECOUPLING - HTTP EXAMPLE “But I’m already using Buzz HTTP

    client”
  86. DECOUPLING - HTTP EXAMPLE “But I’m already using Guzzle v4

    instead of v6”
  87. DECOUPLING - HTTP EXAMPLE Decouple from the implementation

  88. DECOUPLING - HTTP EXAMPLE php-http/adapter-interface

  89. DECOUPLING - HTTP EXAMPLE Uses PSR-7 HTTP Message Interfaces

  90. DECOUPLING - HTTP EXAMPLE /** * @param \Psr\Http\Message\RequestInterface $request *

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

    public function __construct(HttpAdapter $adapter) { $this->adapter = $adapter; }
  92. DECOUPLING - HTTP EXAMPLE (optional) $client = new \Http\Adapter\Client($adapter) $psrResponse

    = $client->get($url);
  93. DECOUPLING - HTTP EXAMPLE Adapters: Guzzle5 Guzzle6 Mock Buzz Zend2

    Zend1 Cake
  94. DECOUPLING - HTTP EXAMPLE Used by friendsofsymfony/http-cache

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

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

  97. DECOUPLING - OTHER CASES - HTTP - Logging - File

    system
  98. DECOUPLING - OTHER CASES Sadly not for caching

  99. DECOUPLING - INTERFACES Decoupling requires interfaces

  100. DECOUPLING - INTERFACES interfaces have implementations

  101. DECOUPLING - INTERFACES concept virtual package is a placeholder to

    be able to define a dependency on some implementation
  102. DECOUPLING - INTERFACES company/sdk requires (1.0.0) company/sdk - composer.json php-http/adapter-implementation

  103. 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)
  104. DECOUPLING - INTERFACES application - composer.json company/sdk requires (x.y.z) application

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

    application requires (1.0.0)
  106. None
  107. DECOUPLING - RECAP Prefer requirements on stable packages

  108. DECOUPLING - RECAP Prefer requirements on virtual packages

  109. DECOUPLING - BENEFITS Added benefits As package maintainer: more stable

    deps
  110. DECOUPLING - BENEFITS Added benefits let people use implementations they

    want
  111. DECOUPLING - BENEFITS Added benefits No lock-in to a major

    version
  112. DECOUPLING - RECAP - Single responsibility - Try to decouple

    from implementation - Depend on virtual packages
  113. None
  114. RECAP 1. Beginner steps 2. Intermediate tips 3. Decouple for

    stability
  115. 1 November 2015

  116. gofundme.com/tcsnycmarathon

  117. Thank you! @hannesvdvreken

  118. Time for questions. @hannesvdvreken

  119. • http:/ /mwl.be REFERENCES