Skip to content

Commit

Permalink
APIv4 - Standardize generic entity name resolution so that classNames…
Browse files Browse the repository at this point in the history
… do not have to equal entityNames

This will allow the addition of a CiviCase API which cannot have the className "Case" because that is a reserved php keyword

Also cleans up other entities that use `static::class` in place of entityName because it's not a good pattern to replicate.
  • Loading branch information
colemanw committed Mar 24, 2021
1 parent 218f91c commit 9f338c0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Civi/Api4/Generic/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class AbstractEntity {
* @return \Civi\Api4\Action\GetActions
*/
public static function getActions($checkPermissions = TRUE) {
return (new \Civi\Api4\Action\GetActions(self::getEntityName(), __FUNCTION__))
return (new \Civi\Api4\Action\GetActions(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand Down Expand Up @@ -109,7 +109,7 @@ protected static function getEntityPaths() {
* @throws NotImplementedException
*/
public static function __callStatic($action, $args) {
$entity = self::getEntityName();
$entity = static::getEntityName();
$nameSpace = str_replace('Civi\Api4\\', 'Civi\Api4\Action\\', static::class);
// Find class for this action
$entityAction = "$nameSpace\\" . ucfirst($action);
Expand Down
12 changes: 6 additions & 6 deletions Civi/Api4/Generic/BasicEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ abstract class BasicEntity extends AbstractEntity {
* @return BasicGetAction
*/
public static function get($checkPermissions = TRUE) {
return (new BasicGetAction(static::class, __FUNCTION__, static::$getter))
return (new BasicGetAction(static::getEntityName(), __FUNCTION__, static::$getter))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -96,7 +96,7 @@ public static function get($checkPermissions = TRUE) {
* @return BasicCreateAction
*/
public static function create($checkPermissions = TRUE) {
return (new BasicCreateAction(static::class, __FUNCTION__, static::$setter))
return (new BasicCreateAction(static::getEntityName(), __FUNCTION__, static::$setter))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -105,7 +105,7 @@ public static function create($checkPermissions = TRUE) {
* @return BasicSaveAction
*/
public static function save($checkPermissions = TRUE) {
return (new BasicSaveAction(static::class, __FUNCTION__, static::$idField, static::$setter))
return (new BasicSaveAction(static::getEntityName(), __FUNCTION__, static::$idField, static::$setter))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -114,7 +114,7 @@ public static function save($checkPermissions = TRUE) {
* @return BasicUpdateAction
*/
public static function update($checkPermissions = TRUE) {
return (new BasicUpdateAction(static::class, __FUNCTION__, static::$idField, static::$setter))
return (new BasicUpdateAction(static::getEntityName(), __FUNCTION__, static::$idField, static::$setter))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -123,7 +123,7 @@ public static function update($checkPermissions = TRUE) {
* @return BasicBatchAction
*/
public static function delete($checkPermissions = TRUE) {
return (new BasicBatchAction(static::class, __FUNCTION__, static::$idField, static::$deleter))
return (new BasicBatchAction(static::getEntityName(), __FUNCTION__, static::$idField, static::$deleter))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -132,7 +132,7 @@ public static function delete($checkPermissions = TRUE) {
* @return BasicReplaceAction
*/
public static function replace($checkPermissions = TRUE) {
return (new BasicReplaceAction(static::class, __FUNCTION__))
return (new BasicReplaceAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand Down
14 changes: 7 additions & 7 deletions Civi/Api4/Generic/DAOEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class DAOEntity extends AbstractEntity {
* @return DAOGetAction
*/
public static function get($checkPermissions = TRUE) {
return (new DAOGetAction(static::class, __FUNCTION__))
return (new DAOGetAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -41,7 +41,7 @@ public static function get($checkPermissions = TRUE) {
* @return DAOSaveAction
*/
public static function save($checkPermissions = TRUE) {
return (new DAOSaveAction(static::class, __FUNCTION__))
return (new DAOSaveAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -50,7 +50,7 @@ public static function save($checkPermissions = TRUE) {
* @return DAOGetFieldsAction
*/
public static function getFields($checkPermissions = TRUE) {
return (new DAOGetFieldsAction(static::class, __FUNCTION__))
return (new DAOGetFieldsAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -59,7 +59,7 @@ public static function getFields($checkPermissions = TRUE) {
* @return DAOCreateAction
*/
public static function create($checkPermissions = TRUE) {
return (new DAOCreateAction(static::class, __FUNCTION__))
return (new DAOCreateAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -68,7 +68,7 @@ public static function create($checkPermissions = TRUE) {
* @return DAOUpdateAction
*/
public static function update($checkPermissions = TRUE) {
return (new DAOUpdateAction(static::class, __FUNCTION__))
return (new DAOUpdateAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -77,7 +77,7 @@ public static function update($checkPermissions = TRUE) {
* @return DAODeleteAction
*/
public static function delete($checkPermissions = TRUE) {
return (new DAODeleteAction(static::class, __FUNCTION__))
return (new DAODeleteAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -86,7 +86,7 @@ public static function delete($checkPermissions = TRUE) {
* @return BasicReplaceAction
*/
public static function replace($checkPermissions = TRUE) {
return (new BasicReplaceAction(static::class, __FUNCTION__))
return (new BasicReplaceAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MailSettings extends Generic\DAOEntity {
* @return \Civi\Api4\Action\MailSettings\TestConnection
*/
public static function testConnection($checkPermissions = TRUE) {
$action = new \Civi\Api4\Action\MailSettings\TestConnection(static::class, __FUNCTION__);
$action = new \Civi\Api4\Action\MailSettings\TestConnection(__CLASS__, __FUNCTION__);
return $action->setCheckPermissions($checkPermissions);
}

Expand Down
4 changes: 2 additions & 2 deletions Civi/Api4/RelationshipCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RelationshipCache extends Generic\AbstractEntity {
* @return Generic\DAOGetAction
*/
public static function get($checkPermissions = TRUE) {
return (new Generic\DAOGetAction(static::class, __FUNCTION__))
return (new Generic\DAOGetAction(__CLASS__, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand All @@ -44,7 +44,7 @@ public static function get($checkPermissions = TRUE) {
* @return Generic\DAOGetFieldsAction
*/
public static function getFields($checkPermissions = TRUE) {
return (new Generic\DAOGetFieldsAction(static::class, __FUNCTION__))
return (new Generic\DAOGetFieldsAction(__CLASS__, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

Expand Down
7 changes: 4 additions & 3 deletions ang/api4Explorer/Explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,13 +713,14 @@

// Format oop params
function formatOOP(entity, action, params, indent) {
var code = '',
var info = getEntity(entity),
newLine = "\n" + _.repeat(' ', indent),
code = '\\' + info.class + '::' + action + '(',
perm = params.checkPermissions === false ? 'FALSE' : '';
if (entity.substr(0, 7) !== 'Custom_') {
code = "\\Civi\\Api4\\" + entity + '::' + action + '(' + perm + ')';
code += perm + ')';
} else {
code = "\\Civi\\Api4\\CustomValue::" + action + "('" + entity.substr(7) + "'" + (perm ? ', ' : '') + perm + ")";
code += "'" + entity.substr(7) + "'" + (perm ? ', ' : '') + perm + ")";
}
_.each(params, function(param, key) {
var val = '';
Expand Down

0 comments on commit 9f338c0

Please sign in to comment.