Skip to content

Commit acd55f4

Browse files
authored
Feat: access parameters in expressions (#584)
* feat: parameters in expression language * access from symfony application * laravel provider
1 parent 52caa28 commit acd55f4

File tree

10 files changed

+219
-1
lines changed

10 files changed

+219
-1
lines changed

packages/Ecotone/src/Messaging/Handler/SymfonyExpressionEvaluationAdapter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Ecotone\Messaging\Handler;
66

7+
use Ecotone\Messaging\ConfigurationVariableService;
78
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
89

910
/**
@@ -96,6 +97,19 @@ function ($arguments, string $referenceName) use ($expressionLanguage) {
9697
}
9798
);
9899

100+
$expressionLanguage->register(
101+
'parameter',
102+
function ($str) {
103+
return $str;
104+
},
105+
function ($arguments, string $parameterName) use ($referenceSearchService) {
106+
/** @var ConfigurationVariableService $configurationVariableService */
107+
$configurationVariableService = $referenceSearchService->get(ConfigurationVariableService::REFERENCE_NAME);
108+
109+
return $configurationVariableService->getByName($parameterName);
110+
}
111+
);
112+
99113
$this->language = $expressionLanguage;
100114
}
101115

packages/Ecotone/tests/Messaging/Unit/Handler/SymfonyExpressionEvaluationAdapterTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace Test\Ecotone\Messaging\Unit\Handler;
44

5+
use Ecotone\Lite\EcotoneLite;
6+
use Ecotone\Messaging\Attribute\Parameter\Payload;
7+
use Ecotone\Messaging\Config\ModulePackageList;
8+
use Ecotone\Messaging\Config\ServiceConfiguration;
59
use Ecotone\Messaging\Handler\InMemoryReferenceSearchService;
610
use Ecotone\Messaging\Handler\SymfonyExpressionEvaluationAdapter;
11+
use Ecotone\Modelling\Attribute\CommandHandler;
12+
use Ecotone\Modelling\Attribute\QueryHandler;
713
use PHPUnit\Framework\TestCase;
814

915
/**
@@ -46,4 +52,37 @@ public function test_do_for_each_element_in_array()
4652
$expressionLanguage->evaluate("each(payload, 'createArray(\'id\', element)')", ['payload' => [1, 2, 3]], InMemoryReferenceSearchService::createEmpty())
4753
);
4854
}
55+
56+
public function test_parameter_function_in_expression()
57+
{
58+
$handler = new class {
59+
public int $result = 0;
60+
61+
#[CommandHandler('calculate')]
62+
public function handle(#[Payload("parameter('multiplier') * payload['value']")] int $calculatedValue): void
63+
{
64+
$this->result = $calculatedValue;
65+
}
66+
67+
#[QueryHandler('getResult')]
68+
public function getResult(): int
69+
{
70+
return $this->result;
71+
}
72+
};
73+
74+
$messaging = EcotoneLite::bootstrapFlowTesting(
75+
[get_class($handler)],
76+
[$handler],
77+
ServiceConfiguration::createWithDefaults()
78+
->withSkippedModulePackageNames(ModulePackageList::allPackages()),
79+
configurationVariables: [
80+
'multiplier' => 10,
81+
]
82+
);
83+
84+
$messaging->sendCommandWithRoutingKey('calculate', ['value' => 5]);
85+
86+
$this->assertEquals(50, $messaging->sendQueryWithRouting('getResult'));
87+
}
4988
}

packages/Laravel/tests/Application/Execution/ApplicationTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,38 @@ public function test_logs_are_collected()
146146
$logs[1]->message
147147
);
148148
}
149+
150+
public function test_parameter_function_in_expression_with_laravel_application(): void
151+
{
152+
$app = $this->createApplication();
153+
/** @var CommandBus $commandBus */
154+
$commandBus = $app->get(CommandBus::class);
155+
/** @var QueryBus $queryBus */
156+
$queryBus = $app->get(QueryBus::class);
157+
158+
$commandBus->sendWithRouting('calculator.multiply', ['value' => 5]);
159+
160+
$this->assertEquals(
161+
50,
162+
$queryBus->sendWithRouting('calculator.getResult')
163+
);
164+
}
165+
166+
public function test_parameter_function_with_env_variable_in_expression(): void
167+
{
168+
putenv('APP_MULTIPLIER=7');
169+
170+
$app = $this->createApplication();
171+
/** @var CommandBus $commandBus */
172+
$commandBus = $app->get(CommandBus::class);
173+
/** @var QueryBus $queryBus */
174+
$queryBus = $app->get(QueryBus::class);
175+
176+
$commandBus->sendWithRouting('calculator.multiplyWithEnv', ['value' => 3]);
177+
178+
$this->assertEquals(
179+
21,
180+
$queryBus->sendWithRouting('calculator.getEnvResult')
181+
);
182+
}
149183
}

packages/Laravel/tests/Application/config/app.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
return [
66

7+
/*
8+
|--------------------------------------------------------------------------
9+
| Custom Application Parameters
10+
|--------------------------------------------------------------------------
11+
|
12+
| Custom parameters for testing Ecotone's parameter() function.
13+
|
14+
*/
15+
16+
'multiplier' => 10,
17+
'env_multiplier' => env('APP_MULTIPLIER', 1),
18+
719
/*
820
|--------------------------------------------------------------------------
921
| Application Name
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Ecotone\Laravel\Fixture\ExpressionLanguage;
6+
7+
use Ecotone\Messaging\Attribute\Parameter\Payload;
8+
use Ecotone\Modelling\Attribute\CommandHandler;
9+
use Ecotone\Modelling\Attribute\QueryHandler;
10+
11+
/**
12+
* licence Apache-2.0
13+
*/
14+
final class CalculatorService
15+
{
16+
private int $result = 0;
17+
private int $envResult = 0;
18+
19+
#[CommandHandler('calculator.multiply')]
20+
public function multiply(#[Payload("parameter('app.multiplier') * payload['value']")] int $calculatedValue): void
21+
{
22+
$this->result = $calculatedValue;
23+
}
24+
25+
#[CommandHandler('calculator.multiplyWithEnv')]
26+
public function multiplyWithEnv(#[Payload("parameter('app.env_multiplier') * payload['value']")] int $calculatedValue): void
27+
{
28+
$this->envResult = $calculatedValue;
29+
}
30+
31+
#[QueryHandler('calculator.getResult')]
32+
public function getResult(): int
33+
{
34+
return $this->result;
35+
}
36+
37+
#[QueryHandler('calculator.getEnvResult')]
38+
public function getEnvResult(): int
39+
{
40+
return $this->envResult;
41+
}
42+
}
43+

packages/Laravel/tests/Fixture/ExpressionLanguage/LaravelProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ final class LaravelProvider extends ServiceProvider
1414
public function register()
1515
{
1616
$this->app->bind(ExpressionLanguageCommandHandler::class, ExpressionLanguageCommandHandler::class, true);
17+
$this->app->bind(CalculatorService::class, CalculatorService::class, true);
1718
}
1819
}

packages/Symfony/DependencyInjection/EcotoneExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function load(array $configs, ContainerBuilder $container): void
7474

7575
$configurationVariableService = new SymfonyConfigurationVariableService($container);
7676

77-
$container->register(\Ecotone\Messaging\ConfigurationVariableService::class, SymfonyConfigurationVariableService::class)->setAutowired(true);
77+
$container->register(\Ecotone\Messaging\ConfigurationVariableService::class, SymfonyConfigurationVariableService::class)->setAutowired(true)->setPublic(true);
7878

7979
$container->register(ServiceCacheConfiguration::REFERENCE_NAME, ServiceCacheConfiguration::class)
8080
->setArguments([

packages/Symfony/tests/phpunit/SingleTenant/SingleTenantTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ public function test_single_tenant_with_inner_container_using_event_bus(): void
149149
$ecotoneLite->getAggregate(Customer::class, 2)->getCustomerId()
150150
);
151151
}
152+
public function test_parameter_function_in_expression_with_symfony_application(): void
153+
{
154+
$this->commandBus->sendWithRouting('calculator.multiply', ['value' => 5]);
155+
156+
$this->assertEquals(
157+
50,
158+
$this->queryBus->sendWithRouting('calculator.getResult')
159+
);
160+
}
161+
162+
public function test_parameter_function_with_env_variable_in_expression(): void
163+
{
164+
putenv('APP_MULTIPLIER=7');
165+
166+
// Need to reboot kernel to pick up new env variable
167+
$kernel = new Kernel('dev', true);
168+
$kernel->boot();
169+
$app = $kernel->getContainer();
170+
171+
$commandBus = $app->get(CommandBus::class);
172+
$queryBus = $app->get(QueryBus::class);
173+
174+
$commandBus->sendWithRouting('calculator.multiplyWithEnv', ['value' => 3]);
175+
176+
$this->assertEquals(
177+
21,
178+
$queryBus->sendWithRouting('calculator.getEnvResult')
179+
);
180+
}
181+
152182
public function test_exception_handling_with_error_channel(): void
153183
{
154184
$application = new Application($this->kernel);

packages/Symfony/tests/phpunit/SingleTenant/config/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
return static function (ContainerConfigurator $containerConfigurator): void {
77

88
$containerConfigurator->parameters()->set('app.customer.activate_on_register', true);
9+
$containerConfigurator->parameters()->set('app.multiplier', 10);
10+
$containerConfigurator->parameters()->set('app.env_multiplier', '%env(int:APP_MULTIPLIER)%');
911

1012
$containerConfigurator->extension('ecotone', [
1113
'skippedModulePackageNames' => ModulePackageList::allPackagesExcept([
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\App\SingleTenant\Application;
6+
7+
use Ecotone\Messaging\Attribute\Parameter\Payload;
8+
use Ecotone\Modelling\Attribute\CommandHandler;
9+
use Ecotone\Modelling\Attribute\QueryHandler;
10+
11+
/**
12+
* licence Apache-2.0
13+
*/
14+
final class CalculatorService
15+
{
16+
private int $result = 0;
17+
private int $envResult = 0;
18+
19+
#[CommandHandler('calculator.multiply')]
20+
public function multiply(#[Payload("parameter('app.multiplier') * payload['value']")] int $calculatedValue): void
21+
{
22+
$this->result = $calculatedValue;
23+
}
24+
25+
#[CommandHandler('calculator.multiplyWithEnv')]
26+
public function multiplyWithEnv(#[Payload("parameter('app.env_multiplier') * payload['value']")] int $calculatedValue): void
27+
{
28+
$this->envResult = $calculatedValue;
29+
}
30+
31+
#[QueryHandler('calculator.getResult')]
32+
public function getResult(): int
33+
{
34+
return $this->result;
35+
}
36+
37+
#[QueryHandler('calculator.getEnvResult')]
38+
public function getEnvResult(): int
39+
{
40+
return $this->envResult;
41+
}
42+
}
43+

0 commit comments

Comments
 (0)