creating-plugins.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ================
  2. Creating plugins
  3. ================
  4. .. highlight:: php
  5. Guzzle is extremely extensible because of the behavioral modifications that can be added to requests, clients, and
  6. commands using an event system. Before and after the majority of actions are taken in the library, an event is emitted
  7. with the name of the event and context surrounding the event. Observers can subscribe to a subject and modify the
  8. subject based on the events received. Guzzle's event system utilizes the Symfony2 EventDispatcher and is the backbone
  9. of its plugin architecture.
  10. Overview
  11. --------
  12. Plugins must implement the ``Symfony\Component\EventDispatcher\EventSubscriberInterface`` interface. The
  13. ``EventSubscriberInterface`` requires that your class implements a static method, ``getSubscribedEvents()``, that
  14. returns an associative array mapping events to methods on the object. See the
  15. `Symfony2 documentation <http://symfony.com/doc/2.0/book/internals.html#the-event-dispatcher>`_ for more information.
  16. Plugins can be attached to any subject, or object in Guzzle that implements that
  17. ``Guzzle\Common\HasDispatcherInterface``.
  18. Subscribing to a subject
  19. ~~~~~~~~~~~~~~~~~~~~~~~~
  20. You can subscribe an instantiated observer to an event by calling ``addSubscriber`` on a subject.
  21. .. code-block:: php
  22. $testPlugin = new TestPlugin();
  23. $client->addSubscriber($testPlugin);
  24. You can also subscribe to only specific events using a closure::
  25. $client->getEventDispatcher()->addListener('request.create', function(Event $event) {
  26. echo $event->getName();
  27. echo $event['request'];
  28. });
  29. ``Guzzle\Common\Event`` objects are passed to notified functions. The Event object has a ``getName()`` method which
  30. return the name of the emitted event and may contain contextual information that can be accessed like an array.
  31. Knowing what events to listen to
  32. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33. Any class that implements the ``Guzzle\Common\HasDispatcherInterface`` must implement a static method,
  34. ``getAllEvents()``, that returns an array of the events that are emitted from the object. You can browse the source
  35. to see each event, or you can call the static method directly in your code to get a list of available events.
  36. Event hooks
  37. -----------
  38. * :ref:`client-events`
  39. * :ref:`service-client-events`
  40. * :ref:`request-events`
  41. * ``Guzzle\Http\Curl\CurlMulti``:
  42. * :ref:`service-builder-events`
  43. Examples of the event system
  44. ----------------------------
  45. Simple Echo plugin
  46. ~~~~~~~~~~~~~~~~~~
  47. This simple plugin prints a string containing the request that is about to be sent by listening to the
  48. ``request.before_send`` event::
  49. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  50. class EchoPlugin implements EventSubscriberInterface
  51. {
  52. public static function getSubscribedEvents()
  53. {
  54. return array('request.before_send' => 'onBeforeSend');
  55. }
  56. public function onBeforeSend(Guzzle\Common\Event $event)
  57. {
  58. echo 'About to send a request: ' . $event['request'] . "\n";
  59. }
  60. }
  61. $client = new Guzzle\Service\Client('http://www.test.com/');
  62. // Create the plugin and add it as an event subscriber
  63. $plugin = new EchoPlugin();
  64. $client->addSubscriber($plugin);
  65. // Send a request and notice that the request is printed to the screen
  66. $client->get('/')->send();
  67. Running the above code will print a string containing the HTTP request that is about to be sent.