-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes FallbackDispatcher name and small code refactoring (#64)
- Loading branch information
1 parent
9f411f0
commit 78d3418
Showing
10 changed files
with
186 additions
and
155 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 was deleted.
Oops, something went wrong.
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerBridge; | ||
|
||
use Spiral\Boot\DispatcherInterface; | ||
use Spiral\RoadRunnerBridge\Exception\DispatcherNotFoundException; | ||
use Spiral\RoadRunnerBridge\Centrifugo\Dispatcher as Centrifugo; | ||
use Spiral\RoadRunnerBridge\GRPC\Dispatcher as GRPC; | ||
use Spiral\RoadRunnerBridge\Http\Dispatcher as Http; | ||
use Spiral\RoadRunnerBridge\Queue\Dispatcher as Queue; | ||
use Spiral\RoadRunnerBridge\Tcp\Dispatcher as Tcp; | ||
|
||
final class FallbackDispatcher implements DispatcherInterface | ||
{ | ||
private const ERROR = 'To use RoadRunner in `%s` mode, please register dispatcher `%s`.'; | ||
|
||
public function __construct( | ||
private readonly RoadRunnerMode $mode, | ||
) { | ||
} | ||
|
||
public function canServe(): bool | ||
{ | ||
return \PHP_SAPI === 'cli' && $this->mode !== RoadRunnerMode::Unknown; | ||
} | ||
|
||
public function serve(): void | ||
{ | ||
match ($this->mode) { | ||
RoadRunnerMode::Http => $this->throwException(Http::class), | ||
RoadRunnerMode::Jobs => $this->throwException(Queue::class), | ||
RoadRunnerMode::Grpc => $this->throwException(GRPC::class), | ||
RoadRunnerMode::Tcp => $this->throwException(Tcp::class), | ||
RoadRunnerMode::Centrifuge => $this->throwException(Centrifugo::class), | ||
RoadRunnerMode::Temporal => throw new DispatcherNotFoundException( | ||
'To use Temporal with RoadRunner, please install `spiral/temporal-bridge` package.' | ||
), | ||
RoadRunnerMode::Unknown => null, | ||
}; | ||
} | ||
|
||
/** | ||
* @param class-string<DispatcherInterface> $class | ||
* | ||
* @throws DispatcherNotFoundException | ||
*/ | ||
private function throwException(string $class): void | ||
{ | ||
throw new DispatcherNotFoundException(\sprintf(self::ERROR, $this->mode->name, $class)); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests; | ||
|
||
use Spiral\RoadRunnerBridge\Exception\DispatcherNotFoundException; | ||
use Spiral\RoadRunnerBridge\FallbackDispatcher; | ||
use Spiral\RoadRunnerBridge\RoadRunnerMode; | ||
|
||
final class FallbackDispatcherTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider canServeDataProvider | ||
*/ | ||
public function testCanServe(RoadRunnerMode $mode, bool $expected): void | ||
{ | ||
$dispatcher = new FallbackDispatcher($mode); | ||
|
||
$this->assertSame($expected, $dispatcher->canServe()); | ||
} | ||
|
||
/** | ||
* @dataProvider exceptionDataProvider | ||
*/ | ||
public function testException(RoadRunnerMode $mode, string $message): void | ||
{ | ||
$this->expectException(DispatcherNotFoundException::class); | ||
$this->expectExceptionMessage($message); | ||
|
||
(new FallbackDispatcher($mode))->serve(); | ||
} | ||
|
||
public function canServeDataProvider(): \Traversable | ||
{ | ||
yield [RoadRunnerMode::Http, true]; | ||
yield [RoadRunnerMode::Temporal, true]; | ||
yield [RoadRunnerMode::Jobs, true]; | ||
yield [RoadRunnerMode::Grpc, true]; | ||
yield [RoadRunnerMode::Tcp, true]; | ||
yield [RoadRunnerMode::Unknown, false]; | ||
} | ||
|
||
public function exceptionDataProvider(): \Traversable | ||
{ | ||
yield 'http' => [ | ||
RoadRunnerMode::Http, | ||
'To use RoadRunner in `Http` mode, please register dispatcher `Spiral\RoadRunnerBridge\Http\Dispatcher`.', | ||
]; | ||
yield 'jobs' => [ | ||
RoadRunnerMode::Jobs, | ||
'To use RoadRunner in `Jobs` mode, please register dispatcher `Spiral\RoadRunnerBridge\Queue\Dispatcher`.', | ||
]; | ||
yield 'grpc' => [ | ||
RoadRunnerMode::Grpc, | ||
'To use RoadRunner in `Grpc` mode, please register dispatcher `Spiral\RoadRunnerBridge\GRPC\Dispatcher`.', | ||
]; | ||
yield 'tcp' => [ | ||
RoadRunnerMode::Tcp, | ||
'To use RoadRunner in `Tcp` mode, please register dispatcher `Spiral\RoadRunnerBridge\Tcp\Dispatcher`.', | ||
]; | ||
yield 'temporal' => [ | ||
RoadRunnerMode::Temporal, | ||
'To use Temporal with RoadRunner, please install `spiral/temporal-bridge` package.', | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.