Skip to content

Commit

Permalink
APIv4 - Deprecate unnecessary event
Browse files Browse the repository at this point in the history
Before: To implement a virtual API4 entity, an extension must listen to 2 events, one of which is redundant.
After: Redundant event no longer used, as the one event returns all necessary data.

All the info from `civi.api4.entityTypes` is sufficient to create an APIv4 request.
`civi.api4.createRequest` was redundant.
  • Loading branch information
colemanw committed Apr 27, 2022
1 parent 82ba010 commit 34d36d7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 103 deletions.
15 changes: 6 additions & 9 deletions Civi/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Civi\API;

use Civi\Api4\Event\CreateApi4RequestEvent;
use Civi\Api4\Utils\CoreUtil;

/**
* Class Request
Expand Down Expand Up @@ -45,22 +46,18 @@ public static function create(string $entity, string $action, array $params) {
];

case 4:
// Load the API kernel service for registering API providers, as
// otherwise subscribers to the civi.api4.createRequest event registered
// through the EventSubscriberInterface will not be registered.
\Civi::service('civi_api_kernel');
$e = new CreateApi4RequestEvent($entity);
\Civi::dispatcher()->dispatch('civi.api4.createRequest', $e);
$callable = [$e->className, $action];
if (!$e->className || !is_callable($callable)) {
$className = CoreUtil::getApiClass($entity);
$callable = [$className, $action];
if (!$className || !is_callable($callable)) {
throw new \Civi\API\Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)");
}
// Check enabled components
$daoName = \CRM_Core_DAO_AllCoreTables::getFullName($entity);
if ($daoName && defined("{$daoName}::COMPONENT") && !\CRM_Core_Component::isEnabled($daoName::COMPONENT)) {
throw new \Civi\API\Exception\NotImplementedException("$entity API is not available because " . $daoName::COMPONENT . " component is disabled");
}
$apiRequest = call_user_func_array($callable, $e->args);
$args = (array) CoreUtil::getInfoItem($entity, 'class_args');
$apiRequest = call_user_func_array($callable, $args);
foreach ($params as $name => $param) {
$setter = 'set' . ucfirst($name);
$apiRequest->$setter($param);
Expand Down
39 changes: 2 additions & 37 deletions Civi/Api4/Event/CreateApi4RequestEvent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
Expand All @@ -14,43 +13,9 @@
use Civi\Core\Event\GenericHookEvent;

/**
* civi.api4.createRequest event.
*
* This event fires whenever resolving the name of an api entity to an api class.
*
* e.g. the entity name "Contact" resolves to the class `Civi\Api4\Contact`
* and the entity "Case" resolves to `Civi\Api4\CiviCase`
* Unused event, kept around to prevent undefined class errors in extensions that listen for it.
* @deprecated
*/
class CreateApi4RequestEvent extends GenericHookEvent {

/**
* Name of the entity to matched to an api class
*
* @var string
*/
public $entityName;

/**
* Resolved fully-namespaced class name.
*
* @var string
*/
public $className;

/**
* Additional arguments which should be passed to the action factory function.
*
* For example, `Civi\Api4\CustomValue` factory functions require the name of the custom group.
*
* @var array
*/
public $args = [];

/**
* Event constructor
*/
public function __construct($entityName) {
$this->entityName = $entityName;
}

}
54 changes: 0 additions & 54 deletions Civi/Api4/Event/Subscriber/CreateApi4RequestSubscriber.php

This file was deleted.

4 changes: 1 addition & 3 deletions Civi/Api4/Utils/CoreUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public static function getBAOFromApiName($entityName) {
* @return string|\Civi\Api4\Generic\AbstractEntity
*/
public static function getApiClass($entityName) {
$e = new CreateApi4RequestEvent($entityName);
\Civi::dispatcher()->dispatch('civi.api4.createRequest', $e);
return $e->className;
return self::getInfoItem($entityName, 'class');
}

/**
Expand Down

0 comments on commit 34d36d7

Please sign in to comment.