Skip to content

Commit

Permalink
Fix autowiring of APIv4 action classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Tubach committed Feb 28, 2024
1 parent 37228e0 commit 4e2854f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types = 1);

namespace Civi\Civioffice\DependencyInjection\Compiler;

use Civi\Api4\Generic\AbstractAction;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Symfony DI (v5 and v6) tries to autowire properties annotated with @required.
* Though CiviCRM action parameters can be annotated in this way to make them
* mandatory. This pass clears the properties that are going to be injected for
* APIv4 action classes registered as services in the
* Civi\Civioffice\Api4\Action namespace. (In Symfony DI v7 support of
* annotations is dropped in favor of PHP attributes.)
*/
final class ActionPropertyAutowireFixPass implements CompilerPassInterface {

public function process(ContainerBuilder $container): void {
foreach ($container->getDefinitions() as $id => $definition) {
if ([] === $definition->getProperties() || !str_starts_with($id, 'Civi\\Civioffice\\Api4\\Action\\')) {
continue;
}

if (is_a($id, AbstractAction::class, TRUE)) {
$definition->setProperties([]);
}
}
}

}
4 changes: 4 additions & 0 deletions services/civioffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/** @var \Symfony\Component\DependencyInjection\ContainerBuilder $container */

use Civi\Civioffice\Api4\Action\Civioffice\RenderWebAction;
use Civi\Civioffice\DependencyInjection\Compiler\ActionPropertyAutowireFixPass;
use Civi\Civioffice\EventSubscriber\ActivityCiviOfficeTokenSubscriber;
use Civi\Civioffice\EventSubscriber\CaseCiviOfficeTokenSubscriber;
use Civi\Civioffice\EventSubscriber\CiviOfficeSearchKitTaskSubscriber;
Expand All @@ -31,11 +32,14 @@
use Civi\Civioffice\EventSubscriber\ParticipantCiviOfficeTokenSubscriber;
use Civi\Civioffice\Render\Queue\RenderQueueBuilderFactory;
use Civi\Civioffice\Render\Queue\RenderQueueRunner;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

if (!$container->has(\CRM_Queue_Service::class)) {
$container->autowire(\CRM_Queue_Service::class, \CRM_Queue_Service::class);
}

$container->addCompilerPass(new ActionPropertyAutowireFixPass(), PassConfig::TYPE_BEFORE_REMOVING);

$container->autowire(RenderQueueBuilderFactory::class)
->setPublic(TRUE);
$container->autowire(RenderQueueRunner::class)
Expand Down

0 comments on commit 4e2854f

Please sign in to comment.