Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions mixin/entity-types-php@2.0.0.mixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Auto-register entity declarations from `schema/*.entityType.php`.
*
* @mixinName entity-types-php
* @mixinVersion 2.0.0
* @since 5.73
*
* Changelog:
* - v2.0 scans /schema directory instead of /xml/schema/*
* - v2.0 supports only one entity per file
* - v2.0 adds 'module' key to each entity
*
* @param CRM_Extension_MixInfo $mixInfo
* On newer deployments, this will be an instance of MixInfo. On older deployments, Civix may polyfill with a work-a-like.
* @param \CRM_Extension_BootCache $bootCache
* On newer deployments, this will be an instance of BootCache. On older deployments, Civix may polyfill with a work-a-like.
*/
return function ($mixInfo, $bootCache) {

/**
* @param \Civi\Core\Event\GenericHookEvent $e
* @see CRM_Utils_Hook::entityTypes()
*/
Civi::dispatcher()->addListener('hook_civicrm_entityTypes', function ($e) use ($mixInfo) {
// When deactivating on a polyfill/pre-mixin system, listeners may not cleanup automatically.
if (!$mixInfo->isActive() || !is_dir($mixInfo->getPath('schema'))) {
return;
}

$files = (array) glob($mixInfo->getPath('schema/*.entityType.php'));
foreach ($files as $file) {
$entity = include $file;
$entity['module'] = $mixInfo->longName;
$e->entityTypes[$entity['name']] = $entity;
}
Comment on lines +32 to +37
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The glob() result should be handled safely to avoid issues if it returns false. Additionally, adding validation for the $entity variable (ensuring it is an array and contains a name) improves robustness and maintains consistency with the implementation in paymentprocessingcore.php. This prevents potential errors if a file in the schema directory is malformed or does not return the expected array.

    foreach (glob($mixInfo->getPath('schema/*.entityType.php')) ?: [] as $file) {
      $entity = include $file;
      if (is_array($entity) && !empty($entity['name'])) {
        $entity['module'] = $mixInfo->longName;
        $e->entityTypes[$entity['name']] = $entity;
      }
    }
References
  1. Prefer manual loops with explicit type checks to ensure compliance with strict static analysis and maintain consistency with established codebase patterns.

});

};
29 changes: 29 additions & 0 deletions paymentprocessingcore.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ function paymentprocessingcore_civicrm_enable(): void {
_paymentprocessingcore_civix_civicrm_enable();
}

/**
* Implements hook_civicrm_entityTypes().
*
* Directly registers entity types to work around a mixin timing issue on
* CiviCRM 5.75: the entity-types-php@2 mixin is only loaded for installed
* extensions, so during installation the mixin listener is absent and
* EntityRepository cannot find our entities. This direct hook ensures entity
* types are always registered. When both this and the mixin fire, the result
* is identical (same data, same keys).
*
* @see https://compucorp.atlassian.net/browse/CIVIPLMMSR-623
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
*/
function paymentprocessingcore_civicrm_entityTypes(&$entityTypes): void {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add the array type hint to the $entityTypes parameter. This ensures compliance with PHPStan level 9 requirements for proper typing and improves code clarity by explicitly defining the expected type for the reference.

function paymentprocessingcore_civicrm_entityTypes(array &$entityTypes): void {
References
  1. PHPStan level 9 compliance (proper types, no mixed where avoidable) (link)

$schemaDir = __DIR__ . '/schema';
if (!is_dir($schemaDir)) {
return;
}
$files = (array) glob($schemaDir . '/*.entityType.php');
foreach ($files as $file) {
Comment on lines +54 to +55
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Casting the result of glob() directly to (array) is problematic because glob() returns false on failure. In PHP, (array) false results in [false], which causes the loop to execute once with an invalid filename, leading to a warning during the include call. Using the null coalescing operator ?: [] is a safer way to handle potential failures.

  foreach (glob($schemaDir . '/*.entityType.php') ?: [] as $file) {

$entity = include $file;
if (is_array($entity) && !empty($entity['name'])) {
$entity['module'] = E::LONG_NAME;
$entityTypes[$entity['name']] = $entity;
}
}
}

/**
* Implements hook_civicrm_container().
*
Expand Down
Loading