Remove indirect usage of Container.
This commit is contained in:
parent
be0360fb4e
commit
d371e37dc1
1 changed files with 25 additions and 51 deletions
76
src/App.php
76
src/App.php
|
|
@ -133,12 +133,7 @@ class App
|
||||||
}
|
}
|
||||||
$container = $container->build();
|
$container = $container->build();
|
||||||
|
|
||||||
$container->set(Slim\Views\Twig::class, function (ContainerInterface $container) {
|
$container->set(Slim\Views\Twig::class, function (EnvironmentService $environmentService, SessionService $sessionService, Translation\Translator $translator) {
|
||||||
/** @var EnvironmentService $environmentService */
|
|
||||||
$environmentService = $container->get(EnvironmentService::class);
|
|
||||||
/** @var SessionService $sessionService */
|
|
||||||
$sessionService = $container->get(SessionService::class);
|
|
||||||
|
|
||||||
foreach ($this->viewPaths as $i => $viewLocation) {
|
foreach ($this->viewPaths as $i => $viewLocation) {
|
||||||
if (!file_exists($viewLocation) || !is_dir($viewLocation)) {
|
if (!file_exists($viewLocation) || !is_dir($viewLocation)) {
|
||||||
unset($this->viewPaths[$i]);
|
unset($this->viewPaths[$i]);
|
||||||
|
|
@ -180,7 +175,6 @@ class App
|
||||||
$twig->addExtension(new \Kint\Twig\TwigExtension());
|
$twig->addExtension(new \Kint\Twig\TwigExtension());
|
||||||
|
|
||||||
// Add Twig Translate from symfony/twig-bridge
|
// Add Twig Translate from symfony/twig-bridge
|
||||||
$translator = $container->get(Translation\Translator::class);
|
|
||||||
$selectedLanguage = $sessionService->has('Language') ? $sessionService->get('Language') : 'en_US';
|
$selectedLanguage = $sessionService->has('Language') ? $sessionService->get('Language') : 'en_US';
|
||||||
$twig->addExtension(new SymfonyTwigExtensions\TranslationExtension($translator));
|
$twig->addExtension(new SymfonyTwigExtensions\TranslationExtension($translator));
|
||||||
$twig->offsetSet('language', $translator->trans($selectedLanguage));
|
$twig->offsetSet('language', $translator->trans($selectedLanguage));
|
||||||
|
|
@ -194,14 +188,11 @@ class App
|
||||||
});
|
});
|
||||||
|
|
||||||
// This is required as some plugins for Slim expect there to be a twig available as "view"
|
// This is required as some plugins for Slim expect there to be a twig available as "view"
|
||||||
$container->set('view', function (ContainerInterface $container) {
|
$container->set('view', function (Slim\Views\Twig $twig) {
|
||||||
return $container->get(Slim\Views\Twig::class);
|
return $twig;
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(Translation\Translator::class, function (ContainerInterface $container) {
|
$container->set(Translation\Translator::class, function (SessionService $sessionService) {
|
||||||
/** @var SessionService $sessionService */
|
|
||||||
$sessionService = $container->get(SessionService::class);
|
|
||||||
|
|
||||||
$selectedLanguage = $sessionService->has('Language') ? $sessionService->get('Language') : 'en_US';
|
$selectedLanguage = $sessionService->has('Language') ? $sessionService->get('Language') : 'en_US';
|
||||||
|
|
||||||
$translator = new Translation\Translator($selectedLanguage);
|
$translator = new Translation\Translator($selectedLanguage);
|
||||||
|
|
@ -221,18 +212,18 @@ class App
|
||||||
return $translator;
|
return $translator;
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(EnvironmentService::class, function (ContainerInterface $container) {
|
$container->set(EnvironmentService::class, function () {
|
||||||
return new EnvironmentService();
|
return new EnvironmentService();
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(ConfigurationService::class, function (ContainerInterface $container) use ($app) {
|
$container->set(ConfigurationService::class, function (EnvironmentService $environmentService) use ($app) {
|
||||||
return new ConfigurationService(
|
return new ConfigurationService(
|
||||||
$app,
|
$app,
|
||||||
$container->get(EnvironmentService::class)
|
$environmentService
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(\Faker\Generator::class, function (ContainerInterface $c) {
|
$container->set(\Faker\Generator::class, function () {
|
||||||
$faker = FakerFactory::create();
|
$faker = FakerFactory::create();
|
||||||
$faker->addProvider(new Provider\Base($faker));
|
$faker->addProvider(new Provider\Base($faker));
|
||||||
$faker->addProvider(new Provider\DateTime($faker));
|
$faker->addProvider(new Provider\DateTime($faker));
|
||||||
|
|
@ -247,7 +238,7 @@ class App
|
||||||
return $faker;
|
return $faker;
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(CachePoolChain::class, function (ContainerInterface $c) {
|
$container->set(CachePoolChain::class, function (\Redis $redis) {
|
||||||
$caches = [];
|
$caches = [];
|
||||||
|
|
||||||
// If apc/apcu present, add it to the pool
|
// If apc/apcu present, add it to the pool
|
||||||
|
|
@ -258,53 +249,40 @@ class App
|
||||||
}
|
}
|
||||||
|
|
||||||
// If Redis is configured, add it to the pool.
|
// If Redis is configured, add it to the pool.
|
||||||
$caches[] = new RedisCachePool($c->get(\Redis::class));
|
$caches[] = new RedisCachePool($redis);
|
||||||
$caches[] = new ArrayCachePool();
|
$caches[] = new ArrayCachePool();
|
||||||
|
|
||||||
return new CachePoolChain($caches);
|
return new CachePoolChain($caches);
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set('MonologFormatter', function (ContainerInterface $c) {
|
$container->set('MonologFormatter', function (EnvironmentService $environmentService) {
|
||||||
/** @var Services\EnvironmentService $environment */
|
return new LineFormatter(
|
||||||
$environment = $c->get(Services\EnvironmentService::class);
|
|
||||||
|
|
||||||
return
|
|
||||||
new LineFormatter(
|
|
||||||
// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%"
|
// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%"
|
||||||
$environment->get('MONOLOG_FORMAT', '[%datetime%] %channel%.%level_name%: %message% %context% %extra%')."\n",
|
$environmentService->get('MONOLOG_FORMAT', '[%datetime%] %channel%.%level_name%: %message% %context% %extra%')."\n",
|
||||||
'Y n j, g:i a'
|
'Y n j, g:i a'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(Logger::class, function (ContainerInterface $c) {
|
$container->set(Logger::class, function (ConfigurationService $configurationService) {
|
||||||
/** @var ConfigurationService $configuration */
|
$monolog = new Logger($configurationService->get(ConfigurationService::KEY_APP_NAME));
|
||||||
$configuration = $c->get(ConfigurationService::class);
|
|
||||||
|
|
||||||
$monolog = new Logger($configuration->get(ConfigurationService::KEY_APP_NAME));
|
|
||||||
$monolog->pushHandler(new ErrorLogHandler(), Logger::DEBUG);
|
$monolog->pushHandler(new ErrorLogHandler(), Logger::DEBUG);
|
||||||
$monolog->pushProcessor(new PsrLogMessageProcessor());
|
$monolog->pushProcessor(new PsrLogMessageProcessor());
|
||||||
|
|
||||||
return $monolog;
|
return $monolog;
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(DebugBar::class, function (ContainerInterface $container) {
|
$container->set(DebugBar::class, function (Logger $logger) {
|
||||||
$debugBar = new StandardDebugBar();
|
$debugBar = new StandardDebugBar();
|
||||||
/** @var Logger $logger */
|
|
||||||
$logger = $container->get(Logger::class);
|
|
||||||
$debugBar->addCollector(new MonologCollector($logger));
|
$debugBar->addCollector(new MonologCollector($logger));
|
||||||
|
|
||||||
return $debugBar;
|
return $debugBar;
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(\Middlewares\Debugbar::class, function (ContainerInterface $container) {
|
$container->set(\Middlewares\Debugbar::class, function (DebugBar $debugBar) {
|
||||||
$debugBar = $container->get(DebugBar::class);
|
|
||||||
|
|
||||||
return new \Middlewares\Debugbar($debugBar);
|
return new \Middlewares\Debugbar($debugBar);
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(\Redis::class, function (ContainerInterface $container) {
|
$container->set(\Redis::class, function (EnvironmentService $environmentService) {
|
||||||
$environmentService = $container->get(EnvironmentService::class);
|
|
||||||
|
|
||||||
$redis = new Redis();
|
$redis = new Redis();
|
||||||
$redis->connect(
|
$redis->connect(
|
||||||
$environmentService->get('REDIS_HOST', 'redis'),
|
$environmentService->get('REDIS_HOST', 'redis'),
|
||||||
|
|
@ -314,23 +292,19 @@ class App
|
||||||
return $redis;
|
return $redis;
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(SessionService::class, function (ContainerInterface $container) {
|
$container->set(SessionService::class, function (\Redis $redis) {
|
||||||
return new SessionService(
|
return new SessionService($redis);
|
||||||
$container->get(\Redis::class)
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(Databases::class, function (ContainerInterface $container) {
|
$container->set(Databases::class, function (ConfigurationService $configurationService) {
|
||||||
return new Databases(
|
return new Databases($configurationService);
|
||||||
$container->get(ConfigurationService::class)
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->set(Laminator::class, function (ContainerInterface $container) {
|
$container->set(Laminator::class, function (ConfigurationService $configurationService, Databases $databases) {
|
||||||
return new Laminator(
|
return new Laminator(
|
||||||
APP_ROOT,
|
APP_ROOT,
|
||||||
$container->get(ConfigurationService::class),
|
$configurationService,
|
||||||
$container->get(Databases::class)
|
$databases
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue