|
| 1 | +# Guzzle Commands |
| 2 | + |
| 3 | +[](https://packagist.org/packages/guzzlehttp/command) |
| 4 | +[](https://travis-ci.org/guzzle/command) |
| 5 | +[](https://scrutinizer-ci.com/g/guzzle/command/?branch=master) |
| 6 | +[](https://scrutinizer-ci.com/g/guzzle/command/?branch=master) |
| 7 | +[](https://insight.sensiolabs.com/projects/7a93338e-50cd-42f7-9299-17c44d92148f) |
| 8 | +[](https://packagist.org/packages/guzzlehttp/command) |
| 9 | +[](https://packagist.org/packages/guzzlehttp/command) |
| 10 | +[](https://packagist.org/packages/guzzlehttp/command) |
| 11 | + |
| 12 | +This library uses Guzzle (``guzzlehttp/guzzle``, version 6.x) and provides the |
| 13 | +foundations to create fully-featured web service clients by abstracting Guzzle |
| 14 | +HTTP **requests** and **responses** into higher-level **commands** and |
| 15 | +**results**. A **middleware** system, analogous to — but separate from — the one |
| 16 | +in the HTTP layer may be used to customize client behavior when preparing |
| 17 | +commands into requests and processing responses into results. |
| 18 | + |
| 19 | +### Commands |
| 20 | + |
| 21 | +Key-value pair objects representing an operation of a web service. Commands have a name and a set of parameters. |
| 22 | + |
| 23 | +### Results |
| 24 | + |
| 25 | +Key-value pair objects representing the processed result of executing an operation of a web service. |
| 26 | + |
| 27 | +## Installing |
| 28 | + |
| 29 | +This project can be installed using Composer: |
| 30 | + |
| 31 | +``composer require guzzlehttp/command`` |
| 32 | + |
| 33 | +For **Guzzle 5**, use ``composer require guzzlehttp/command:0.8.*``. The source |
| 34 | +code for the Guzzle 5 version is available on the |
| 35 | +`0.8 branch <https://github.com/guzzle/command/tree/0.8>`_. |
| 36 | + |
| 37 | +**Note:** If Composer is not |
| 38 | +`installed globally <https://getcomposer.org/doc/00-intro.md#globally>`_, |
| 39 | +then you may need to run the preceding Composer commands using |
| 40 | +``php composer.phar`` (where ``composer.phar`` is the path to your copy of |
| 41 | +Composer), instead of just ``composer``. |
| 42 | + |
| 43 | +## Service Clients |
| 44 | + |
| 45 | +Service Clients are web service clients that implement the |
| 46 | +``GuzzleHttp\Command\ServiceClientInterface`` and use an underlying Guzzle HTTP |
| 47 | +client (``GuzzleHttp\Client``) to communicate with the service. Service clients |
| 48 | +create and execute **commands** (``GuzzleHttp\Command\CommandInterface``), |
| 49 | +which encapsulate operations within the web service, including the operation |
| 50 | +name and parameters. This library provides a generic implementation of a service |
| 51 | +client: the ``GuzzleHttp\Command\ServiceClient`` class. |
| 52 | + |
| 53 | +## Instantiating a Service Client |
| 54 | + |
| 55 | +@TODO Add documentation |
| 56 | + |
| 57 | +* ``ServiceClient``'s constructor |
| 58 | +* Transformer functions (``$commandToRequestTransformer`` and ``$responseToResultTransformer``) |
| 59 | +* The ``HandlerStack`` |
| 60 | + |
| 61 | +## Executing Commands |
| 62 | + |
| 63 | +Service clients create command objects using the ``getCommand()`` method. |
| 64 | + |
| 65 | +```php |
| 66 | +$commandName = 'foo'; |
| 67 | +$arguments = ['baz' => 'bar']; |
| 68 | +$command = $client->getCommand($commandName, $arguments); |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +After creating a command, you may execute the command using the ``execute()`` |
| 73 | +method of the client. |
| 74 | + |
| 75 | +```php |
| 76 | +$result = $client->execute($command); |
| 77 | +``` |
| 78 | + |
| 79 | +The result of executing a command will be a ``GuzzleHttp\Command\ResultInterface`` |
| 80 | +object. Result objects are ``ArrayAccess``-ible and contain the data parsed from |
| 81 | +HTTP response. |
| 82 | + |
| 83 | +Service clients have magic methods that act as shortcuts to executing commands |
| 84 | +by name without having to create the ``Command`` object in a separate step |
| 85 | +before executing it. |
| 86 | + |
| 87 | +```php |
| 88 | +$result = $client->foo(['baz' => 'bar']); |
| 89 | +``` |
| 90 | + |
| 91 | +## Asynchronous Commands |
| 92 | + |
| 93 | +@TODO Add documentation |
| 94 | + |
| 95 | +* ``-Async`` suffix for client methods |
| 96 | +* Promises |
| 97 | + |
| 98 | +```php |
| 99 | +// Create and execute an asynchronous command. |
| 100 | +$command = $command = $client->getCommand('foo', ['baz' => 'bar']); |
| 101 | +$promise = $client->executeAsync($command); |
| 102 | + |
| 103 | +// Use asynchronous commands with magic methods. |
| 104 | +$promise = $client->fooAsync(['baz' => 'bar']); |
| 105 | +``` |
| 106 | + |
| 107 | +@TODO Add documentation |
| 108 | + |
| 109 | +* ``wait()``-ing on promises. |
| 110 | + |
| 111 | +```php |
| 112 | +$result = $promise->wait(); |
| 113 | + |
| 114 | +echo $result['fizz']; //> 'buzz' |
| 115 | +``` |
| 116 | + |
| 117 | +## Concurrent Requests |
| 118 | + |
| 119 | +@TODO Add documentation |
| 120 | + |
| 121 | +* ``executeAll()`` |
| 122 | +* ``executeAllAsync()``. |
| 123 | +* Options (``fulfilled``, ``rejected``, ``concurrency``) |
| 124 | + |
| 125 | +## Middleware: Extending the Client |
| 126 | + |
| 127 | +Middleware can be added to the service client or underlying HTTP client to |
| 128 | +implement additional behavior and customize the ``Command``-to-``Result`` and |
| 129 | +``Request``-to-``Response`` lifecycles, respectively. |
| 130 | + |
| 131 | +## Todo |
| 132 | + |
| 133 | +* Middleware system and command vs request layers |
| 134 | +* The ``HandlerStack`` |
0 commit comments