From 25243a10ecb00a5f1e2a56a7cad529892fd69fc3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 27 Dec 2019 14:02:43 +0100 Subject: [PATCH] Moved common bootstrapping code to run.php script --- bin/cli | 9 +++------ config/container.php | 15 ++++++++++----- config/run.php | 15 +++++++++++++++ module/Core/config/event_dispatcher.config.php | 1 - public/index.php | 10 ++-------- 5 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 config/run.php diff --git a/bin/cli b/bin/cli index 7f512eb0..c185efd3 100755 --- a/bin/cli +++ b/bin/cli @@ -1,10 +1,7 @@ #!/usr/bin/env php get(CliApp::class)->run(); +$run = require __DIR__ . '/../config/run.php'; +$run(true); diff --git a/config/container.php b/config/container.php index 29bc3e28..2ea9dc06 100644 --- a/config/container.php +++ b/config/container.php @@ -10,10 +10,15 @@ chdir(dirname(__DIR__)); require 'vendor/autoload.php'; // This class alias tricks the ConfigAbstractFactory to return Lock\Factory instances even with a different service name -class_alias(Lock\LockFactory::class, 'Shlinkio\Shlink\LocalLockFactory'); +if (! class_exists('Shlinkio\Shlink\LocalLockFactory')) { + class_alias(Lock\LockFactory::class, 'Shlinkio\Shlink\LocalLockFactory'); +} // Build container -$config = require __DIR__ . '/config.php'; -$container = new ServiceManager($config['dependencies']); -$container->setService('config', $config); -return $container; +return (function () { + $config = require __DIR__ . '/config.php'; + $container = new ServiceManager($config['dependencies']); + $container->setService('config', $config); + + return $container; +})(); diff --git a/config/run.php b/config/run.php new file mode 100644 index 00000000..4ea61775 --- /dev/null +++ b/config/run.php @@ -0,0 +1,15 @@ +get($isCli ? CliApp::class : Application::class); + + $app->run(); +}; diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index 8cee1f5c..60869d04 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core; -use GuzzleHttp\ClientInterface; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater; use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface; use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory; diff --git a/public/index.php b/public/index.php index 336f5cc2..78bb412a 100644 --- a/public/index.php +++ b/public/index.php @@ -2,11 +2,5 @@ declare(strict_types=1); -use Psr\Container\ContainerInterface; -use Zend\Expressive\Application; - -(function () { - /** @var ContainerInterface $container */ - $container = include __DIR__ . '/../config/container.php'; - $container->get(Application::class)->run(); -})(); +$run = require __DIR__ . '/../config/run.php'; +$run();