-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
245 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace SlimFacades\Support; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use SlimFacades\Support\Pipeline; | ||
|
||
class Hub | ||
{ | ||
/** | ||
* The container implementation. | ||
* | ||
* @var \Psr\Container\ContainerInterface|null | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* All of the available pipelines. | ||
* | ||
* @var array | ||
*/ | ||
protected $pipelines = []; | ||
|
||
/** | ||
* Create a new Hub instance. | ||
* | ||
* @param \Psr\Container\ContainerInterface|null $container | ||
* @return void | ||
*/ | ||
public function __construct(ContainerInterface $container = null) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Define the default named pipeline. | ||
* | ||
* @param \Closure $callback | ||
* @return void | ||
*/ | ||
public function defaults(\Closure $callback) | ||
{ | ||
return $this->pipeline('default', $callback); | ||
} | ||
|
||
/** | ||
* Define a new named pipeline. | ||
* | ||
* @param string $name | ||
* @param \Closure $callback | ||
* @return void | ||
*/ | ||
public function pipeline($name, \Closure $callback) | ||
{ | ||
$this->pipelines[$name] = $callback; | ||
} | ||
|
||
/** | ||
* Send an object through one of the available pipelines. | ||
* | ||
* @param mixed $object | ||
* @param string|null $pipeline | ||
* @return mixed | ||
*/ | ||
public function pipe($object, $pipeline = null) | ||
{ | ||
$pipeline = $pipeline ?: 'default'; | ||
|
||
return call_user_func($this->pipelines[$pipeline], new Pipeline($this->container), $object); | ||
} | ||
|
||
/** | ||
* Get the container instance used by the hub. | ||
* | ||
* @return \Psr\Container\ContainerInterface | ||
*/ | ||
public function getContainer() | ||
{ | ||
return $this->container; | ||
} | ||
|
||
/** | ||
* Set the container instance used by the hub. | ||
* | ||
* @param \Psr\Container\ContainerInterface $container | ||
* @return $this | ||
*/ | ||
public function setContainer(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
|
||
return $this; | ||
} | ||
} |
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,78 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace SlimFacades\Tests\Support; | ||
|
||
use DI\Bridge\Slim\Bridge; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use SlimFacades\Support\Hub; | ||
|
||
final class HubTest extends TestCase | ||
{ | ||
protected ContainerInterface $container; | ||
|
||
public function setUp(): void | ||
{ | ||
$app = Bridge::create(); | ||
$this->container = $app->getContainer(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
unset($this->container); | ||
} | ||
|
||
public function testGetContainer() | ||
{ | ||
$hub = new Hub($this->container); | ||
$this->assertInstanceOf(ContainerInterface::class, $hub->getContainer()); | ||
} | ||
|
||
public function testHubReceivesDefault() | ||
{ | ||
$hub = new Hub($this->container); | ||
$hub->defaults(function ($pipeline, $passable) { | ||
return $pipeline->send($passable) | ||
->through(PipelineEmpty::class) | ||
->thenReturn(); | ||
}); | ||
|
||
$this->assertTrue($hub->pipe(true)); | ||
} | ||
|
||
public function testHubReceivesNamedPipe() | ||
{ | ||
$hub = new Hub($this->container); | ||
|
||
$hub->pipeline('test-pipeline', function ($pipeline, $passable) { | ||
return $pipeline->send($passable) | ||
->through(PipelineEmpty::class) | ||
->thenReturn(); | ||
}); | ||
|
||
$hub->defaults(function ($pipeline, $passable) { | ||
return $pipeline->send($passable) | ||
->through(PipelineFoo::class) | ||
->thenReturn(); | ||
}); | ||
|
||
$this->assertEquals('foo', $hub->pipe('foo', 'test-pipeline')); | ||
$this->assertEquals('foo', $hub->pipe('bar')); | ||
} | ||
} | ||
|
||
class PipelineEmpty | ||
{ | ||
public function handle($piped, $next) | ||
{ | ||
return $next($piped); | ||
} | ||
} | ||
|
||
class PipelineFoo | ||
{ | ||
public function handle($piped, $next) | ||
{ | ||
return $next('foo'); | ||
} | ||
} |