-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
199 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Provider; | ||
|
||
use LogicException; | ||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
final class ChannelNotFoundException extends LogicException implements QueueProviderException | ||
{ | ||
public function __construct(string $channel, int $code = 0, ?Throwable $previous = null) | ||
{ | ||
parent::__construct( | ||
sprintf('Channel "%s" not found.', $channel), | ||
$code, | ||
$previous, | ||
); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Provider; | ||
|
||
use Yiisoft\Queue\QueueInterface; | ||
|
||
final class CompositeQueueProvider implements QueueProviderInterface | ||
{ | ||
/** | ||
* @var QueueProviderInterface[] | ||
*/ | ||
private readonly array $providers; | ||
|
||
public function __construct( | ||
QueueProviderInterface ...$providers | ||
) { | ||
$this->providers = $providers; | ||
} | ||
|
||
public function get(string $channel): QueueInterface | ||
{ | ||
foreach ($this->providers as $provider) { | ||
if ($provider->has($channel)) { | ||
return $provider->get($channel); | ||
} | ||
} | ||
throw new ChannelNotFoundException($channel); | ||
} | ||
|
||
public function has(string $channel): bool | ||
{ | ||
foreach ($this->providers as $provider) { | ||
if ($provider->has($channel)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Provider; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Yiisoft\Definitions\Exception\InvalidConfigException; | ||
use Yiisoft\Factory\StrictFactory; | ||
use Yiisoft\Queue\QueueInterface; | ||
|
||
use function array_key_exists; | ||
|
||
final class FactoryQueueProvider implements QueueProviderInterface | ||
{ | ||
/** | ||
* @psalm-var array<string, QueueInterface|null> | ||
*/ | ||
private array $queues = []; | ||
|
||
private readonly StrictFactory $factory; | ||
|
||
/** | ||
* @throws InvalidConfigException | ||
*/ | ||
public function __construct( | ||
private readonly array $definitions = [], | ||
?ContainerInterface $container = null, | ||
bool $validate = true, | ||
) { | ||
$this->factory = new StrictFactory($definitions, $container, $validate); | ||
} | ||
|
||
public function get(string $channel): QueueInterface | ||
{ | ||
$queue = $this->getOrTryCreate($channel); | ||
if ($queue === null) { | ||
throw new ChannelNotFoundException($channel); | ||
} | ||
return $queue; | ||
} | ||
|
||
public function has(string $channel): bool | ||
{ | ||
return array_key_exists($channel, $this->definitions); | ||
} | ||
|
||
/** | ||
* @throws InvalidQueueConfigException | ||
*/ | ||
private function getOrTryCreate(string $channel): QueueInterface|null | ||
{ | ||
if (array_key_exists($channel, $this->queues)) { | ||
return $this->queues[$channel]; | ||
} | ||
|
||
if ($this->factory->has($channel)) { | ||
try { | ||
$this->queues[$channel] = $this->factory->create($channel); | ||
} catch (InvalidConfigException $exception) { | ||
throw new InvalidQueueConfigException($exception->getMessage(), previous: $exception); | ||
} | ||
} else { | ||
$this->queues[$channel] = null; | ||
} | ||
|
||
return $this->queues[$channel]; | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Provider; | ||
|
||
use LogicException; | ||
|
||
final class InvalidQueueConfigException extends LogicException implements QueueProviderException | ||
{ | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Provider; | ||
|
||
use Yiisoft\Queue\QueueInterface; | ||
|
||
final class PrototypeQueueProvider implements QueueProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly QueueInterface $baseQueue, | ||
) { | ||
} | ||
|
||
public function get(string $channel): QueueInterface | ||
{ | ||
return $this->baseQueue->withChannelName($channel); | ||
} | ||
|
||
public function has(string $channel): bool | ||
{ | ||
return true; | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Provider; | ||
|
||
use Throwable; | ||
|
||
interface QueueProviderException extends Throwable | ||
{ | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Provider; | ||
|
||
use Yiisoft\Queue\QueueInterface; | ||
|
||
interface QueueProviderInterface | ||
{ | ||
/** | ||
* @throws InvalidQueueConfigException | ||
* @throws ChannelNotFoundException | ||
* @throws QueueProviderException | ||
*/ | ||
public function get(string $channel): QueueInterface; | ||
|
||
public function has(string $channel): bool; | ||
} |