Skip to content

Commit

Permalink
Merge pull request #3 from driftphp/feature/added-filesystem
Browse files Browse the repository at this point in the history
Added filesystem as service
  • Loading branch information
mmoreram authored Nov 18, 2019
2 parents 08c1ccc + 6ee47a1 commit 503afe8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
46 changes: 31 additions & 15 deletions AsyncKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

use Drift\HttpKernel\Exception\AsyncHttpKernelNeededException;
use React\EventLoop\LoopInterface;
use React\Filesystem\Filesystem;
use React\Promise\PromiseInterface;
use React\Promise\RejectedPromise;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;

Expand All @@ -35,12 +37,9 @@ abstract class AsyncKernel extends Kernel implements CompilerPassInterface
*
* When $catch is true, the implementation must catch all exceptions
* and do its best to convert them to a Response instance.
*
* @param Request $request
*
* @return PromiseInterface
*/
public function handleAsync(Request $request): PromiseInterface {
public function handleAsync(Request $request): PromiseInterface
{
$httpKernel = $this->getHttpKernel();
if (!$httpKernel instanceof AsyncHttpKernel) {
return new RejectedPromise(
Expand All @@ -56,12 +55,11 @@ public function handleAsync(Request $request): PromiseInterface {
*/
public function process(ContainerBuilder $container)
{

if ($container->has('event_dispatcher')) {
$this->isDebug()
? $this->processEventDispatcherDebug($container)
: $this->processEventDispatcher($container);
};
}

if ($container->has('http_kernel')) {
$container
Expand All @@ -78,15 +76,34 @@ public function process(ContainerBuilder $container)

if ($container->has('reactphp.event_loop')) {
$container->setAlias(LoopInterface::class, 'reactphp.event_loop');
$container->setAlias('event_loop', 'reactphp.event_loop');
$container->setAlias('drift.event_loop', 'reactphp.event_loop');
}

/*
* Create a filesystem instance
*/
if (!$container->has('drift.filesystem')) {
$filesystem = new Definition(Filesystem::class, [
new Reference('drift.event_loop'),
]);

$filesystem->setFactory([
Filesystem::class,
'create',
]);

$filesystem->setPublic(true);
$container->setDefinition('drift.filesystem', $filesystem);
$container->setAlias(Filesystem::class, 'drift.filesystem');
}
}

/**
* Process event dispatcher
*
* @param ContainerBuilder $container
* Process event dispatcher.
*/
private function processEventDispatcher(ContainerBuilder $container) {
private function processEventDispatcher(ContainerBuilder $container)
{
$container
->getDefinition('event_dispatcher')
->setClass(AsyncEventDispatcher::class);
Expand All @@ -95,11 +112,10 @@ private function processEventDispatcher(ContainerBuilder $container) {
}

/**
* Process event dispatcher in debug
*
* @param ContainerBuilder $container
* Process event dispatcher in debug.
*/
private function processEventDispatcherDebug(ContainerBuilder $container) {
private function processEventDispatcherDebug(ContainerBuilder $container)
{
if (!$container->hasDefinition('debug.event_dispatcher')) {
return;
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/AsyncKernelFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

use Mmoreram\BaseBundle\Kernel\DriftBaseKernel;
use Mmoreram\BaseBundle\Tests\BaseFunctionalTest;
use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Drift\HttpKernel\AsyncEventDispatcher;
use Drift\HttpKernel\AsyncHttpKernel;
Expand Down Expand Up @@ -65,6 +67,14 @@ protected static function getKernel(): KernelInterface
['name' => 'container.hot_path'],
],
],
'reactphp.event_loop' => [
'class' => LoopInterface::class,
'public' => true,
'factory' => [
Factory::class,
'create'
]
]
],
];

Expand Down
22 changes: 22 additions & 0 deletions Tests/KernelServicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace Drift\HttpKernel\Tests;

use React\EventLoop\LoopInterface;
use React\Filesystem\Filesystem;

/**
* Class KernelServicesTest
*/
class KernelServicesTest extends AsyncKernelFunctionalTest
{
/**
* Test autowiring
*/
public function testServices()
{
$this->assertInstanceof(LoopInterface::class, $this->get('reactphp.event_loop'));
$this->assertInstanceof(Filesystem::class, $this->get('drift.filesystem'));
}
}
8 changes: 7 additions & 1 deletion Tests/Services/AService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use Drift\HttpKernel\AsyncEventDispatcherInterface;
use Drift\HttpKernel\TraceableAsyncEventDispatcher;
use React\EventLoop\LoopInterface;
use React\Filesystem\Filesystem;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
Expand All @@ -20,10 +22,14 @@ final class AService
*
* @param EventDispatcherInterface $dispatcher
* @param AsyncEventDispatcherInterface $dispatcher
* @param LoopInterface $loop
* @param Filesystem $filesystem
*/
public function __construct(
EventDispatcherInterface $dispatcher1,
AsyncEventDispatcherInterface $dispatcher2
AsyncEventDispatcherInterface $dispatcher2,
LoopInterface $loop,
Filesystem $filesystem
)
{
$this->equal = ($dispatcher1 === $dispatcher2);
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

"symfony/http-kernel": "^4.3 || ^5.0",
"symfony/dependency-injection": "^4.3 || ^5.0",
"react/promise": "*"
"react/promise": "*",
"react/filesystem": "*"
},
"require-dev": {
"mmoreram/symfony-bundle-dependencies": "^2.2.0",
Expand Down

0 comments on commit 503afe8

Please sign in to comment.