Skip to content

Commit

Permalink
EcotoneLite is not able to run console command from configured messag…
Browse files Browse the repository at this point in the history
…ing system (#122)

* add failing test

* handle circular reference with MessagingSystem injection

---------

Co-authored-by: jlabedo <jean@needelp.com>
Co-authored-by: Dariusz Gafka <dgafka.mail@gmail.com>
  • Loading branch information
3 people authored May 28, 2023
1 parent 1fffeea commit d3693d6
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 217 deletions.
9 changes: 5 additions & 4 deletions packages/Ecotone/src/Lite/EcotoneLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Ecotone\Messaging\Config\ModulePackageList;
use Ecotone\Messaging\Config\ProxyGenerator;
use Ecotone\Messaging\Config\ServiceConfiguration;
use Ecotone\Messaging\Config\StubConfiguredMessagingSystem;
use Ecotone\Messaging\Handler\ClassDefinition;
use Ecotone\Messaging\Handler\TypeDescriptor;
use Ecotone\Messaging\InMemoryConfigurationVariableService;
Expand Down Expand Up @@ -181,9 +180,11 @@ private static function prepareConfiguration(ContainerInterface|array $container
}
}

$messagingSystem = $messagingConfiguration->buildMessagingSystemFromConfiguration(
new PsrContainerReferenceSearchService($container, ['logger' => new NullLogger(), ConfiguredMessagingSystem::class => new StubConfiguredMessagingSystem()])
);
$referenceSearchService = new PsrContainerReferenceSearchService($container, ['logger' => new NullLogger()]);

$messagingSystem = $messagingConfiguration->buildMessagingSystemFromConfiguration($referenceSearchService);

$referenceSearchService->setConfiguredMessagingSystem($messagingSystem);

if ($allowGatewaysToBeRegisteredInContainer) {
$container->set(ConfiguredMessagingSystem::class, $messagingSystem);
Expand Down
6 changes: 4 additions & 2 deletions packages/Ecotone/src/Lite/EcotoneLiteConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Ecotone\Messaging\Config\ConfiguredMessagingSystem;
use Ecotone\Messaging\Config\MessagingSystemConfiguration;
use Ecotone\Messaging\Config\ServiceConfiguration;
use Ecotone\Messaging\Config\StubConfiguredMessagingSystem;
use Ecotone\Messaging\Handler\Logger\EchoLogger;
use Ecotone\Messaging\InMemoryConfigurationVariableService;
use Psr\Container\ContainerInterface;
Expand All @@ -27,13 +26,16 @@ public static function create(string $rootProjectDirectoryPath, ContainerInterfa

public static function createWithConfiguration(string $rootProjectDirectoryPath, ContainerInterface|GatewayAwareContainer $container, ServiceConfiguration $serviceConfiguration, array $configurationVariables, bool $useCachedVersion, array $classesToRegister = []): ConfiguredMessagingSystem
{
$referenceSearchService = new PsrContainerReferenceSearchService($container, ['logger' => new EchoLogger()]);
$configuredMessagingSystem = MessagingSystemConfiguration::prepare(
realpath($rootProjectDirectoryPath),
InMemoryConfigurationVariableService::create($configurationVariables),
$serviceConfiguration,
$useCachedVersion,
$classesToRegister
)->buildMessagingSystemFromConfiguration(new PsrContainerReferenceSearchService($container, ['logger' => new EchoLogger(), ConfiguredMessagingSystem::class => new StubConfiguredMessagingSystem()]));
)->buildMessagingSystemFromConfiguration($referenceSearchService);

$referenceSearchService->setConfiguredMessagingSystem($configuredMessagingSystem);

if ($container instanceof GatewayAwareContainer) {
foreach ($configuredMessagingSystem->getGatewayList() as $gatewayReference) {
Expand Down
18 changes: 18 additions & 0 deletions packages/Ecotone/src/Lite/PsrContainerReferenceSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Ecotone\Lite;

use Ecotone\Messaging\Config\ConfiguredMessagingSystem;
use Ecotone\Messaging\Config\LazyConfiguredMessagingSystem;
use Ecotone\Messaging\Handler\ReferenceNotFoundException;
use Ecotone\Messaging\Handler\ReferenceSearchService;
use Psr\Container\ContainerInterface;
Expand All @@ -13,10 +15,13 @@ class PsrContainerReferenceSearchService implements ReferenceSearchService
private ContainerInterface $container;
private array $defaults;

private LazyConfiguredMessagingSystem $lazyConfiguredMessagingSystem;

public function __construct(ContainerInterface $container, array $defaults = [])
{
$this->container = $container;
$this->defaults = $defaults;
$this->lazyConfiguredMessagingSystem = new LazyConfiguredMessagingSystem();
}

/**
Expand All @@ -28,6 +33,9 @@ public function get(string $reference): object
if (array_key_exists($reference, $this->defaults)) {
return $this->defaults[$reference];
}
if ($reference === ConfiguredMessagingSystem::class) {
return $this->lazyConfiguredMessagingSystem;
}

if ($this->container->has($reference . self::POSSIBLE_REFERENCE_SUFFIX)) {
return $this->container->get($reference . self::POSSIBLE_REFERENCE_SUFFIX);
Expand All @@ -51,4 +59,14 @@ public function has(string $referenceName): bool

return true;
}

public function setConfiguredMessagingSystem(ConfiguredMessagingSystem $configuredMessagingSystem): void
{
$this->lazyConfiguredMessagingSystem->replaceWith($configuredMessagingSystem);
}

public static function getServiceNameWithSuffix(string $referenceName)
{
return $referenceName . self::POSSIBLE_REFERENCE_SUFFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/
class LazyConfiguredMessagingSystem implements ConfiguredMessagingSystem
{
public function __construct(private ContainerInterface $container)
private ?ConfiguredMessagingSystem $configuredMessagingSystem = null;

public function __construct()
{
}

Expand Down Expand Up @@ -48,9 +50,7 @@ public function run(string $endpointId, ?ExecutionPollingMetadata $executionPoll

public function getServiceFromContainer(string $referenceName): object
{
Assert::isTrue($this->container->has($referenceName), "Service with reference {$referenceName} does not exists");

return $this->container->get($referenceName);
return $this->getConfiguredSystem()->getServiceFromContainer($referenceName);
}

public function getCommandBus(): CommandBus
Expand Down Expand Up @@ -90,11 +90,14 @@ public function runConsoleCommand(string $commandName, array $parameters): mixed

public function replaceWith(ConfiguredMessagingSystem $messagingSystem): void
{
$this->getConfiguredSystem()->replaceWith($messagingSystem);
$this->configuredMessagingSystem = $messagingSystem;
}

private function getConfiguredSystem(): ConfiguredMessagingSystem
{
return $this->container->get(LazyConfiguredMessagingSystem::class);
if (!$this->configuredMessagingSystem) {
throw new \InvalidArgumentException("Configured messaging system was not set");
}
return $this->configuredMessagingSystem;
}
}

This file was deleted.

27 changes: 27 additions & 0 deletions packages/Ecotone/tests/Lite/EcotoneLiteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Test\Ecotone\Lite;

use Ecotone\Lite\EcotoneLite;
use Ecotone\Messaging\Config\ModulePackageList;
use Ecotone\Messaging\Config\ServiceConfiguration;
use PHPUnit\Framework\TestCase;
use Test\Ecotone\Modelling\Fixture\Order\ChannelConfiguration;
use Test\Ecotone\Modelling\Fixture\Order\OrderService;

class EcotoneLiteTest extends TestCase
{
public function test_it_can_run_console_command(): void
{
$ecotone = EcotoneLite::bootstrap(
[OrderService::class, ChannelConfiguration::class],
[new OrderService()],
ServiceConfiguration::createWithDefaults()
->withSkippedModulePackageNames(ModulePackageList::allPackages())
->withEnvironment("test")
);

$ecotone->runConsoleCommand("ecotone:list", []);
$this->expectNotToPerformAssertions();
}
}
13 changes: 5 additions & 8 deletions packages/Laravel/src/EcotoneProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ecotone\Laravel;

use Ecotone\Lite\PsrContainerReferenceSearchService;
use const DIRECTORY_SEPARATOR;

use Ecotone\Messaging\Config\ConfiguredMessagingSystem;
Expand Down Expand Up @@ -166,15 +167,11 @@ function (ConfiguredMessagingSystem $configuredMessagingSystem) {

$this->app->singleton(
ConfiguredMessagingSystem::class,
function () {
return new LazyConfiguredMessagingSystem($this->app);
}
);

$this->app->singleton(
LazyConfiguredMessagingSystem::class,
function () use ($configuration) {
return $configuration->buildMessagingSystemFromConfiguration(new LaravelReferenceSearchService($this->app));
$referenceSearchService = new PsrContainerReferenceSearchService($this->app);
$messagingSystem = $configuration->buildMessagingSystemFromConfiguration($referenceSearchService);
$referenceSearchService->setConfiguredMessagingSystem($messagingSystem);
return $messagingSystem;
}
);
}
Expand Down
29 changes: 0 additions & 29 deletions packages/Laravel/src/LaravelReferenceSearchService.php

This file was deleted.

39 changes: 0 additions & 39 deletions packages/Laravel/tests/LaravelReferenceSearchServiceTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ecotone\SymfonyBundle\DepedencyInjection\Compiler;

use Ecotone\Lite\PsrContainerReferenceSearchService;
use Ecotone\Messaging\Config\Configuration;
use Ecotone\Messaging\Config\MessagingSystemConfiguration;
use Ecotone\Messaging\Config\ProxyGenerator;
Expand Down Expand Up @@ -111,10 +112,13 @@ public function process(ContainerBuilder $container)
$container->setDefinition(CacheCleaner::class, $definition);

$definition = new Definition();
$definition->setClass(SymfonyReferenceSearchService::class);
$definition->setClass(PsrContainerReferenceSearchService::class);
$definition->setPublic(true);
$definition->addArgument(new Reference('service_container'));
$container->setDefinition('symfonyReferenceSearchService', $definition);
$container->setDefinition(ReferenceSearchService::class, $definition);

// TODO: Remove this alias used for backward compatibility ?
$container->setAlias('symfonyReferenceSearchService', ReferenceSearchService::class)->setPublic(true);

foreach ($messagingConfiguration->getRegisteredGateways() as $gatewayProxyBuilder) {
$definition = new Definition();
Expand All @@ -136,7 +140,7 @@ public function process(ContainerBuilder $container)
continue;
}

$alias = $container->setAlias(SymfonyReferenceSearchService::getServiceNameWithSuffix($requiredReference), $requiredReference);
$alias = $container->setAlias(PsrContainerReferenceSearchService::getServiceNameWithSuffix($requiredReference), $requiredReference);

if ($alias) {
$alias->setPublic(true);
Expand All @@ -145,7 +149,7 @@ public function process(ContainerBuilder $container)

foreach ($messagingConfiguration->getOptionalReferences() as $requiredReference) {
if ($container->has($requiredReference)) {
$alias = $container->setAlias(SymfonyReferenceSearchService::getServiceNameWithSuffix($requiredReference), $requiredReference);
$alias = $container->setAlias(PsrContainerReferenceSearchService::getServiceNameWithSuffix($requiredReference), $requiredReference);

if ($alias) {
$alias->setPublic(true);
Expand Down
Loading

0 comments on commit d3693d6

Please sign in to comment.