From: Tim Otten Date: Tue, 28 Mar 2017 07:10:38 +0000 (-0700) Subject: (NFC) GenericHookEvent - Update docblocks X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0d6813954451aff8a99f3736cc07e8127d1c03bd;p=civicrm-core.git (NFC) GenericHookEvent - Update docblocks --- diff --git a/Civi/Core/Event/GenericHookEvent.php b/Civi/Core/Event/GenericHookEvent.php index 4345c0c8d8..faee1e7e3a 100644 --- a/Civi/Core/Event/GenericHookEvent.php +++ b/Civi/Core/Event/GenericHookEvent.php @@ -38,30 +38,50 @@ namespace Civi\Core\Event; * * function hook_civicrm_foo($bar, &$whiz, &$bang); * - * Symfony Events are based on a class with properties and methods. This - * requires some kind of mapping. - * - * Implementing new event classes for every hook would produce a large - * amount of boilerplate. Symfony Events have an interesting solution to - * that problem: use `GenericEvent` instead of custom event classes. - * This class (`GenericHookEvent`) is conceptually similar to `GenericEvent`, - * but it adds support for (a) altering fields and (b) mapping to hook - * notation. + * The notation for Symfony Events is based on a class with properties + * and methods. This requires some kind of mapping. `GenericHookEvent` + * maps each parameter to a field (using magic methods): * * @code + * // Creating an event object. * $event = GenericHookEvent::create(array( - * 'first' => $readOnlyData, - * 'second' => &readWriteData, - * )); + * 'bar' => 'abc', + * 'whiz' => &$whiz, + * 'bang' => &$bang, + * ); + * + * // Accessing event properties. + * echo $event->bar; + * $event->whiz['array_field'] = 123; + * $event->bang->objProperty = 'abcd'; + * + * // Dispatching an event. * Civi::service('dispatcher')->dispatch('hook_civicrm_foo', $event); - * print_r($event->getReturnValues()); + * @endCode + * + * Design Discussion: + * + * 1. Implementing new event classes for every hook would produce a + * large amount of boilerplate. Symfony Events have an interesting solution to + * that problem: use `GenericEvent` instead of custom event classes. + * `GenericHookEvent` is conceptually similar to `GenericEvent`, but it adds + * support for (a) altering properties and (b) mapping properties to hook notation + * (an ordered parameter list). + * + * 2. A handful of hooks define a return-value. The return-value is treated + * as an array, and all the returned values are merged into one big array. + * You can add and retrieve return-values using these methods: + * + * @code + * $event->addReturnValues(array(...)); + * foreach ($event->getReturnValues() as $retVal) { ... } * @endCode */ class GenericHookEvent extends \Symfony\Component\EventDispatcher\Event { /** * @var array - * Ex: array('contactID' => &$contactID, 'contentPlacement' => &$contentPlacement). + * Ex: array(0 => &$contactID, 1 => &$contentPlacement). */ protected $hookValues;