Pro Yearly is on sale from $80 to $50! »

psr-7-packages

 psr-7-packages

A talk about packages that use the common request/response interfaces of PSR-7. They showcase the force of interoperable interfaces.

Demo code for this talk can be found here: https://github.com/hannesvdvreken/psr7-demo

Presented at php.gent for the first time on June 30th 2015.

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

June 30, 2015
Tweet

Transcript

  1. Highlighted packages packagist’s finest selection of libraries

  2. Hi, my name is Hannes.

  3. madewithlove

  4. MongoDB Belgium

  5. None
  6. Highlighted packages

  7. 1. guzzlehttp/guzzle 2. php-http/adapter

  8. 1. guzzlehttp/guzzle

  9. GUZZLEHTTP/GUZZLE composer require guzzlehttp/guzzle

  10. GUZZLEHTTP/GUZZLE ^6.0

  11. GUZZLEHTTP/GUZZLE semver.mwl.be

  12. None
  13. GUZZLEHTTP/GUZZLE What’s new?

  14. GUZZLEHTTP/GUZZLE - WHAT’S NEW PSR-7

  15. GUZZLEHTTP/GUZZLE - PSR-7 Psr\Http\Message\RequestInterface Psr\Http\Message\ResponseInterface

  16. GUZZLEHTTP/GUZZLE - PSR-7 use GuzzleHttp\Psr7\Request; $psrRequest = new Request( 'GET',

    $uri, $headers, $body );
  17. GUZZLEHTTP/GUZZLE - PSR-7 $client = new GuzzleHttp\Client(); $client->send($psrRequest);

  18. GUZZLEHTTP/GUZZLE - PSR-7 "require": { "php": ">=5.5.0", "guzzlehttp/psr7": "~1.1", }

  19. GUZZLEHTTP/GUZZLE - PSR-7 Differences with Symfony objects: Implementations for interfaces

    Immutable
  20. GUZZLEHTTP/GUZZLE - PSR-7 $newRequest = $request ->withHeader($name, $value); var_dump($newRequest ===

    $request); > false
  21. GUZZLEHTTP/GUZZLE - PSR-7 Factories to convert $psrRequest ⁶ $symfonyRequest $psrResponse

    ⁶ $symfonyResponse
  22. GUZZLEHTTP/GUZZLE - PSR-7 Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/1-guzzle

  23. GUZZLEHTTP/GUZZLE - WHAT’S NEW What else?

  24. GUZZLEHTTP/GUZZLE - PROMISES Promises

  25. GUZZLEHTTP/GUZZLE - PROMISES Not so new: (>=5.0.0) Promises

  26. GUZZLEHTTP/GUZZLE - PROMISES "require": { "php": ">=5.5.0", "guzzlehttp/psr7": "~1.1", "guzzlehttp/promise":

    "~1.0" }
  27. GUZZLEHTTP/GUZZLE - PROMISES $promise = $client->sendAsync($request); $promise->then(function ($response) { $response->getBody();

    }); $promise->wait();
  28. GUZZLEHTTP/GUZZLE - PROMISES Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/2-promise https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/3-multi

  29. GUZZLEHTTP/GUZZLE - WHAT’S NEW What else?

  30. GUZZLEHTTP/GUZZLE - EVENTING Previously (<4.0.0) symfony/event-dispatcher Later (>=4.0.0, <6.0.0) GuzzleHttp\Event\EmitterInterface

    GuzzleHttp\Event\SubscriberInterface
  31. GUZZLEHTTP/GUZZLE - MIDDLEWARES Now (>=6.0.0) Callable middlewares

  32. GUZZLEHTTP/GUZZLE - MIDDLEWARES $stack = HandlerStack::create(); $client = new Client(['handler'

    => $stack]); $stack->push($middleware); $stack->unshift($middleware);
  33. GUZZLEHTTP/GUZZLE - MIDDLEWARES $middleware = function ($handler) { return function

    ($request, $options) use ($handler) { // Do before return $handler($request, $options) ->then(function () { // Do after }); }; }
  34. GUZZLEHTTP/GUZZLE - MIDDLEWARES $middleware = GuzzleHttp\Middleware::log( $psrLogger, new GuzzleHttp\MessageFormatter() );

  35. GUZZLEHTTP/GUZZLE - MIDDLEWARES Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/4-middleware

  36. GUZZLEHTTP/GUZZLE - WHAT ELSE What else?

  37. GUZZLEHTTP/GUZZLE - PROGRESS $request->getEmitter()->on( 'progress', function (ProgressEvent $event) { …

    } );
  38. GUZZLEHTTP/GUZZLE - PROGRESS Is gone! (>=5.0.0, <6.0.0)

  39. GUZZLEHTTP/GUZZLE - PROGRESS No progress on promise? Too bad!

  40. GUZZLEHTTP/GUZZLE - PROGRESS use Symfony\Component\Console \Helper\ProgressBar;

  41. GUZZLEHTTP/GUZZLE - PROGRESS 10104/455642 [======>---------------------] 22% 14 sec/59 sec 1.4

    MB
  42. GUZZLEHTTP/GUZZLE - PROGRESS 15214/455642 [=========>------------------] 33% 21 sec/58 sec 2.1

    MB
  43. GUZZLEHTTP/GUZZLE - PROGRESS 20325/455642 [============>---------------] 43% 28 sec/59 sec 3.0

    MB
  44. GUZZLEHTTP/GUZZLE - PROGRESS Roll your own

  45. GUZZLEHTTP/GUZZLE - PROGRESS $response = $client->get($uri); // Psr\Http\Message\StreamInterface $stream =

    $response->getBody(); $stream->getSize(); $stream->eof(); $stream->read(1024);
  46. 2. php-http/adapter

  47. PHP-HTTP/ADAPTER Imagine you’re writing an SDK for GitHub/Slack/Hipchat/DO/…

  48. PHP-HTTP/ADAPTER composer require guzzlehttp/guzzle

  49. None
  50. PHP-HTTP/ADAPTER - :CRY: “I already use Zend’s HTTP client! ”

  51. PHP-HTTP/ADAPTER - :CRY: “I can’t install Guzzle v5 ánd v6!

  52. PHP-HTTP/ADAPTER - SOLUTION Decouple from the HTTP client

  53. None
  54. PHP-HTTP/ADAPTER - SOLUTION $adapter = new Adapter($guzzleClient); $psrRes = $adapter->sendRequest($psrReq);

    $client = new Client($adapter); $psrRes = $client->get($url);
  55. PHP-HTTP/ADAPTER - SOLUTION public function __construct( Http\Adapter\HttpAdapter $adapter ) {

    $this->adapter = $adapter; } public function __construct( Http\Client\HttpClient $client ) { $this->client = $client; }
  56. PHP-HTTP/ADAPTER - SOLUTION "require": { "php-http/adapter": "^0.1.0", "php-http/adapter-client": "^0.1.0",
 "php-http/adapter-implementation":

    "^0.1.0" }
  57. PHP-HTTP/ADAPTER - SOLUTION Virtual package "provide": {
 "php-http/adapter-implementation": "^0.1.0" }

  58. None
  59. PHP-HTTP/ADAPTER - ADAPTERS guzzle6-adapter guzzle5-adapter guzzle4-adapter guzzle3-adapter zend2-adapter zend1-adapter cake-adapter

  60. PHP-HTTP/ADAPTER - ADAPTERS buzz-adapter react-adapter socket-adapter fopen-adapter file-get-contents-adapter native-curl-adapter pecl-adapter

  61. PHP-HTTP/ADAPTER - COMPOSER.JSON "require": { "guzzlehttp/guzzle": "^5.0", "your-awesome/sdk": "^1.0",
 "php-http/guzzle5-adapter":

    "^0.1.0" }
  62. PHP-HTTP/ADAPTER - COMPOSER.JSON "require": { - "guzzlehttp/guzzle": "^5.0", + "guzzlehttp/guzzle":

    "^6.0", "your-awesome/sdk": "^1.0",
 - "php-http/guzzle5-adapter": "^0.1.0" + "php-http/guzzle6-adapter": "^0.1.0" }
  63. None
  64. PHP-HTTP/ADAPTER - DEMO Demo https:/ /github.com/hannesvdvreken/psr7-demo/tree/master/5-github

  65. RECAP 1. guzzlehttp/guzzle 2. php-http/adapter

  66. Questions?

  67. Thank you! @hannesvdvreken

  68. • http:/ /mwl.be • https:/ /github.com/guzzle/guzzle • https:/ /github.com/php-http •

    http:/ /docs.guzzlephp.org/en/latest/ • https:/ /github.com/php-fig/http-message • https:/ /github.com/php-fig/fig-standards/
 blob/master/accepted/PSR-7-http-message.md REFERENCES