Skip to content

Commit 16cb061

Browse files
author
Ashura
committed
Automatically add Slim\App instance to Container
1 parent 3249b0d commit 16cb061

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Bridge.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public static function create(ContainerInterface $container = null): App
3131
$container->set(CallableResolverInterface::class, new CallableResolver($callableResolver));
3232
$app = AppFactory::createFromContainer($container);
3333

34+
$container->set(App::class, $app);
35+
3436
$controllerInvoker = self::createControllerInvoker($container);
3537
$app->getRouteCollector()->setDefaultInvocationStrategy($controllerInvoker);
3638

tests/ApplicationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use DI\Bridge\Slim\Bridge;
66
use DI\Bridge\Slim\Test\Mock\RequestFactory;
77
use PHPUnit\Framework\TestCase;
8+
use DI\ContainerBuilder;
9+
use Slim\App;
10+
use Psr\Http\Message\ResponseInterface;
811

912
class ApplicationTest extends TestCase
1013
{
@@ -24,4 +27,26 @@ public function runs()
2427

2528
$this->assertTrue($called);
2629
}
30+
31+
/**
32+
* @test
33+
*/
34+
public function register_app_instance_to_container()
35+
{
36+
$app = Bridge::create();
37+
38+
$instance = null;
39+
40+
$app->get('/', function (App $app, ResponseInterface $response) use(&$instance) {
41+
$instance = $app;
42+
return $response;
43+
});
44+
45+
$app->handle(RequestFactory::create());
46+
47+
$this->assertInstanceOf(
48+
App::class,
49+
$instance
50+
);
51+
}
2752
}

0 commit comments

Comments
 (0)