Skip to content

Commit

Permalink
Merge pull request #23311 from colemanw/unusedEvent
Browse files Browse the repository at this point in the history
APIv4 - Deprecate unnecessary event
  • Loading branch information
seamuslee001 authored Apr 28, 2022
2 parents 89952fb + 35be577 commit a43ee4b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 121 deletions.
16 changes: 6 additions & 10 deletions Civi/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
namespace Civi\API;

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

/**
* Class Request
Expand Down Expand Up @@ -45,22 +45,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
8 changes: 6 additions & 2 deletions Civi/Api4/Action/Entity/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ class Get extends \Civi\Api4\Generic\BasicGetAction {
protected $includeCustom;

/**
* Returns all APIv4 entities
* Returns all APIv4 entities from core, enabled components and enabled extensions.
*/
protected function getRecords() {
$provider = \Civi::service('action_object_provider');
return $provider->getEntities();
return array_filter($provider->getEntities(), function($entity) {
// Only include DAO entities from enabled components
$daoName = $entity['dao'] ?? NULL;
return (!$daoName || !defined("{$daoName}::COMPONENT") || \CRM_Core_Component::isEnabled($daoName::COMPONENT));
});
}

}
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.

16 changes: 2 additions & 14 deletions Civi/Api4/Provider/ActionObjectProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public function getEntities() {
if (!$entities) {
// Load entities declared in API files
foreach ($this->getAllApiClasses() as $className) {
$this->loadEntity($className, $entities);
$info = $className::getInfo();
$entities[$info['name']] = $info;
}
// Allow extensions to modify the list of entities
$event = GenericHookEvent::create(['entities' => &$entities]);
Expand All @@ -164,19 +165,6 @@ public function getEntities() {
return $entities;
}

/**
* @param \Civi\Api4\Generic\AbstractEntity $className
* @param array $entities
*/
private function loadEntity($className, array &$entities) {
$info = $className::getInfo();
$daoName = $info['dao'] ?? NULL;
// Only include DAO entities from enabled components
if (!$daoName || !defined("{$daoName}::COMPONENT") || \CRM_Core_Component::isEnabled($daoName::COMPONENT)) {
$entities[$info['name']] = $info;
}
}

/**
* Scan all api directories to discover entities
* @return \Civi\Api4\Generic\AbstractEntity[]
Expand Down
9 changes: 5 additions & 4 deletions Civi/Api4/Utils/CoreUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use Civi\API\Exception\NotImplementedException;
use Civi\API\Request;
use Civi\Api4\Event\CreateApi4RequestEvent;
use CRM_Core_DAO_AllCoreTables as AllCoreTables;

class CoreUtil {
Expand All @@ -39,9 +38,11 @@ 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;
$className = 'Civi\Api4\\' . $entityName;
if (class_exists($className)) {
return $className;
}
return self::getInfoItem($entityName, 'class');
}

/**
Expand Down

0 comments on commit a43ee4b

Please sign in to comment.