Skip to content

Commit

Permalink
Client now used as interface in queue connector. Added interface for …
Browse files Browse the repository at this point in the history
…Client service. Added client provider and registered client service as singleton. Readme.md, composer.json updated
  • Loading branch information
goodway committed Nov 9, 2024
1 parent 9e2165a commit 756db37
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ You should publish
the [config/nats.php](https://github.com/goodway/laravel-nats/blob/main/config/nats.php)
config file with:
```bash
$ php artisan vendor:publish --provider="Goodway\LaravelNats\NatsClientProvider"
$ php artisan vendor:publish --provider="Goodway\LaravelNats\NatsQueueProvider"
```

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "goodway/laravel-nats",
"description": "Nats jetstream with queue driver for Laravel",
"description": "Nats queue driver with client for Laravel",
"keywords": [
"php",
"nats",
Expand Down Expand Up @@ -37,6 +37,7 @@
"extra": {
"laravel": {
"providers": [
"Goodway\\LaravelNats\\NatsClientProvider",
"Goodway\\LaravelNats\\NatsQueueProvider"
]
}
Expand Down
16 changes: 8 additions & 8 deletions src/NatsClientService.php → src/Client/NatsClientService.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<?php

namespace Goodway\LaravelNats;
namespace Goodway\LaravelNats\Client;

use Goodway\LaravelNats\DTO\NatsClientConfiguration;
use Goodway\LaravelNats\Exceptions\NatsClientException;
use Basis\Nats\Client as NatsClient;
use Basis\Nats\Configuration as NatsConfiguration;
use Goodway\LaravelNats\Contracts\INatsClientProvider;
use Goodway\LaravelNats\DTO\NatsClientConfiguration;
use Goodway\LaravelNats\Exceptions\NatsClientException;

class NatsClientService
class NatsClientService implements INatsClientProvider
{
public function __construct(

) {}


/**
* Initialize Nats Client with specific configuration
* @param string|array $configuration If string then import from nats.php configuration file
* @param string|array $configuration If string then import from $configurationFileName.php configuration file
* @param string $configurationFileName Name of your nats configuration php file
* @return NatsClient
* @throws NatsClientException
*/
public function init(
string|array $configuration = 'default',
string $configurationFileName = 'nats',
string|array $configuration = 'default',
string $configurationFileName = 'nats',
): NatsClient
{
$configArray = is_array($configuration) ? $configuration
Expand Down
13 changes: 13 additions & 0 deletions src/Contracts/INatsClientProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Goodway\LaravelNats\Contracts;

use Basis\Nats\Client as NatsClient;

interface INatsClientProvider
{
public function init(
string|array $configuration = 'default',
string $configurationFileName = 'nats',
): NatsClient;
}
28 changes: 28 additions & 0 deletions src/NatsClientProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Goodway\LaravelNats;

use Goodway\LaravelNats\Client\NatsClientService;
use Goodway\LaravelNats\Contracts\INatsClientProvider;
use Illuminate\Support\ServiceProvider;

class NatsClientProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
$this->app->singleton(INatsClientProvider::class, function () {
return new NatsClientService();
});
}

/**
* Bootstrap services.
*/
public function boot(): void
{
//
}
}
4 changes: 2 additions & 2 deletions src/NatsMessageJobBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class NatsMessageJobBase extends NatsMessageJob
{

public function __construct(
private readonly mixed $body,
private readonly string $body,
private readonly array $headers = [],
protected string $subject = 'default',
) {}

public function body(): mixed
public function body(): string
{
return $this->body;
}
Expand Down
9 changes: 4 additions & 5 deletions src/NatsQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Goodway\LaravelNats;

use Goodway\LaravelNats\Contracts\INatsClientProvider;
use Goodway\LaravelNats\Queue\NatsQueueConnector;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\ServiceProvider;

class NatsQueueProvider extends ServiceProvider
Expand All @@ -21,17 +23,14 @@ public function register(): void
/**
* Bootstrap services.
*/
public function boot(): void
public function boot(QueueManager $manager, INatsClientProvider $natsClient): void
{
$this->offerPublishing();

$manager = $this->app['queue'];
/**
* Register connector for 'nats' queue driver
*/
$manager->addConnector('nats', function() {
return new NatsQueueConnector();
});
$manager->addConnector('nats', fn() => new NatsQueueConnector($natsClient));
}

protected function offerPublishing(): void
Expand Down
3 changes: 2 additions & 1 deletion src/Queue/Handlers/NatsQueueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Goodway\LaravelNats\DTO\NatsMessage;
use Goodway\LaravelNats\Events\NatsQueueMessageReceived;
use Goodway\LaravelNats\Exceptions\NatsConsumerException;
use Throwable;

abstract class NatsQueueHandler implements INatsQueueHandler
{
Expand Down Expand Up @@ -110,7 +111,7 @@ function () use ($consumer) {
$this->handleEmpty($this->queue, $consumer);
}
);
} catch (\Throwable $e) {
} catch (Throwable $e) {
var_dump($e->getMessage());
}

Expand Down
13 changes: 6 additions & 7 deletions src/Queue/NatsQueueConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@

namespace Goodway\LaravelNats\Queue;

use Goodway\LaravelNats\Contracts\INatsClientProvider;
use Goodway\LaravelNats\Exceptions\NatsClientException;
use Goodway\LaravelNats\NatsClientService;
use Goodway\LaravelNats\Queue\Handlers\NatsQueueHandler;
use Goodway\LaravelNats\Queue\Handlers\NatsQueueHandlerDefault;
use Illuminate\Queue\Connectors\ConnectorInterface;

class NatsQueueConnector implements ConnectorInterface
{
public function __construct() {
//
}
public function __construct(
public INatsClientProvider $natsClient
) {}

/**
* @throws NatsClientException
*/
public function connect(array $config): NatsQueue
{
$clientConfConsumer = $config['consumer_client'] ?? 'default';
$clientConfPublisher = $config['publisher_client'] ?? 'default';
$separateIdentical = isset($config['queue_separated_clients']) && $config['queue_separated_clients'];

$clientSub = (new NatsClientService())->init($clientConfConsumer);
$clientSub = $this->natsClient->init($clientConfConsumer);
// $clientSub->setDelay(0.01, NatsConfiguration::DELAY_LINEAR);

$clientPub = $clientConfConsumer === $clientConfPublisher && !$separateIdentical ?
$clientSub
: (new NatsClientService())->init($clientConfPublisher)
: $this->natsClient->init($clientConfPublisher)
;

$queueHandler = isset($config['queue_handler'])
Expand Down

0 comments on commit 756db37

Please sign in to comment.