Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autowiring of API action classes #58

Merged
merged 1 commit into from
Mar 5, 2024
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
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 Api4ActionPropertyAutowireFixPass 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\Api4ActionPropertyAutowireFixPass;
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 Api4ActionPropertyAutowireFixPass(), PassConfig::TYPE_BEFORE_REMOVING);

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