|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Runner; |
| 6 | + |
| 7 | +use Error; |
| 8 | +use ErrorException; |
| 9 | +use Exception; |
| 10 | +use Psr\Container\ContainerInterface; |
| 11 | +use Yiisoft\Config\Config; |
| 12 | +use Yiisoft\Di\Container; |
| 13 | +use Yiisoft\Definitions\Exception\CircularReferenceException; |
| 14 | +use Yiisoft\Definitions\Exception\InvalidConfigException; |
| 15 | +use Yiisoft\Definitions\Exception\NotFoundException; |
| 16 | +use Yiisoft\Definitions\Exception\NotInstantiableException; |
| 17 | +use Yiisoft\Yii\Console\Application; |
| 18 | +use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput; |
| 19 | + |
| 20 | +final class ConsoleApplicationRunner |
| 21 | +{ |
| 22 | + private bool $debug; |
| 23 | + private ?string $environment; |
| 24 | + |
| 25 | + public function __construct(bool $debug, ?string $environment) |
| 26 | + { |
| 27 | + $this->debug = $debug; |
| 28 | + $this->environment = $environment; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @throws CircularReferenceException|ErrorException|Exception|InvalidConfigException|NotFoundException |
| 33 | + * @throws NotInstantiableException |
| 34 | + */ |
| 35 | + public function run(): void |
| 36 | + { |
| 37 | + $config = new Config( |
| 38 | + dirname(__DIR__, 2), |
| 39 | + '/config/packages', // Configs path. |
| 40 | + $this->environment, |
| 41 | + [ |
| 42 | + 'params', |
| 43 | + 'events', |
| 44 | + 'events-web', |
| 45 | + 'events-console', |
| 46 | + ], |
| 47 | + ); |
| 48 | + |
| 49 | + $container = new Container( |
| 50 | + $config->get('console'), |
| 51 | + $config->get('providers-console'), |
| 52 | + [], |
| 53 | + $this->debug, |
| 54 | + $config->get('delegates-console') |
| 55 | + ); |
| 56 | + |
| 57 | + $container = $container->get(ContainerInterface::class); |
| 58 | + |
| 59 | + // Run bootstrap |
| 60 | + $this->runBootstrap($container, $config->get('bootstrap-console')); |
| 61 | + |
| 62 | + /** @var Application */ |
| 63 | + $application = $container->get(Application::class); |
| 64 | + $exitCode = 1; |
| 65 | + |
| 66 | + try { |
| 67 | + $application->start(); |
| 68 | + $exitCode = $application->run(null, new ConsoleBufferedOutput()); |
| 69 | + } catch (Error $error) { |
| 70 | + $application->renderThrowable($error, new ConsoleBufferedOutput()); |
| 71 | + } finally { |
| 72 | + $application->shutdown($exitCode); |
| 73 | + exit($exitCode); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private function runBootstrap(ContainerInterface $container, array $bootstrapList): void |
| 78 | + { |
| 79 | + (new BootstrapRunner($container, $bootstrapList))->run(); |
| 80 | + } |
| 81 | +} |
0 commit comments