Skip to content

Commit a317ae1

Browse files
author
Dominic Tubach
committed
Fix autowiring of APIv4 action classes
1 parent 37228e0 commit a317ae1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Civi\Civioffice\DependencyInjection\Compiler;
5+
6+
use Civi\Api4\Generic\AbstractAction;
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
10+
/**
11+
* Symfony DI (v5 and v6) tries to autowire properties annotated with @required.
12+
* Though CiviCRM action parameters can be annotated in this way to make them
13+
* mandatory. This pass clears the properties that are going to be injected for
14+
* APIv4 action classes registered as services in the
15+
* Civi\Civioffice\Api4\Action namespace. (In Symfony DI v7 support of
16+
* annotations is dropped in favor of PHP attributes.)
17+
*/
18+
final class Api4ActionPropertyAutowireFixPass implements CompilerPassInterface {
19+
20+
public function process(ContainerBuilder $container): void {
21+
foreach ($container->getDefinitions() as $id => $definition) {
22+
if ([] === $definition->getProperties() || !str_starts_with($id, 'Civi\\Civioffice\\Api4\\Action\\')) {
23+
continue;
24+
}
25+
26+
if (is_a($id, AbstractAction::class, TRUE)) {
27+
$definition->setProperties([]);
28+
}
29+
}
30+
}
31+
32+
}

services/civioffice.php

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/** @var \Symfony\Component\DependencyInjection\ContainerBuilder $container */
2222

2323
use Civi\Civioffice\Api4\Action\Civioffice\RenderWebAction;
24+
use Civi\Civioffice\DependencyInjection\Compiler\Api4ActionPropertyAutowireFixPass;
2425
use Civi\Civioffice\EventSubscriber\ActivityCiviOfficeTokenSubscriber;
2526
use Civi\Civioffice\EventSubscriber\CaseCiviOfficeTokenSubscriber;
2627
use Civi\Civioffice\EventSubscriber\CiviOfficeSearchKitTaskSubscriber;
@@ -31,11 +32,14 @@
3132
use Civi\Civioffice\EventSubscriber\ParticipantCiviOfficeTokenSubscriber;
3233
use Civi\Civioffice\Render\Queue\RenderQueueBuilderFactory;
3334
use Civi\Civioffice\Render\Queue\RenderQueueRunner;
35+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
3436

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

41+
$container->addCompilerPass(new Api4ActionPropertyAutowireFixPass(), PassConfig::TYPE_BEFORE_REMOVING);
42+
3943
$container->autowire(RenderQueueBuilderFactory::class)
4044
->setPublic(TRUE);
4145
$container->autowire(RenderQueueRunner::class)

0 commit comments

Comments
 (0)