-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from fritsvt/slim4
Slim4
- Loading branch information
Showing
15 changed files
with
125 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.