* print_r(CRM_Utils_Array::collect('name', $i->getAll()));
* @endCode
*
+ * An event definition includes these fields:
+ * - type: string, required. Ex: 'hook' or 'object'
+ * - name: string, required. Ex: 'hook_civicrm_post' or 'civi.dao.postInsert'
+ * - class: string, required. Ex: 'Civi\Core\Event\GenericHookEvent'.
+ * - signature: string, required FOR HOOKS. Ex: '$first, &$second'.
+ * - fields: array, required FOR HOOKS. List of hook parameters.
+ * - stub: ReflectionMethod, optional. An example function with docblocks/inputs.
+ *
* Note: The inspector is only designed for use in developer workflows, such
* as code-generation and inspection. It should be not called by regular
* runtime logic.
* Register the default hooks defined by 'CRM_Utils_Hook'.
*
* @param \Civi\Core\Event\GenericHookEvent $e
- * @see \CRM_Utils_Hook::hooks()
+ * @see \CRM_Utils_Hook::eventDefs()
*/
public static function findBuiltInEvents(\Civi\Core\Event\GenericHookEvent $e) {
$skipList = array('singleton');
public function build($force = FALSE) {
if ($force || $this->eventDefs === NULL) {
$this->eventDefs = array();
- \CRM_Utils_Hook::hooks($this);
+ \CRM_Utils_Hook::eventDefs($this);
ksort($this->eventDefs);
}
return $this;
*/
public function find($regex) {
$this->build();
- return array_filter($this->eventDefs, function($e) use ($regex) {
+ return array_filter($this->eventDefs, function ($e) use ($regex) {
return preg_match($regex, $e['name']);
});
}
* TRUE if valid.
*/
public function validate($eventDef) {
- return
- is_array($eventDef)
- && !empty($eventDef['name'])
- && isset($eventDef['signature'])
- && is_array($eventDef['fields']);
+ if (!is_array($eventDef) || empty($eventDef['name']) || !isset($eventDef['type'])) {
+ return FALSE;
+ }
+
+ if (!in_array($eventDef['type'], array('hook', 'object'))) {
+ return FALSE;
+ }
+
+ if ($eventDef['type'] === 'hook') {
+ if (!isset($eventDef['signature']) || !is_array($eventDef['fields'])) {
+ return FALSE;
+ }
+ }
+
+ return TRUE;
}
/**
public function add($eventDef) {
$name = isset($eventDef['name']) ? $eventDef['name'] : NULL;
- if (!isset($eventDef['is_hook'])) {
- $eventDef['is_hook'] = (bool) preg_match('/^hook_/', $eventDef['name']);
+ if (!isset($eventDef['type'])) {
+ $eventDef['type'] = preg_match('/^hook_/', $eventDef['name']) ? 'hook' : 'object';
}
- if (empty($eventDef['signature'])) {
+ if ($eventDef['type'] === 'hook' && empty($eventDef['signature'])) {
$eventDef['signature'] = implode(', ', array_map(
function ($field) {
$sigil = $field['ref'] ? '&$' : '$';
return $this;
}
+ /**
+ * Scan a Symfony event class for metadata, and add it.
+ *
+ * @param string $event
+ * Ex: 'civi.api.authorize'.
+ * @param string $className
+ * Ex: 'Civi\API\Event\AuthorizeEvent'.
+ * @return CiviEventInspector
+ */
+ public function addEventClass($event, $className) {
+ $this->add(array(
+ 'name' => $event,
+ 'class' => $className,
+ ));
+ return $this;
+ }
+
/**
* Scan a class for hook stubs, and add all of them.
*
'description_html' => $method->getDocComment() ? \CRM_Admin_Page_APIExplorer::formatDocBlock($method->getDocComment()) : '',
'fields' => array(),
'class' => 'Civi\Core\Event\GenericHookEvent',
+ 'stub' => $method,
);
foreach ($method->getParameters() as $parameter) {