Skip to content

Commit

Permalink
Merge pull request #2793 from mnapoli/2778-configure-app-via-container
Browse files Browse the repository at this point in the history
Configure the application via a container
  • Loading branch information
l0gicgate committed Aug 11, 2019
2 parents a62257e + 691878f commit b8d2006
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Slim/Factory/AppFactory.php
Expand Up @@ -89,6 +89,31 @@ public static function create(
);
}

/**
* @param ContainerInterface $container
* @return App
*/
public static function createFromContainer(ContainerInterface $container): App
{
$responseFactory = $container->has(ResponseFactoryInterface::class)
? $container->get(ResponseFactoryInterface::class)
: self::determineResponseFactory();

$callableResolver = $container->has(CallableResolverInterface::class)
? $container->get(CallableResolverInterface::class)
: null;

$routeCollector = $container->has(RouteCollectorInterface::class)
? $container->get(RouteCollectorInterface::class)
: null;

$routeResolver = $container->has(RouteResolverInterface::class)
? $container->get(RouteResolverInterface::class)
: null;

return new App($responseFactory, $container, $callableResolver, $routeCollector, $routeResolver);
}

/**
* @return ResponseFactoryInterface
* @throws RuntimeException
Expand Down
65 changes: 65 additions & 0 deletions tests/Factory/AppFactoryTest.php
Expand Up @@ -228,4 +228,69 @@ public function testResponseAndStreamFactoryIsBeingInjectedInDecoratedResponseFa

$this->assertSame($streamFactoryProphecy->reveal(), $streamFactoryProperty->getValue($response));
}

public function testCreateAppWithContainerUsesContainerDependenciesWhenPresent()
{
$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$callableResolverProphecy = $this->prophesize(CallableResolverInterface::class);
$routeResolverProphecy = $this->prophesize(RouteResolverInterface::class);
$routeParserProphecy = $this->prophesize(RouteParserInterface::class);

$routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class);
$routeCollectorProphecy
->getRouteParser()
->willReturn($routeParserProphecy->reveal())
->shouldBeCalledOnce();

$containerProphecy = $this->prophesize(ContainerInterface::class);

$containerProphecy
->has(ResponseFactoryInterface::class)
->willReturn(true)
->shouldBeCalledOnce();

$containerProphecy
->get(ResponseFactoryInterface::class)
->willReturn($responseFactoryProphecy->reveal())
->shouldBeCalledOnce();

$containerProphecy
->has(CallableResolverInterface::class)
->willReturn(true)
->shouldBeCalledOnce();

$containerProphecy
->get(CallableResolverInterface::class)
->willReturn($callableResolverProphecy->reveal())
->shouldBeCalledOnce();

$containerProphecy
->has(RouteCollectorInterface::class)
->willReturn(true)
->shouldBeCalledOnce();

$containerProphecy
->get(RouteCollectorInterface::class)
->willReturn($routeCollectorProphecy->reveal())
->shouldBeCalledOnce();

$containerProphecy
->has(RouteResolverInterface::class)
->willReturn(true)
->shouldBeCalledOnce();

$containerProphecy
->get(RouteResolverInterface::class)
->willReturn($routeResolverProphecy->reveal())
->shouldBeCalledOnce();

AppFactory::setSlimHttpDecoratorsAutomaticDetection(false);
$app = AppFactory::createFromContainer($containerProphecy->reveal());

$this->assertSame($app->getResponseFactory(), $responseFactoryProphecy->reveal());
$this->assertSame($app->getContainer(), $containerProphecy->reveal());
$this->assertSame($app->getCallableResolver(), $callableResolverProphecy->reveal());
$this->assertSame($app->getRouteCollector(), $routeCollectorProphecy->reveal());
$this->assertSame($app->getRouteResolver(), $routeResolverProphecy->reveal());
}
}

0 comments on commit b8d2006

Please sign in to comment.