Skip to content

Commit

Permalink
Add support for nette 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed May 12, 2021
1 parent 1f6ad96 commit 47878cd
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 21 deletions.
23 changes: 14 additions & 9 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ config.neon:

```neon
extensions:
# Nette 3.0+
rabbitmq: Contributte\RabbitMQ\DI\RabbitMQExtension
# Nette 2.4
rabbitmq: Contributte\RabbitMQ\DI\RabbitMQExtension24
```

### Example configuration
Expand All @@ -40,7 +44,7 @@ rabbitmq:
testQueue:
connection: default
# force queue declare on first queue operation during request
# autoCreate: true
# autoCreate: true
exchanges:
testExchange:
Expand Down Expand Up @@ -75,20 +79,20 @@ tracy:

### Declaring Queues and Exchanges

Since v3.0, all queues and exchanges are by default declared on demand using the console command:
Since v3.0, all queues and exchanges are by default declared on demand using the console command:

```bash
php index.php rabbitmq:declareQueuesAndExchanges
```

It's intended to be a part of the deploy process to make sure all the queues and exchanges are prepared for use.

If you need to override this behavior (for example only declare queues that are used during a request and nothing else),
If you need to override this behavior (for example only declare queues that are used during a request and nothing else),
just add the `autoCreate: true` parameter to queue or exchange of your choice.

You may also want to declare the queues and exchanges via rabbitmq management interface or a script but if you fail to
do so, don't run the declare console command and don't specify `autoCreate: true`, exceptions will be thrown
when accessing undeclared queues/exchanges.
You may also want to declare the queues and exchanges via rabbitmq management interface or a script but if you fail to
do so, don't run the declare console command and don't specify `autoCreate: true`, exceptions will be thrown when
accessing undeclared queues/exchanges.

### Publishing messages

Expand Down Expand Up @@ -201,7 +205,8 @@ final class LongRunningTestQueue

### Consuming messages

Your consumer callback has to return a confirmation that particular message has been acknowledges (or different states - unack, reject).
Your consumer callback has to return a confirmation that particular message has been acknowledges (or different states -
unack, reject).

TestConsumer.php

Expand Down Expand Up @@ -234,7 +239,8 @@ final class TestConsumer implements IConsumer

### Running a consumer trough CLI

There are two consumer commands prepared. `rabbitmq:consumer` wiil consume messages for specified amount of time (in seconds), to run indefinitely skip this parameter. Following command will be consuming messages for one hour:
There are two consumer commands prepared. `rabbitmq:consumer` wiil consume messages for specified amount of time (in
seconds), to run indefinitely skip this parameter. Following command will be consuming messages for one hour:

```bash
php index.php rabbitmq:consumer testConsumer 3600
Expand All @@ -246,7 +252,6 @@ Following command will be consuming messages indefinitely:
php index.php rabbitmq:consumer testConsumer
```


`rabbitmq:staticConsumer` will consume particular amount of messages. Following example will consume just 20 messages:

```bash
Expand Down
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ indent_style = tab
indent_size = tab
tab_width = 4

[{*.json,*.yml,*.md}]
[{*.yml,*.md}]
indent_style = space
indent_size = 2
6 changes: 3 additions & 3 deletions src/DI/Helpers/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Contributte\RabbitMQ\DI\Helpers;

use Contributte\RabbitMQ\DI\RabbitMQExtension;
use Nette\DI\CompilerExtension;

abstract class AbstractHelper
{
Expand All @@ -13,10 +13,10 @@ abstract class AbstractHelper
* @var array
*/
protected array $defaults = [];
protected RabbitMQExtension $extension;
protected CompilerExtension $extension;


public function __construct(RabbitMQExtension $extension)
public function __construct(CompilerExtension $extension)
{
$this->extension = $extension;
}
Expand Down
107 changes: 107 additions & 0 deletions src/DI/RabbitMQExtension24.php
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']);
}
}
48 changes: 48 additions & 0 deletions tests/Cases/DI/RabbitMQExtension24Test.phpt
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();
48 changes: 48 additions & 0 deletions tests/Cases/DI/RabbitMQExtensionTest.phpt
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();
12 changes: 4 additions & 8 deletions tests/Cases/ProducerTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,11 @@ final class ProducerTest extends TestCase
$channelMock = new ChannelMock();

$connectionMock = \Mockery::mock(IConnection::class)
->shouldReceive('getChannel')->andReturn($channelMock)->getMock()
;
->shouldReceive('getChannel')->andReturn($channelMock)->getMock();

$queueMock = \Mockery::mock(IQueue::class)
->shouldReceive('getConnection')->andReturn($connectionMock)->getMock()
->shouldReceive('getName')->andReturn($testQueueName)->getMock()
;
->shouldReceive('getName')->andReturn($testQueueName)->getMock();

$producer = new Producer(
null,
Expand All @@ -319,13 +317,11 @@ final class ProducerTest extends TestCase
$channelMock = new ChannelMock();

$connectionMock = \Mockery::mock(IConnection::class)
->shouldReceive('getChannel')->andReturn($channelMock)->getMock()
;
->shouldReceive('getChannel')->andReturn($channelMock)->getMock();

$exchangeMock = \Mockery::mock(IExchange::class)
->shouldReceive('getConnection')->andReturn($connectionMock)->getMock()
->shouldReceive('getName')->andReturn($testExchange)->getMock()
;
->shouldReceive('getName')->andReturn($testExchange)->getMock();

$producer = new Producer(
$exchangeMock,
Expand Down
21 changes: 21 additions & 0 deletions tests/Toolkit/NeonLoader.php
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));
}

}

0 comments on commit 47878cd

Please sign in to comment.