plugins-overview.rst 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ======================
  2. Plugin system overview
  3. ======================
  4. The workflow of sending a request and parsing a response is driven by Guzzle's event system, which is powered by the
  5. `Symfony2 Event Dispatcher component <http://symfony.com/doc/current/components/event_dispatcher/introduction.html>`_.
  6. Any object in Guzzle that emits events will implement the ``Guzzle\Common\HasEventDispatcher`` interface. You can add
  7. event subscribers directly to these objects using the ``addSubscriber()`` method, or you can grab the
  8. ``Symfony\Component\EventDispatcher\EventDispatcher`` object owned by the object using ``getEventDispatcher()`` and
  9. add a listener or event subscriber.
  10. Adding event subscribers to clients
  11. -----------------------------------
  12. Any event subscriber or event listener attached to the EventDispatcher of a ``Guzzle\Http\Client`` or
  13. ``Guzzle\Service\Client`` object will automatically be attached to all request objects created by the client. This
  14. allows you to attach, for example, a HistoryPlugin to a client object, and from that point on, every request sent
  15. through that client will utilize the HistoryPlugin.
  16. .. code-block:: php
  17. use Guzzle\Plugin\History\HistoryPlugin;
  18. use Guzzle\Service\Client;
  19. $client = new Client();
  20. // Create a history plugin and attach it to the client
  21. $history = new HistoryPlugin();
  22. $client->addSubscriber($history);
  23. // Create and send a request. This request will also utilize the HistoryPlugin
  24. $client->get('http://httpbin.org')->send();
  25. // Echo out the last sent request by the client
  26. echo $history->getLastRequest();
  27. .. tip::
  28. :doc:`Create event subscribers <creating-plugins>`, or *plugins*, to implement reusable logic that can be
  29. shared across clients. Event subscribers are also easier to test than anonymous functions.
  30. Pre-Built plugins
  31. -----------------
  32. Guzzle provides easy to use request plugins that add behavior to requests based on signal slot event notifications
  33. powered by the Symfony2 Event Dispatcher component.
  34. * :doc:`async-plugin`
  35. * :doc:`backoff-plugin`
  36. * :doc:`cache-plugin`
  37. * :doc:`cookie-plugin`
  38. * :doc:`curl-auth-plugin`
  39. * :doc:`history-plugin`
  40. * :doc:`log-plugin`
  41. * :doc:`md5-validator-plugin`
  42. * :doc:`mock-plugin`
  43. * :doc:`oauth-plugin`