Merge pull request #17362 from colemanw/dev-core-1757-hide-toggle-menu-button
[civicrm-core.git] / Civi / Core / CiviEventInspector.php
index d5d3678607239cddd59dad6fe195526241317bba..34514f9bf750dffa23203502a5133a5a22e6d0ed 100644 (file)
@@ -7,10 +7,18 @@ namespace Civi\Core;
  * The event inspector is a development tool which provides metadata about events.
  * It can be used for code-generators and documentation-generators.
  *
- * @code
+ * ```
  * $i = new CiviEventInspector();
  * 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
@@ -25,7 +33,7 @@ class CiviEventInspector {
    * @see \CRM_Utils_Hook::eventDefs()
    */
   public static function findBuiltInEvents(\Civi\Core\Event\GenericHookEvent $e) {
-    $skipList = array('singleton');
+    $skipList = ['singleton'];
     $e->inspector->addStaticStubs('CRM_Utils_Hook', 'hook_civicrm_',
       function ($eventDef, $method) use ($skipList) {
         return in_array($method->name, $skipList) ? NULL : $eventDef;
@@ -48,7 +56,7 @@ class CiviEventInspector {
    */
   public function build($force = FALSE) {
     if ($force || $this->eventDefs === NULL) {
-      $this->eventDefs = array();
+      $this->eventDefs = [];
       \CRM_Utils_Hook::eventDefs($this);
       ksort($this->eventDefs);
     }
@@ -105,7 +113,7 @@ class CiviEventInspector {
       return FALSE;
     }
 
-    if (!in_array($eventDef['type'], array('hook', 'object'))) {
+    if (!in_array($eventDef['type'], ['hook', 'object'])) {
       return FALSE;
     }
 
@@ -125,7 +133,7 @@ class CiviEventInspector {
    * @return CiviEventInspector
    */
   public function add($eventDef) {
-    $name = isset($eventDef['name']) ? $eventDef['name'] : NULL;
+    $name = $eventDef['name'] ?? NULL;
 
     if (!isset($eventDef['type'])) {
       $eventDef['type'] = preg_match('/^hook_/', $eventDef['name']) ? 'hook' : 'object';
@@ -159,10 +167,10 @@ class CiviEventInspector {
    * @return CiviEventInspector
    */
   public function addEventClass($event, $className) {
-    $this->add(array(
+    $this->add([
       'name' => $event,
       'class' => $className,
-    ));
+    ]);
     return $this;
   }
 
@@ -187,19 +195,20 @@ class CiviEventInspector {
         continue;
       }
 
-      $eventDef = array(
+      $eventDef = [
         'name' => $prefix . $method->name,
         'description_html' => $method->getDocComment() ? \CRM_Admin_Page_APIExplorer::formatDocBlock($method->getDocComment()) : '',
-        'fields' => array(),
+        'fields' => [],
         'class' => 'Civi\Core\Event\GenericHookEvent',
-      );
+        'stub' => $method,
+      ];
 
       foreach ($method->getParameters() as $parameter) {
-        $eventDef['fields'][$parameter->getName()] = array(
+        $eventDef['fields'][$parameter->getName()] = [
           'name' => $parameter->getName(),
           'ref' => (bool) $parameter->isPassedByReference(),
           // WISHLIST: 'type' => 'mixed',
-        );
+        ];
       }
 
       if ($filter !== NULL) {