|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Runtime; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Application; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Input\ArgvInput; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Symfony\Component\Dotenv\Dotenv; |
| 22 | +use Symfony\Component\ErrorHandler\Debug; |
| 23 | +use Symfony\Component\ErrorHandler\ErrorHandler; |
| 24 | +use Symfony\Component\HttpFoundation\Request; |
| 25 | +use Symfony\Component\HttpFoundation\Response; |
| 26 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 27 | +use Symfony\Component\HttpKernel\TerminableInterface; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Nicolas Grekas <p@tchwork.com> |
| 31 | + */ |
| 32 | +class SymfonyRuntime implements RuntimeInterface |
| 33 | +{ |
| 34 | + private $request; |
| 35 | + private $input; |
| 36 | + private $output; |
| 37 | + private $application; |
| 38 | + private $command; |
| 39 | + |
| 40 | + public function __construct(array $options = []) |
| 41 | + { |
| 42 | + if (isset($_SERVER['argv']) && class_exists(ArgvInput::class)) { |
| 43 | + $this->getInput(); |
| 44 | + } |
| 45 | + |
| 46 | + if ($path = $options['dotenv'] ?? true) { |
| 47 | + if (!class_exists(Dotenv::class)) { |
| 48 | + throw new \LogicException(sprintf('You cannot use "%s" as the Dotenv component is not installed. Try running "composer require symfony/dotenv".', __CLASS__)); |
| 49 | + } |
| 50 | + |
| 51 | + (new Dotenv())->bootEnv(\is_string($path) ? $options['dotenv'] : (\dirname(__DIR__, 3).'/.env')); |
| 52 | + } |
| 53 | + |
| 54 | + if (!class_exists(ErrorHandler::class)) { |
| 55 | + throw new \LogicException(sprintf('You cannot use "%s" as the ErrorHandler component is not installed. Try running "composer require symfony/error-handler".', __CLASS__)); |
| 56 | + } |
| 57 | + |
| 58 | + if ($_SERVER['APP_DEBUG']) { |
| 59 | + umask(0000); |
| 60 | + Debug::enable(); |
| 61 | + } else { |
| 62 | + ErrorHandler::register(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public function getApp(\Closure $app): \Closure |
| 67 | + { |
| 68 | + $arguments = []; |
| 69 | + |
| 70 | + try { |
| 71 | + foreach ((new \ReflectionFunction($app))->getParameters() as $parameter) { |
| 72 | + $arguments[] = $this->getArgument($parameter); |
| 73 | + } |
| 74 | + } catch (\InvalidArgumentException $e) { |
| 75 | + if (!$parameter->isOptional()) { |
| 76 | + throw $e; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (!$arguments) { |
| 81 | + return $app; |
| 82 | + } |
| 83 | + |
| 84 | + return static function () use ($app, $arguments) { |
| 85 | + return $app(...$arguments); |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + public function getMain(object $app): \Closure |
| 90 | + { |
| 91 | + if ($app instanceof HttpKernelInterface) { |
| 92 | + $request = $this->request ?? $this->request = Request::createFromGlobals(); |
| 93 | + |
| 94 | + return static function () use ($app, $request): int { |
| 95 | + $response = $app->handle($request); |
| 96 | + $response->send(); |
| 97 | + |
| 98 | + if ($app instanceof TerminableInterface) { |
| 99 | + $app->terminate($request, $response); |
| 100 | + } |
| 101 | + |
| 102 | + return 0; |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + if ($app instanceof Response) { |
| 107 | + return static function () use ($app): int { |
| 108 | + $app->send(); |
| 109 | + |
| 110 | + return 0; |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + if ($app instanceof Command) { |
| 115 | + $application = $this->application ?? $this->application = new Application(); |
| 116 | + $main = $this->getMain($application); |
| 117 | + |
| 118 | + return static function () use ($app, $application, $main): int { |
| 119 | + $application->setName($app->getName() ?: $application->getName()); |
| 120 | + |
| 121 | + if (!$app->getName() || !$application->has($app->getName())) { |
| 122 | + $app->setName($_SERVER['argv'][0]); |
| 123 | + $application->add($app); |
| 124 | + } |
| 125 | + |
| 126 | + $application->setDefaultCommand($app->getName(), true); |
| 127 | + |
| 128 | + return $main(); |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + if ($app instanceof Application) { |
| 133 | + if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) { |
| 134 | + echo 'Warning: The console should be invoked via the CLI version of PHP, not the '.\PHP_SAPI.' SAPI'.\PHP_EOL; |
| 135 | + } |
| 136 | + |
| 137 | + set_time_limit(0); |
| 138 | + $input = $this->getInput(); |
| 139 | + |
| 140 | + return static function () use ($app, $input): int { |
| 141 | + $definition = $app->getDefinition(); |
| 142 | + |
| 143 | + if (!$definition->hasOption('env')) { |
| 144 | + $definition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $_SERVER['APP_ENV'])); |
| 145 | + } |
| 146 | + |
| 147 | + if (!$definition->hasOption('no-debug')) { |
| 148 | + $definition->addOption(new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.')); |
| 149 | + } |
| 150 | + |
| 151 | + return $app->run($input); |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + throw new \LogicException(sprintf('Cannot handle return value of type "%s".', \get_class($app))); |
| 156 | + } |
| 157 | + |
| 158 | + protected function getArgument(\ReflectionParameter $parameter) |
| 159 | + { |
| 160 | + switch ($parameter->getType()->getName()) { |
| 161 | + case 'array': |
| 162 | + if ('context' !== $parameter->name) { |
| 163 | + break; |
| 164 | + } |
| 165 | + |
| 166 | + return $_SERVER; |
| 167 | + |
| 168 | + case Request::class: |
| 169 | + return $this->request ?? $this->request = Request::createFromGlobals(); |
| 170 | + |
| 171 | + case InputInterface::class: |
| 172 | + return $this->getInput(); |
| 173 | + |
| 174 | + case OutputInterface::class: |
| 175 | + return $this->output ?? $this->output = new ConsoleOutput(); |
| 176 | + |
| 177 | + case Application::class: |
| 178 | + return $this->application ?? $this->application = new Application(); |
| 179 | + |
| 180 | + case Command::class: |
| 181 | + return $this->command ?? $this->command = new Command(); |
| 182 | + } |
| 183 | + |
| 184 | + throw new \InvalidArgumentException(sprintf('Cannot resolve argument "%s $%s".', $parameter->getType()->getName(), $parameter->name)); |
| 185 | + } |
| 186 | + |
| 187 | + private function getInput(): ArgvInput |
| 188 | + { |
| 189 | + if (null !== $this->input) { |
| 190 | + return $this->input; |
| 191 | + } |
| 192 | + |
| 193 | + $input = new ArgvInput(); |
| 194 | + |
| 195 | + if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) { |
| 196 | + putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env); |
| 197 | + } |
| 198 | + |
| 199 | + if ($input->hasParameterOption('--no-debug', true)) { |
| 200 | + putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'); |
| 201 | + } |
| 202 | + |
| 203 | + return $this->input = $input; |
| 204 | + } |
| 205 | +} |
0 commit comments