You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.8KB

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