Browse Source

Moved common bootstrapping code to run.php script

Alejandro Celaya 4 years ago
parent
commit
25243a10ec
5 changed files with 30 additions and 20 deletions
  1. 3 6
      bin/cli
  2. 10 5
      config/container.php
  3. 15 0
      config/run.php
  4. 0 1
      module/Core/config/event_dispatcher.config.php
  5. 2 8
      public/index.php

+ 3 - 6
bin/cli

@@ -1,10 +1,7 @@
 #!/usr/bin/env php
 <?php
-declare(strict_types=1);
 
-use Psr\Container\ContainerInterface;
-use Symfony\Component\Console\Application as CliApp;
+declare(strict_types=1);
 
-/** @var ContainerInterface $container */
-$container = include __DIR__ . '/../config/container.php';
-$container->get(CliApp::class)->run();
+$run = require __DIR__ . '/../config/run.php';
+$run(true);

+ 10 - 5
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;
+})();

+ 15 - 0
config/run.php

@@ -0,0 +1,15 @@
+<?php
+
+declare(strict_types=1);
+
+use Psr\Container\ContainerInterface;
+use Symfony\Component\Console\Application as CliApp;
+use Zend\Expressive\Application;
+
+return function (bool $isCli = false): void {
+    /** @var ContainerInterface $container */
+    $container = include __DIR__ . '/container.php';
+    $app = $container->get($isCli ? CliApp::class : Application::class);
+
+    $app->run();
+};

+ 0 - 1
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;

+ 2 - 8
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();