Skip to content

Commit

Permalink
ActionHandlerPass: Also check constant when looking for entity name
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Tubach committed Aug 11, 2023
1 parent 1ecf074 commit 8453737
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions Civi/RemoteTools/DependencyInjection/Compiler/ActionHandlerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function process(ContainerBuilder $container): void {
$handlerPriorities = [];
foreach ($container->findTaggedServiceIds(ActionHandlerInterface::SERVICE_TAG) as $id => $tags) {
foreach ($tags as $attributes) {
$entityName = $this->getAttribute($id, 'entity_name', $attributes);
$entityName = $this->getAttributeOrConst($container, $id, 'entity_name', $attributes);
$profileName = $attributes['profile_name'] ?? NULL;
$priority = $attributes['priority'] ?? 0;
$serviceClass = $this->getServiceClass($container, $id);
Expand Down Expand Up @@ -86,20 +86,40 @@ public function process(ContainerBuilder $container): void {
/**
* @phpstan-param array<int|string, scalar> $attributes
*/
private function getAttribute(string $id, string $key, array $attributes): string {
Assert::keyExists(
$attributes,
private function getAttributeOrConst(
ContainerBuilder $container,
string $id,
string $key,
array $attributes
): string {
if (isset($attributes[$key])) {
Assert::string(
$attributes[$key],
sprintf(
'Attribute "%s" in tag "%s" of service "%s" expected to be string, got %s',
$key,
ActionHandlerInterface::SERVICE_TAG,
$id,
gettype($attributes[$key])
)
);

return $attributes[$key];
}

$constantName = $this->getServiceClass($container, $id) . '::' . strtoupper($key);
if (defined($constantName)) {
// @phpstan-ignore-next-line
return constant($constantName);
}

throw new \RuntimeException(sprintf(
'Neither attribute "%s" in tag "%s" of service "%s" nor constant "%s" exists',
$key,
sprintf('Attribute "%s" in tag "%s" of service "%s" is missing', $key, ActionHandlerInterface::SERVICE_TAG, $id)
);
Assert::string($attributes[$key], sprintf(
'Attribute "%s" in tag "%s" of service "%s" expected to be string, got %s',
$key, ActionHandlerInterface::SERVICE_TAG,
ActionHandlerInterface::SERVICE_TAG,
$id,
gettype($attributes[$key])
$constantName
));

return $attributes[$key];
}

/**
Expand Down

0 comments on commit 8453737

Please sign in to comment.