Skip to content

Commit b732a31

Browse files
committed
v4 support
1 parent df3c116 commit b732a31

File tree

4 files changed

+29
-105
lines changed

4 files changed

+29
-105
lines changed

src/App.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,43 @@
22

33
namespace DI\Bridge\Slim;
44

5-
use DI\ContainerBuilder;
5+
use Invoker\Invoker;
6+
use Invoker\ParameterResolver\AssociativeArrayResolver;
7+
use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
8+
use Invoker\ParameterResolver\DefaultValueResolver;
9+
use Invoker\ParameterResolver\ResolverChain;
10+
use Psr\Container\ContainerInterface;
11+
use Slim\Factory\AppFactory;
12+
use \Invoker\CallableResolver as InvokerCallableResolver;
613

714
/**
815
* Slim application configured with PHP-DI.
916
*
1017
* As you can see, this class is very basic and is only useful to get started quickly.
1118
* You can also very well *not* use it and build the container manually.
1219
*/
13-
class App extends \Slim\App
20+
class App
1421
{
15-
public function __construct()
22+
public static function boot(ContainerInterface $container)
1623
{
17-
$containerBuilder = new ContainerBuilder;
18-
$containerBuilder->addDefinitions(__DIR__ . '/config.php');
19-
$this->configureContainer($containerBuilder);
20-
$container = $containerBuilder->build();
24+
AppFactory::setContainer($container);
25+
$callableResolver = new InvokerCallableResolver($container);
26+
AppFactory::setCallableResolver(new CallableResolver($callableResolver));
2127

22-
parent::__construct($container);
23-
}
28+
$app = AppFactory::create();
2429

25-
/**
26-
* Override this method to configure the container builder.
27-
*
28-
* For example, to load additional configuration files:
29-
*
30-
* protected function configureContainer(ContainerBuilder $builder)
31-
* {
32-
* $builder->addDefinitions(__DIR__ . 'my-config-file.php');
33-
* }
34-
*/
35-
protected function configureContainer(ContainerBuilder $builder)
36-
{
30+
$resolvers = [
31+
// Inject parameters by name first
32+
new AssociativeArrayResolver(),
33+
// Then inject services by type-hints for those that weren't resolved
34+
new TypeHintContainerResolver($container),
35+
// Then fall back on parameters default values for optional route parameters
36+
new DefaultValueResolver(),
37+
];
38+
39+
$invoker = new Invoker(new ResolverChain($resolvers), $container);
40+
$app->getRouteCollector()->setDefaultInvocationStrategy(new ControllerInvoker($invoker));
41+
42+
return $app;
3743
}
3844
}

src/CallableResolver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ public function __construct(\Invoker\CallableResolver $callableResolver)
1818
{
1919
$this->callableResolver = $callableResolver;
2020
}
21-
2221
/**
2322
* {@inheritdoc}
2423
*/
25-
public function resolve($toResolve)
24+
public function resolve($toResolve): callable
2625
{
2726
return $this->callableResolver->resolve($toResolve);
2827
}

src/ControllerInvoker.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public function __construct(InvokerInterface $invoker)
1818
{
1919
$this->invoker = $invoker;
2020
}
21-
2221
/**
2322
* Invoke a route callable.
2423
*
@@ -34,16 +33,14 @@ public function __invoke(
3433
ServerRequestInterface $request,
3534
ResponseInterface $response,
3635
array $routeArguments
37-
) {
36+
): ResponseInterface {
3837
// Inject the request and response by parameter name
3938
$parameters = [
4039
'request' => $request,
4140
'response' => $response,
4241
];
43-
4442
// Inject the route arguments by name
4543
$parameters += $routeArguments;
46-
4744
// Inject the attributes defined on the request
4845
$parameters += $request->getAttributes();
4946

src/config.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)