-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
246 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Contributte\RabbitMQ\DI; | ||
|
||
use Contributte\RabbitMQ\Client; | ||
use Contributte\RabbitMQ\Console\Command\ConsumerCommand; | ||
use Contributte\RabbitMQ\Console\Command\DeclareQueuesAndExchangesCommand; | ||
use Contributte\RabbitMQ\Console\Command\StaticConsumerCommand; | ||
use Contributte\RabbitMQ\DI\Helpers\ConnectionsHelper; | ||
use Contributte\RabbitMQ\DI\Helpers\ConsumersHelper; | ||
use Contributte\RabbitMQ\DI\Helpers\ExchangesHelper; | ||
use Contributte\RabbitMQ\DI\Helpers\ProducersHelper; | ||
use Contributte\RabbitMQ\DI\Helpers\QueuesHelper; | ||
use Nette\DI\CompilerExtension; | ||
|
||
final class RabbitMQExtension24 extends CompilerExtension | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private array $defaults = [ | ||
'connections' => [], | ||
'queues' => [], | ||
'exchanges' => [], | ||
'producers' => [], | ||
'consumers' => [], | ||
]; | ||
private ConnectionsHelper $connectionsHelper; | ||
private QueuesHelper $queuesHelper; | ||
private ProducersHelper $producersHelper; | ||
private ExchangesHelper $exchangesHelper; | ||
private ConsumersHelper $consumersHelper; | ||
|
||
|
||
public function __construct() | ||
{ | ||
$this->connectionsHelper = new ConnectionsHelper($this); | ||
$this->queuesHelper = new QueuesHelper($this); | ||
$this->exchangesHelper = new ExchangesHelper($this); | ||
$this->producersHelper = new ProducersHelper($this); | ||
$this->consumersHelper = new ConsumersHelper($this); | ||
} | ||
|
||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function loadConfiguration(): void | ||
{ | ||
$config = $this->validateConfig($this->defaults); | ||
$builder = $this->getContainerBuilder(); | ||
|
||
/** | ||
* Connections | ||
*/ | ||
$this->connectionsHelper->setup($builder, $config['connections']); | ||
|
||
/** | ||
* Queues | ||
*/ | ||
$this->queuesHelper->setup($builder, $config['queues']); | ||
|
||
/** | ||
* Exchanges | ||
*/ | ||
$this->exchangesHelper->setup($builder, $config['exchanges']); | ||
|
||
/** | ||
* Producers | ||
*/ | ||
$this->producersHelper->setup($builder, $config['producers']); | ||
|
||
/** | ||
* Consumers | ||
*/ | ||
$this->consumersHelper->setup($builder, $config['consumers']); | ||
|
||
/** | ||
* Register Client class | ||
*/ | ||
$builder->addDefinition($this->prefix('client')) | ||
->setFactory(Client::class); | ||
|
||
$this->setupConsoleCommand(); | ||
} | ||
|
||
|
||
public function setupConsoleCommand(): void | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$builder->addDefinition($this->prefix('console.consumerCommand')) | ||
->setFactory(ConsumerCommand::class) | ||
->setTags(['console.command' => 'rabbitmq:consumer']); | ||
|
||
$builder->addDefinition($this->prefix('console.staticConsumerCommand')) | ||
->setFactory(StaticConsumerCommand::class) | ||
->setTags(['console.command' => 'rabbitmq:staticConsumer']); | ||
|
||
$builder->addDefinition($this->prefix('console.declareQueuesExchangesCommand')) | ||
->setFactory(DeclareQueuesAndExchangesCommand::class) | ||
->setTags(['console.command' => 'rabbitmq:declareQueuesAndExchanges']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Contributte\RabbitMQ\Tests\Cases\DI; | ||
|
||
use Contributte\RabbitMQ\Connection\Connection; | ||
use Contributte\RabbitMQ\Connection\ConnectionFactory; | ||
use Contributte\RabbitMQ\DI\RabbitMQExtension24; | ||
use Contributte\RabbitMQ\Tests\Toolkit\NeonLoader; | ||
use Nette\DI\Compiler; | ||
use Nette\DI\Container; | ||
use Nette\DI\ContainerLoader; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
require __DIR__ . '/../../bootstrap.php'; | ||
|
||
final class RabbitMQExtension24Test extends TestCase | ||
{ | ||
|
||
public function testDefault(): void | ||
{ | ||
$loader = new ContainerLoader(TMP_DIR, true); | ||
$class = $loader->load(function (Compiler $compiler): void { | ||
$compiler->addExtension('rabbitmq', new RabbitMQExtension24()); | ||
$compiler->addConfig(NeonLoader::load(' | ||
rabbitmq: | ||
connections: | ||
default: | ||
user: guest | ||
password: guest | ||
host: localhost | ||
port: 5672 | ||
lazy: false | ||
')); | ||
$compiler->addDependencies([__FILE__]); | ||
}, __METHOD__); | ||
|
||
/** @var Container $container */ | ||
$container = new $class(); | ||
|
||
Assert::type(ConnectionFactory::class, $container->getByType(ConnectionFactory::class)); | ||
} | ||
|
||
} | ||
|
||
(new RabbitMQExtension24Test())->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Contributte\RabbitMQ\Tests\Cases\DI; | ||
|
||
use Contributte\RabbitMQ\Connection\Connection; | ||
use Contributte\RabbitMQ\Connection\ConnectionFactory; | ||
use Contributte\RabbitMQ\DI\RabbitMQExtension24; | ||
use Contributte\RabbitMQ\Tests\Toolkit\NeonLoader; | ||
use Nette\DI\Compiler; | ||
use Nette\DI\Container; | ||
use Nette\DI\ContainerLoader; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
require __DIR__ . '/../../bootstrap.php'; | ||
|
||
final class RabbitMQExtensionTest extends TestCase | ||
{ | ||
|
||
public function testDefault(): void | ||
{ | ||
$loader = new ContainerLoader(TMP_DIR, true); | ||
$class = $loader->load(function (Compiler $compiler): void { | ||
$compiler->addExtension('rabbitmq', new RabbitMQExtension24()); | ||
$compiler->addConfig(NeonLoader::load(' | ||
rabbitmq: | ||
connections: | ||
default: | ||
user: guest | ||
password: guest | ||
host: localhost | ||
port: 5672 | ||
lazy: false | ||
')); | ||
$compiler->addDependencies([__FILE__]); | ||
}, __METHOD__); | ||
|
||
/** @var Container $container */ | ||
$container = new $class(); | ||
|
||
Assert::type(ConnectionFactory::class, $container->getByType(ConnectionFactory::class)); | ||
} | ||
|
||
} | ||
|
||
(new RabbitMQExtensionTest())->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Contributte\RabbitMQ\Tests\Toolkit; | ||
|
||
use Nette\DI\Config\Adapters\NeonAdapter; | ||
use Nette\Neon\Neon; | ||
|
||
final class NeonLoader | ||
{ | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public static function load(string $str): array | ||
{ | ||
return (new NeonAdapter())->process((array) Neon::decode($str)); | ||
} | ||
|
||
} |