Skip to content

Commit

Permalink
Améliore le conteneur
Browse files Browse the repository at this point in the history
  • Loading branch information
Donov4n committed May 2, 2021
1 parent e0b4bcd commit 602a944
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
35 changes: 6 additions & 29 deletions server/src/App/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
namespace Robert2\API;

use DI\Container;
use DI\ContainerBuilder;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Robert2\API\Config\Config;
use Robert2\API\Errors\ErrorHandler;
use Robert2\API\Services\I18n;
use Robert2\API\Services\View;
use Slim\Http\Response;
use Slim\Exception\HttpNotFoundException;
use Slim\Factory\AppFactory;
use Slim\Http\Response;
use Slim\Routing\RouteCollectorProxy;
use Slim\Routing\RouteContext;

Expand Down Expand Up @@ -184,39 +183,17 @@ protected function configureErrorHandlers()

protected static function createContainer()
{
$container = new Container();
$builder = new ContainerBuilder();
$builder->addDefinitions(CONFIG_FOLDER . DS . 'definitions.php');

$container = $builder->build();

//
// - Settings
//

$container->set('settings', Config::getSettings());

//
// - Http Cache Provider
//

$container->set('cache', function () {
return new \Slim\HttpCache\CacheProvider();
});

//
// - Services
//

$container->set('i18n', I18n::class);

$container->set('logger', function () use ($container) {
$settings = $container->get('settings')['logger'] ?? [];
return new Services\Logger($settings);
});

$container->set('auth', new Services\Auth([
new Services\Auth\JWT,
]));

$container->set('view', View::class);

return $container;
}
}
30 changes: 30 additions & 0 deletions server/src/App/Config/definitions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

use function DI\get;
use Psr\Container\ContainerInterface;
use Robert2\API\Services;

return [
'logger' => function (ContainerInterface $container) {
$settings = $container->get('settings')['logger'] ?? [];
return new Services\Logger($settings);
},

'auth' => function () {
return new Services\Auth([
new Services\Auth\JWT
]);
},

//
// - Aliases
//

'cache' => get(\Slim\HttpCache\CacheProvider::class),
'i18n' => get(Services\I18n::class),
'view' => get(Services\View::class),

Services\Auth::class => get('auth'),
Services\Logger::class => get('logger'),
];

0 comments on commit 602a944

Please sign in to comment.