Merge pull request #13855 from eileenmcnaughton/activity_date_picker
[civicrm-core.git] / Civi / Core / CiviEventInspector.php
index f4dc1c6e2a8e7e33302abbc86e5d8083fdd759d7..4f74cc54a8d9ef24407c57bc3b2df31c29e63e8d 100644 (file)
@@ -12,6 +12,14 @@ namespace Civi\Core;
  * 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.
@@ -22,7 +30,7 @@ class CiviEventInspector {
    * 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');
@@ -49,7 +57,7 @@ class CiviEventInspector {
   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;
@@ -77,7 +85,7 @@ class CiviEventInspector {
    */
   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']);
     });
   }
@@ -101,11 +109,21 @@ class CiviEventInspector {
    *   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;
   }
 
   /**
@@ -117,11 +135,11 @@ class CiviEventInspector {
   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'] ? '&$' : '$';
@@ -139,6 +157,23 @@ class CiviEventInspector {
     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.
    *
@@ -165,6 +200,7 @@ class CiviEventInspector {
         '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) {