Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded CallableResolver to Advanced interface #71

Merged
merged 3 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/CallableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace DI\Bridge\Slim;

use Slim\Interfaces\CallableResolverInterface;
use Invoker\Exception\NotCallableException;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Interfaces\AdvancedCallableResolverInterface;

/**
* Resolve middleware and route callables using PHP-DI.
*/
class CallableResolver implements CallableResolverInterface
class CallableResolver implements AdvancedCallableResolverInterface
{
/**
* @var \Invoker\CallableResolver
Expand All @@ -18,11 +21,59 @@ public function __construct(\Invoker\CallableResolver $callableResolver)
{
$this->callableResolver = $callableResolver;
}

/**
* {@inheritdoc}
*/
public function resolve($toResolve): callable
{
return $this->callableResolver->resolve($this->translateNotation($toResolve));
}

/**
* {@inheritdoc}
*/
public function resolveRoute($toResolve): callable
{
return $this->resolvePossibleSignature($toResolve, 'handle', RequestHandlerInterface::class);
}

/**
* {@inheritdoc}
*/
public function resolveMiddleware($toResolve): callable
{
return $this->resolvePossibleSignature($toResolve, 'process', MiddlewareInterface::class);
}

/**
* Translate Slim string callable notation ('nameOrKey:method') to PHP-DI notation ('nameOrKey::method').
*/
private function translateNotation($toResolve)
{
if (is_string($toResolve) && preg_match(\Slim\CallableResolver::$callablePattern, $toResolve)) {
$toResolve = str_replace(':', '::', $toResolve);
}

return $toResolve;
}

private function resolvePossibleSignature($toResolve, string $method, string $typeName): callable
{
if (is_string($toResolve)) {
$toResolve = $this->translateNotation($toResolve);

try {
$callable = $this->callableResolver->resolve([$toResolve, $method]);

if (is_array($callable) && $callable[0] instanceof $typeName) {
return $callable;
}
} catch (NotCallableException $e) {
// Fall back to looking for a generic callable.
}
}

return $this->callableResolver->resolve($toResolve);
}
}
89 changes: 89 additions & 0 deletions tests/CallableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace DI\Bridge\Slim\Test;

use DI\Bridge\Slim\Bridge;
use DI\Bridge\Slim\Test\Fixture\UserController;
use DI\Bridge\Slim\Test\Fixture\UserControllerInvokable;
use DI\Bridge\Slim\Test\Fixture\UserControllerPsr;
use DI\Bridge\Slim\Test\Fixture\UserMiddlewarePsr;
use DI\Container;
use PHPUnit\Framework\TestCase;

use Slim\Interfaces\AdvancedCallableResolverInterface;

use function DI\get;


class CallableTest extends TestCase
{
/**
* @test
*/
public function resolves_callable()
{
$app = Bridge::create();
$resolver = $app->getCallableResolver();

/** @var Container $container */
$container = $app->getContainer();
$container->set('controller.user', get(UserController::class));

[$object, $method] = $resolver->resolve(UserController::class . ':dashboard');
$this->assertInstanceOf(UserController::class, $object);
$this->assertEquals('dashboard', $method);

[$object, $method] = $resolver->resolve('controller.user:dashboard');
$this->assertInstanceOf(UserController::class, $object);
$this->assertEquals('dashboard', $method);

[$object, $method] = $resolver->resolve(UserController::class . '::dashboard');
$this->assertInstanceOf(UserController::class, $object);
$this->assertEquals('dashboard', $method);

[$object, $method] = $resolver->resolve([UserController::class, 'dashboard']);
$this->assertInstanceOf(UserController::class, $object);
$this->assertEquals('dashboard', $method);

$resolved = $resolver->resolve(UserControllerInvokable::class);
$this->assertInstanceOf(UserControllerInvokable::class, $resolved);
}

/**
* @test
*/
public function resolves_route()
{
$app = Bridge::create();
/** @var AdvancedCallableResolverInterface $resolver */
$resolver = $app->getCallableResolver();

/** @var Container $container */
$container = $app->getContainer();
$container->set('controller.userpsr', get(UserControllerPsr::class));

[$object, $method] = $resolver->resolveRoute(UserControllerPsr::class);
$this->assertInstanceOf(UserControllerPsr::class, $object);
$this->assertEquals('handle', $method);

[$object, $method] = $resolver->resolveRoute('controller.userpsr');
$this->assertInstanceOf(UserControllerPsr::class, $object);
$this->assertEquals('handle', $method);
}

/**
* @test
*/
public function resolves_middleware()
{
$app = Bridge::create();
/** @var AdvancedCallableResolverInterface $resolver */
$resolver = $app->getCallableResolver();

[$object, $method] = $resolver->resolveMiddleware(UserMiddlewarePsr::class);
$this->assertInstanceOf(UserMiddlewarePsr::class, $object);
$this->assertEquals('process', $method);
}
}
15 changes: 15 additions & 0 deletions tests/Fixture/UserControllerInvokable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace DI\Bridge\Slim\Test\Fixture;

use Psr\Http\Message\ResponseInterface;

class UserControllerInvokable
{
public function __invoke(ResponseInterface $response)
{
$response->getBody()->write('Hello world!');

return $response;
}
}
17 changes: 17 additions & 0 deletions tests/Fixture/UserControllerPsr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace DI\Bridge\Slim\Test\Fixture;

use Laminas\Diactoros\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class UserControllerPsr implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new TextResponse('Hello world!');
}
}
22 changes: 22 additions & 0 deletions tests/Fixture/UserMiddlewarePsr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace DI\Bridge\Slim\Test\Fixture;

use Laminas\Diactoros\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class UserMiddlewarePsr implements MiddlewareInterface
{
/**
* @inheritDoc
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return new TextResponse('Hello ' . $request->getQueryParams()['foo']);
}
}
17 changes: 16 additions & 1 deletion tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace DI\Bridge\Slim\Test;

use DI\Bridge\Slim\Bridge;
use DI\Bridge\Slim\Test\Fixture\UserMiddlewarePsr;
use DI\Bridge\Slim\Test\Mock\RequestFactory;
use Laminas\Diactoros\Response\TextResponse;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\TextResponse;

class MiddlewareTest extends TestCase
{
Expand All @@ -26,4 +27,18 @@ public function invokes_closure_middleware()

$this->assertEquals('Hello matt', $response->getBody()->__toString());
}

/**
* @test
*/
public function invokes_class_name_middleware()
{
$app = Bridge::create();
$app->add(UserMiddlewarePsr::class);
$app->get('/', function () {});

$response = $app->handle(RequestFactory::create('/', 'foo=matt'));

$this->assertEquals('Hello matt', $response->getBody()->__toString());
}
}