Skip to content

Commit

Permalink
Cleanup (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen authored and mnapoli committed Jun 2, 2016
1 parent d0e8987 commit 78e24e6
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 75 deletions.
18 changes: 8 additions & 10 deletions src/Application.php
Expand Up @@ -8,6 +8,7 @@
use DI\ContainerBuilder;
use Interop\Container\ContainerInterface;
use Invoker\CallableResolver;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
Expand Down Expand Up @@ -46,8 +47,8 @@ public function __construct(ContainerBuilder $containerBuilder = null, array $va

$containerBuilder = $containerBuilder ?: new ContainerBuilder();
$containerBuilder->addDefinitions([
'Interop\Container\ContainerInterface' => $this->containerInteropProxy,
'Silex\Application' => $this,
ContainerInterface::class => $this->containerInteropProxy,
\Silex\Application::class => $this,
get_class($this) => $this,
]);
$containerBuilder->wrapContainer($this->containerInteropProxy);
Expand Down Expand Up @@ -104,7 +105,7 @@ public function before($callback, $priority = 0)
$middleware = $this['callback_resolver']->resolveCallback($callback);
$ret = $this->callbackInvoker->call($middleware, [
// type hints
'Symfony\Component\HttpFoundation\Request' => $request,
Request::class => $request,
// Silex' default parameter order
0 => $request,
1 => $this,
Expand All @@ -113,7 +114,6 @@ public function before($callback, $priority = 0)
if ($ret instanceof Response) {
$event->setResponse($ret);
}

}, $priority);
}

Expand All @@ -129,8 +129,8 @@ public function after($callback, $priority = 0)
$middleware = $this['callback_resolver']->resolveCallback($callback);
$ret = $this->callbackInvoker->call($middleware, [
// type hints
'Symfony\Component\HttpFoundation\Request' => $request,
'Symfony\Component\HttpFoundation\Response' => $response,
Request::class => $request,
Response::class => $response,
// Silex' default parameter order
0 => $request,
1 => $response,
Expand All @@ -142,21 +142,19 @@ public function after($callback, $priority = 0)
} elseif (null !== $ret) {
throw new \RuntimeException('An after middleware returned an invalid response value. Must return null or an instance of Response.');
}

}, $priority);
}

public function finish($callback, $priority = 0)
{
$this->on(KernelEvents::TERMINATE, function (PostResponseEvent $event) use ($callback) {

$request = $event->getRequest();
$response = $event->getResponse();
$middleware = $this['callback_resolver']->resolveCallback($callback);
$this->callbackInvoker->call($middleware, [
// type hints
'Symfony\Component\HttpFoundation\Request' => $request,
'Symfony\Component\HttpFoundation\Response' => $response,
Request::class => $request,
Response::class => $response,
// Silex' default parameter order
0 => $request,
1 => $response,
Expand Down
11 changes: 5 additions & 6 deletions src/CallbackInvoker.php
Expand Up @@ -2,25 +2,24 @@

namespace DI\Bridge\Silex;

use DI\Bridge\Silex\Application;
use Invoker\Invoker;
use Interop\Container\ContainerInterface;
use Invoker\ParameterResolver\ResolverChain;
use Invoker\ParameterResolver\TypeHintResolver;
use Invoker\Invoker;
use Invoker\ParameterResolver\AssociativeArrayResolver;
use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
use Invoker\ParameterResolver\NumericArrayResolver;
use Invoker\ParameterResolver\ResolverChain;
use Invoker\ParameterResolver\TypeHintResolver;

/**
* A subclass of Invoker that always tries to first resolve through provided parameter names, then
* type hints, then through the DI container and finally allows a fallback to a default parameter order
* type hints, then through the DI container and finally allows a fallback to a default parameter order.
*
* @author Felix Becker <f.becker@outlook.com>
*/
class CallbackInvoker extends Invoker
{
/**
* @param ContainerInterface $container the container for injection
* @param ContainerInterface $container The container for injection
*/
public function __construct(ContainerInterface $container)
{
Expand Down
6 changes: 2 additions & 4 deletions src/ConverterListener.php
Expand Up @@ -2,8 +2,7 @@

namespace DI\Bridge\Silex;

use DI\Bridge\Silex\CallbackResolver;
use Interop\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Routing\RouteCollection;

Expand Down Expand Up @@ -36,14 +35,13 @@ public function onKernelController(FilterControllerEvent $event)
$route = $this->routes->get($request->attributes->get('_route'));
if ($route && $converters = $route->getOption('_converters')) {
foreach ($converters as $name => $callback) {

$value = $request->attributes->get($name);
$middleware = $this->callbackResolver->resolveCallback($callback);
$ret = $this->callbackInvoker->call($middleware, [
// parameter name
$name => $value,
// type hints
'Symfony\Component\HttpFoundation\Request' => $request,
Request::class => $request,
// Silex' default parameter order
0 => $value,
1 => $request,
Expand Down
26 changes: 15 additions & 11 deletions src/MiddlewareListener.php
Expand Up @@ -2,10 +2,10 @@

namespace DI\Bridge\Silex;

use DI\Bridge\Silex\Application;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
* Replacement for the Silex MiddlewareListener to allow arbitrary injection into middleware functions.
Expand All @@ -21,7 +21,7 @@ class MiddlewareListener extends \Silex\EventListener\MiddlewareListener

/**
* @param Application $app The application
* @param CallbackInvoker $callbackInvoker The invoker that handles injecting middlewares
* @param CallbackInvoker $callbackInvoker The invoker that handles injecting middleware
*/
public function __construct(Application $app, CallbackInvoker $callbackInvoker)
{
Expand All @@ -38,11 +38,10 @@ public function onKernelRequest(GetResponseEvent $event)
}

foreach ((array) $route->getOption('_before_middlewares') as $callback) {

$middleware = $this->app['callback_resolver']->resolveCallback($callback);
$ret = $this->callbackInvoker->call($middleware, [
// type hints
'Symfony\Component\HttpFoundation\Request' => $request,
Request::class => $request,
// Silex' default parameter order
0 => $request,
1 => $this->app,
Expand All @@ -51,7 +50,10 @@ public function onKernelRequest(GetResponseEvent $event)
if ($ret instanceof Response) {
$event->setResponse($ret);
} elseif (null !== $ret) {
throw new \RuntimeException(sprintf('A before middleware for route "%s" returned an invalid response value. Must return null or an instance of Response.', $routeName));
throw new \RuntimeException(sprintf(
'A before middleware for route "%s" returned an invalid response value. Must return null or an instance of Response.',
$routeName
));
}
}
}
Expand All @@ -66,12 +68,11 @@ public function onKernelResponse(FilterResponseEvent $event)
}

foreach ((array) $route->getOption('_after_middlewares') as $callback) {

$middleware = $this->app['callback_resolver']->resolveCallback($callback);
$ret = $this->callbackInvoker->call($middleware, [
// type hints
'Symfony\Component\HttpFoundation\Request' => $request,
'Symfony\Component\HttpFoundation\Response' => $response,
Request::class => $request,
Response::class => $response,
// Silex' default parameter order
0 => $request,
1 => $response,
Expand All @@ -81,7 +82,10 @@ public function onKernelResponse(FilterResponseEvent $event)
if ($ret instanceof Response) {
$event->setResponse($ret);
} elseif (null !== $ret) {
throw new \RuntimeException(sprintf('An after middleware for route "%s" returned an invalid response value. Must return null or an instance of Response.', $routeName));
throw new \RuntimeException(sprintf(
'An after middleware for route "%s" returned an invalid response value. Must return null or an instance of Response.',
$routeName
));
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions tests/ApplicationTest.php
Expand Up @@ -3,7 +3,11 @@
namespace DI\Bridge\Silex\Test;

use DI\Bridge\Silex\Application;
use DI\Bridge\Silex\CallbackResolver;
use DI\Bridge\Silex\Controller\ControllerResolver;
use DI\Container;
use DI\ContainerBuilder;
use Interop\Container\ContainerInterface;

class ApplicationTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -30,7 +34,7 @@ public function the_application_container_should_be_container_interface()
{
$app = new Application();

$this->assertInstanceOf('Interop\Container\ContainerInterface', $app->getContainer());
$this->assertInstanceOf(ContainerInterface::class, $app->getContainer());
}

/**
Expand All @@ -40,7 +44,7 @@ public function the_application_should_expose_phpdi_container()
{
$app = new Application();

$this->assertInstanceOf('DI\Container', $app->getPhpDi());
$this->assertInstanceOf(Container::class, $app->getPhpDi());
}

/**
Expand All @@ -50,8 +54,8 @@ public function the_controller_resolver_should_be_registered_as_a_service()
{
$app = new Application();

$this->assertInstanceOf('Closure', $app->raw('resolver'));
$this->assertInstanceOf('DI\Bridge\Silex\Controller\ControllerResolver', $app['resolver']);
$this->assertInstanceOf(\Closure::class, $app->raw('resolver'));
$this->assertInstanceOf(ControllerResolver::class, $app['resolver']);
}

/**
Expand All @@ -61,7 +65,7 @@ public function the_callback_resolver_should_be_registered_as_a_service()
{
$app = new Application();

$this->assertInstanceOf('Closure', $app->raw('callback_resolver'));
$this->assertInstanceOf('DI\Bridge\Silex\CallbackResolver', $app['callback_resolver']);
$this->assertInstanceOf(\Closure::class, $app->raw('callback_resolver'));
$this->assertInstanceOf(CallbackResolver::class, $app['callback_resolver']);
}
}
2 changes: 1 addition & 1 deletion tests/CallbackResolverTest.php
Expand Up @@ -58,7 +58,7 @@ public function resolver_must_throw_exception_when_callback_not_found_in_contain
{
$app = new Application();

$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException(\InvalidArgumentException::class);
$app['callback_resolver']->resolveCallback('some.service');
}
}
4 changes: 3 additions & 1 deletion tests/ContainerTest.php
Expand Up @@ -2,6 +2,8 @@

namespace DI\Bridge\Silex\Test;

use Silex\Route;

class ContainerTest extends BaseTestCase
{
/**
Expand Down Expand Up @@ -35,6 +37,6 @@ public function php_di_use_delegate_lookup()
// Get from PHP-DI into Pimple
$container->set('foo', \DI\get('route_class'));

$this->assertEquals('Silex\Route', $container->get('foo'));
$this->assertEquals(Route::class, $container->get('foo'));
}
}
8 changes: 3 additions & 5 deletions tests/ConverterTest.php
Expand Up @@ -2,12 +2,10 @@

namespace DI\Bridge\Silex\Test;

use DI\Bridge\Silex\Application;
use DI\ContainerBuilder;

use stdClass;
use DI\Bridge\Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ConverterTest extends BaseTestCase
{
Expand All @@ -30,7 +28,7 @@ public function should_allow_arbitrary_injection_in_converter()
$request = Request::create('/john?some=param');

$converter = function (Request $req, Application $app, stdClass $someService, $user) use ($application) {
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Request', $req);
$this->assertInstanceOf(Request::class, $req);
$this->assertEquals('param', $req->query->get('some'));
$this->assertEquals($application, $app);
$this->assertInstanceOf('stdClass', $someService);
Expand Down Expand Up @@ -60,7 +58,7 @@ public function should_fall_back_to_silex_default_behaviour()

$converter = function ($user, $req) use ($application) {
$this->assertEquals($user, 'john');
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Request', $req);
$this->assertInstanceOf(Request::class, $req);
$this->assertEquals('param', $req->query->get('some'));
return ['name' => $user];
};
Expand Down
24 changes: 12 additions & 12 deletions tests/FunctionalTest.php
Expand Up @@ -31,7 +31,7 @@ public function should_resolve_controllers_from_the_container()
{
$app = $this->createApplication();

$app->get('/foo', 'DI\Bridge\Silex\Test\Fixture\InvokableController');
$app->get('/foo', Fixture\InvokableController::class);

$response = $app->handle(Request::create('/foo'));
$this->assertEquals('Hello world', $response->getContent());
Expand All @@ -44,7 +44,7 @@ public function should_resolve_array_controllers()
{
$app = $this->createApplication();

$app->get('/foo', ['DI\Bridge\Silex\Test\Fixture\Controller', 'home']);
$app->get('/foo', [Fixture\Controller::class, 'home']);

$response = $app->handle(Request::create('/foo'));
$this->assertEquals('Hello world', $response->getContent());
Expand All @@ -57,7 +57,7 @@ public function should_pass_url_placeholders()
{
$app = $this->createApplication();

$app->get('/{name}', ['DI\Bridge\Silex\Test\Fixture\Controller', 'hello']);
$app->get('/{name}', [Fixture\Controller::class, 'hello']);

$response = $app->handle(Request::create('/john'));
$this->assertEquals('Hello john', $response->getContent());
Expand Down Expand Up @@ -162,11 +162,11 @@ public function should_pass_the_silex_application_based_on_type_hint()
*/
public function should_pass_the_own_application_based_on_type_hint()
{
$app = new \DI\Bridge\Silex\Test\Fixture\Application(null, [
$app = new Fixture\Application(null, [
'foo' => 'bar',
]);

$app->get('/', function (\DI\Bridge\Silex\Test\Fixture\Application $a) {
$app->get('/', function (Fixture\Application $a) {
return $a['foo'];
});

Expand Down Expand Up @@ -196,8 +196,8 @@ public function should_be_able_to_convert_request()
{
$app = $this->createApplication();

$app->get('/{user}', 'DI\Bridge\Silex\Test\Fixture\HelloController')
->convert('user', 'DI\Bridge\Silex\Test\Fixture\InvokableConverter');
$app->get('/{user}', Fixture\HelloController::class)
->convert('user', Fixture\InvokableConverter::class);

$response = $app->handle(Request::create('/PHPDI'));
$this->assertEquals('PHPDI', $response->getContent());
Expand All @@ -210,8 +210,8 @@ public function should_be_able_to_use_invokable_middleware()
{
$app = $this->createApplication();

$app->get('/', 'DI\Bridge\Silex\Test\Fixture\InvokableController')
->before('DI\Bridge\Silex\Test\Fixture\InvokableMiddleware');
$app->get('/', Fixture\InvokableController::class)
->before(Fixture\InvokableMiddleware::class);

$response = $app->handle(Request::create('/'));
$this->assertEquals('Hello from middleware', $response->getContent());
Expand All @@ -224,7 +224,7 @@ public function should_be_able_to_use_invokable_error_listener()
{
$app = $this->createApplication();

$app->error('DI\Bridge\Silex\Test\Fixture\InvokableErrorListener');
$app->error(Fixture\InvokableErrorListener::class);

$response = $app->handle(Request::create('/'));
$this->assertEquals('Sad panda :(', $response->getContent());
Expand All @@ -237,9 +237,9 @@ public function should_be_able_to_use_view_listener()
{
$app = $this->createApplication();

$app->get('/', 'DI\Bridge\Silex\Test\Fixture\InvokableController');
$app->get('/', Fixture\InvokableController::class);

$app->view('DI\Bridge\Silex\Test\Fixture\InvokableViewListener');
$app->view(Fixture\InvokableViewListener::class);

$response = $app->handle(Request::create('/'));
$this->assertEquals('Hello world from mars', $response->getContent());
Expand Down

0 comments on commit 78e24e6

Please sign in to comment.