Guzzle 5 or 6 or ????

Posted: 2016-11-11 01:37:41

Sometimes Guzzle can be a real pain to use in an library. You choose 5.x and then some other library needed 6.x and heck you really did not care either way.

One interesting package example is aws/aws-sdk-php

If you look in it's composer.json you will see this

    "require": {
        "php": ">=5.5",
        "guzzlehttp/guzzle": "~5.3|~6.0.1|~6.1",
        "guzzlehttp/psr7": "~1.0",
        "guzzlehttp/promises": "~1.0",
        "mtdowling/jmespath.php": "~2.2"

So it allows for other libraries to choose as needed and it will deal with the different versions as needed.


function default_http_handler()
    $version = (string) ClientInterface::VERSION;
    if ($version[0] === '5') {
        return new \Aws\Handler\GuzzleV5\GuzzleHandler();
    } elseif ($version[0] === '6') {
        return new \Aws\Handler\GuzzleV6\GuzzleHandler();
    } else {
        throw new \RuntimeException('Unknown Guzzle version: ' . $version);

Then each class deals with instantiating the GuzzleHttp\Client as needed.


php note2self