Skip to content
Permalink
Browse files

Merge branch '4x-request-handler-non-handle' into 4.x

Closes #2578
  • Loading branch information...
akrabat committed Feb 22, 2019
2 parents ce4c651 + d86d6eb commit 5311e952552dcd3c8c49a363c438649031965783
@@ -37,7 +37,7 @@ public function __construct(ContainerInterface $container = null)
}
/**
* Resolve toResolve into a closure that that the router can dispatch.
* Resolve toResolve into a callable that the router can dispatch.
*
* If toResolve is of the format 'class:method', then try to extract 'class'
* from the container otherwise instantiate it and then dispatch 'method'.
@@ -54,7 +54,8 @@ public function resolve($toResolve): callable
if (!is_callable($toResolve) && is_string($toResolve)) {
$class = $toResolve;
$method = '__invoke';
$instance = null;
$method = null;
// check for slim callable as "class:method"
$callablePattern = '!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!';
@@ -64,18 +65,21 @@ public function resolve($toResolve): callable
}
if ($this->container instanceof ContainerInterface && $this->container->has($class)) {
$resolved = [$this->container->get($class), $method];
$instance = $this->container->get($class);
} else {
if (!class_exists($class)) {
throw new RuntimeException(sprintf('Callable %s does not exist', $class));
}
$resolved = [new $class($this->container), $method];
$instance = new $class($this->container);
}
// For a class that implements RequestHandlerInterface, we will call handle()
if ($resolved[0] instanceof RequestHandlerInterface) {
$resolved[1] = 'handle';
// if no method has been specified explicitly
if ($instance instanceof RequestHandlerInterface && $method === null) {
$method = 'handle';
}
$resolved = [$instance, $method ?? '__invoke'];
}
if ($resolved instanceof RequestHandlerInterface) {
@@ -22,7 +22,7 @@
interface CallableResolverInterface
{
/**
* Invoke the resolved callable.
* Resolve $toResolve into a callable
*
* @param mixed $toResolve
* @return callable
@@ -151,6 +151,16 @@ public function testObjPsrRequestHandlerClassInContainer()
$this->assertEquals("1", RequestHandlerTest::$CalledCount);
}
public function testResolutionToAPsrRequestHandlerClassWithCustomMethod()
{
$request = $this->createServerRequest('/', 'GET');
$resolver = new CallableResolver(); // No container injected
$callable = $resolver->resolve(RequestHandlerTest::class . ':custom');
$this->assertInternalType('array', $callable);
$this->assertInstanceOf(RequestHandlerTest::class, $callable[0]);
$this->assertEquals('custom', $callable[1]);
}
/**
* @expectedException \RuntimeException
*/
@@ -42,4 +42,9 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
return $response;
}
public function custom(ServerRequestInterface $request) : ResponseInterface
{
return $responseFactory->createResponse();
}
}

0 comments on commit 5311e95

Please sign in to comment.
You can’t perform that action at this time.