Skip to content

Commit cfdb93d

Browse files
authored
Merge pull request #49 from fritsvt/slim4
Slim4
2 parents 77186fe + 34151be commit cfdb93d

15 files changed

+125
-348
lines changed

.travis.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
language: php
22

3+
notifications:
4+
email:
5+
on_success: never
6+
37
php:
4-
- 7.0
58
- 7.1
69
- 7.2
710
- 7.3
811

912
matrix:
1013
include:
11-
- php: 7.0
14+
- php: 7.1
1215
env: dependencies=lowest
1316

17+
cache:
18+
directories:
19+
- $HOME/.composer/cache
20+
1421
sudo: false
1522

1623
before_script:

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
}
1515
},
1616
"require": {
17-
"php": "~7.0",
17+
"php": "~7.1",
1818
"php-di/php-di": "^6.0.0",
1919
"php-di/invoker": "^2.0.0",
20-
"slim/slim": "^3.9.0"
20+
"slim/slim": "^4.2.0"
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit": "~6.0"
23+
"phpunit/phpunit": "~6.0",
24+
"zendframework/zend-diactoros": "^2.1"
2425
}
2526
}

src/App.php

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

src/Bridge.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DI\Bridge\Slim;
4+
5+
use DI\Container;
6+
use Invoker\Invoker;
7+
use Invoker\ParameterResolver\AssociativeArrayResolver;
8+
use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
9+
use Invoker\ParameterResolver\DefaultValueResolver;
10+
use Invoker\ParameterResolver\ResolverChain;
11+
use Psr\Container\ContainerInterface;
12+
use Slim\App;
13+
use Slim\Factory\AppFactory;
14+
use \Invoker\CallableResolver as InvokerCallableResolver;
15+
use Slim\Interfaces\CallableResolverInterface;
16+
17+
/**
18+
* This factory creates a Slim application correctly configured with PHP-DI.
19+
*
20+
* To use this, replace `Slim\Factory\AppFactory::create()`
21+
* with `DI\Bridge\Slim\Bridge::create()`.
22+
*/
23+
class Bridge
24+
{
25+
public static function create(ContainerInterface $container = null): App
26+
{
27+
$container = $container ?: new Container;
28+
29+
$callableResolver = new InvokerCallableResolver($container);
30+
31+
$container->set(CallableResolverInterface::class, new CallableResolver($callableResolver));
32+
$app = AppFactory::createFromContainer($container);
33+
34+
$controllerInvoker = self::createControllerInvoker($container);
35+
$app->getRouteCollector()->setDefaultInvocationStrategy($controllerInvoker);
36+
37+
return $app;
38+
}
39+
40+
private static function createControllerInvoker(ContainerInterface $container): ControllerInvoker
41+
{
42+
$resolvers = [
43+
// Inject parameters by name first
44+
new AssociativeArrayResolver(),
45+
// Then inject services by type-hints for those that weren't resolved
46+
new TypeHintContainerResolver($container),
47+
// Then fall back on parameters default values for optional route parameters
48+
new DefaultValueResolver(),
49+
];
50+
51+
$invoker = new Invoker(new ResolverChain($resolvers), $container);
52+
53+
return new ControllerInvoker($invoker);
54+
}
55+
}

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.

tests/ApplicationTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace DI\Bridge\Slim\Test;
44

5-
use DI\Bridge\Slim\App;
5+
use DI\Bridge\Slim\Bridge;
66
use DI\Bridge\Slim\Test\Mock\RequestFactory;
77
use PHPUnit\Framework\TestCase;
8-
use Slim\Http\Response;
98

109
class ApplicationTest extends TestCase
1110
{
@@ -14,14 +13,15 @@ class ApplicationTest extends TestCase
1413
*/
1514
public function runs()
1615
{
17-
$app = new App;
16+
$app = Bridge::create();
1817

1918
$called = false;
20-
$app->get('/', function () use (&$called) {
19+
$app->get('/', function ($request, $response) use (&$called) {
2120
$called = true;
21+
return $response;
2222
});
23+
$app->handle(RequestFactory::create());
2324

24-
$app->callMiddlewareStack(RequestFactory::create(), new Response);
2525
$this->assertTrue($called);
2626
}
2727
}

tests/ContainerTest.php

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

0 commit comments

Comments
 (0)