Skip to content

Commit

Permalink
Merge pull request #49 from fritsvt/slim4
Browse files Browse the repository at this point in the history
Slim4
  • Loading branch information
mnapoli committed Sep 8, 2019
2 parents 77186fe + 34151be commit cfdb93d
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 348 deletions.
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
language: php

notifications:
email:
on_success: never

php:
- 7.0
- 7.1
- 7.2
- 7.3

matrix:
include:
- php: 7.0
- php: 7.1
env: dependencies=lowest

cache:
directories:
- $HOME/.composer/cache

sudo: false

before_script:
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
}
},
"require": {
"php": "~7.0",
"php": "~7.1",
"php-di/php-di": "^6.0.0",
"php-di/invoker": "^2.0.0",
"slim/slim": "^3.9.0"
"slim/slim": "^4.2.0"
},
"require-dev": {
"phpunit/phpunit": "~6.0"
"phpunit/phpunit": "~6.0",
"zendframework/zend-diactoros": "^2.1"
}
}
38 changes: 0 additions & 38 deletions src/App.php

This file was deleted.

55 changes: 55 additions & 0 deletions src/Bridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);

namespace DI\Bridge\Slim;

use DI\Container;
use Invoker\Invoker;
use Invoker\ParameterResolver\AssociativeArrayResolver;
use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
use Invoker\ParameterResolver\DefaultValueResolver;
use Invoker\ParameterResolver\ResolverChain;
use Psr\Container\ContainerInterface;
use Slim\App;
use Slim\Factory\AppFactory;
use \Invoker\CallableResolver as InvokerCallableResolver;
use Slim\Interfaces\CallableResolverInterface;

/**
* This factory creates a Slim application correctly configured with PHP-DI.
*
* To use this, replace `Slim\Factory\AppFactory::create()`
* with `DI\Bridge\Slim\Bridge::create()`.
*/
class Bridge
{
public static function create(ContainerInterface $container = null): App
{
$container = $container ?: new Container;

$callableResolver = new InvokerCallableResolver($container);

$container->set(CallableResolverInterface::class, new CallableResolver($callableResolver));
$app = AppFactory::createFromContainer($container);

$controllerInvoker = self::createControllerInvoker($container);
$app->getRouteCollector()->setDefaultInvocationStrategy($controllerInvoker);

return $app;
}

private static function createControllerInvoker(ContainerInterface $container): ControllerInvoker
{
$resolvers = [
// Inject parameters by name first
new AssociativeArrayResolver(),
// Then inject services by type-hints for those that weren't resolved
new TypeHintContainerResolver($container),
// Then fall back on parameters default values for optional route parameters
new DefaultValueResolver(),
];

$invoker = new Invoker(new ResolverChain($resolvers), $container);

return new ControllerInvoker($invoker);
}
}
3 changes: 1 addition & 2 deletions src/CallableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public function __construct(\Invoker\CallableResolver $callableResolver)
{
$this->callableResolver = $callableResolver;
}

/**
* {@inheritdoc}
*/
public function resolve($toResolve)
public function resolve($toResolve): callable
{
return $this->callableResolver->resolve($toResolve);
}
Expand Down
5 changes: 1 addition & 4 deletions src/ControllerInvoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function __construct(InvokerInterface $invoker)
{
$this->invoker = $invoker;
}

/**
* Invoke a route callable.
*
Expand All @@ -34,16 +33,14 @@ public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
) {
): ResponseInterface {
// Inject the request and response by parameter name
$parameters = [
'request' => $request,
'response' => $response,
];

// Inject the route arguments by name
$parameters += $routeArguments;

// Inject the attributes defined on the request
$parameters += $request->getAttributes();

Expand Down
78 changes: 0 additions & 78 deletions src/config.php

This file was deleted.

10 changes: 5 additions & 5 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace DI\Bridge\Slim\Test;

use DI\Bridge\Slim\App;
use DI\Bridge\Slim\Bridge;
use DI\Bridge\Slim\Test\Mock\RequestFactory;
use PHPUnit\Framework\TestCase;
use Slim\Http\Response;

class ApplicationTest extends TestCase
{
Expand All @@ -14,14 +13,15 @@ class ApplicationTest extends TestCase
*/
public function runs()
{
$app = new App;
$app = Bridge::create();

$called = false;
$app->get('/', function () use (&$called) {
$app->get('/', function ($request, $response) use (&$called) {
$called = true;
return $response;
});
$app->handle(RequestFactory::create());

$app->callMiddlewareStack(RequestFactory::create(), new Response);
$this->assertTrue($called);
}
}
59 changes: 0 additions & 59 deletions tests/ContainerTest.php

This file was deleted.

0 comments on commit cfdb93d

Please sign in to comment.