-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSymfonyRuntime.php
228 lines (195 loc) · 9 KB
/
SymfonyRuntime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Runtime;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Runtime\Internal\MissingDotenv;
use Symfony\Component\Runtime\Internal\SymfonyErrorHandler;
use Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner;
use Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner;
use Symfony\Component\Runtime\Runner\Symfony\ResponseRunner;
// Help opcache.preload discover always-needed symbols
class_exists(MissingDotenv::class, false) || class_exists(Dotenv::class) || class_exists(MissingDotenv::class);
/**
* Knows the basic conventions to run Symfony apps.
*
* In addition to the options managed by GenericRuntime, it accepts the following options:
* - "env" to define the name of the environment the app runs in;
* - "disable_dotenv" to disable looking for .env files;
* - "dotenv_path" to define the path of dot-env files - defaults to ".env";
* - "prod_envs" to define the names of the production envs - defaults to ["prod"];
* - "test_envs" to define the names of the test envs - defaults to ["test"];
* - "use_putenv" to tell Dotenv to set env vars using putenv() (NOT RECOMMENDED.)
* - "dotenv_overload" to tell Dotenv to override existing vars
*
* When the "debug" / "env" options are not defined, they will fallback to the
* "APP_DEBUG" / "APP_ENV" environment variables, and to the "--env|-e" / "--no-debug"
* command line arguments if "symfony/console" is installed.
*
* When the "symfony/dotenv" component is installed, .env files are loaded.
* When "symfony/error-handler" is installed, it is registered in debug mode.
*
* On top of the base arguments provided by GenericRuntime,
* this runtime can feed the app-callable with arguments of type:
* - Request from "symfony/http-foundation" if the component is installed;
* - Application, Command, InputInterface and/or OutputInterface
* from "symfony/console" if the component is installed.
*
* This runtime can handle app-callables that return instances of either:
* - HttpKernelInterface,
* - Response,
* - Application,
* - Command,
* - int|string|null as handled by GenericRuntime.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class SymfonyRuntime extends GenericRuntime
{
private readonly ArgvInput $input;
private readonly ConsoleOutput $output;
private readonly Application $console;
private readonly Command $command;
/**
* @param array {
* debug?: ?bool,
* env?: ?string,
* disable_dotenv?: ?bool,
* project_dir?: ?string,
* prod_envs?: ?string[],
* dotenv_path?: ?string,
* test_envs?: ?string[],
* use_putenv?: ?bool,
* runtimes?: ?array,
* error_handler?: string|false,
* env_var_name?: string,
* debug_var_name?: string,
* dotenv_overload?: ?bool,
* } $options
*/
public function __construct(array $options = [])
{
$envKey = $options['env_var_name'] ??= 'APP_ENV';
$debugKey = $options['debug_var_name'] ??= 'APP_DEBUG';
if (isset($_SERVER['argv']) && !empty($_GET)) {
// register_argc_argv=On is too risky in web servers
$_SERVER['argv'] = [];
$_SERVER['argc'] = 0;
}
if (isset($options['env'])) {
$_SERVER[$envKey] = $options['env'];
} elseif (empty($_GET) && isset($_SERVER['argv']) && class_exists(ArgvInput::class)) {
$this->options = $options;
$this->getInput();
}
if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) {
(new Dotenv($envKey, $debugKey))
->setProdEnvs((array) ($options['prod_envs'] ?? ['prod']))
->usePutenv($options['use_putenv'] ?? false)
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $options['dotenv_overload'] ?? false);
if (isset($this->input) && ($options['dotenv_overload'] ?? false)) {
if ($this->input->getParameterOption(['--env', '-e'], $_SERVER[$envKey], true) !== $_SERVER[$envKey]) {
throw new \LogicException(\sprintf('Cannot use "--env" or "-e" when the "%s" file defines "%s" and the "dotenv_overload" runtime option is true.', $options['dotenv_path'] ?? '.env', $envKey));
}
if ($_SERVER[$debugKey] && $this->input->hasParameterOption('--no-debug', true)) {
putenv($debugKey.'='.$_SERVER[$debugKey] = $_ENV[$debugKey] = '0');
}
}
$options['debug'] ??= '1' === $_SERVER[$debugKey];
$options['disable_dotenv'] = true;
} else {
$_SERVER[$envKey] ??= $_ENV[$envKey] ?? 'dev';
$_SERVER[$debugKey] ??= $_ENV[$debugKey] ?? !\in_array($_SERVER[$envKey], (array) ($options['prod_envs'] ?? ['prod']), true);
}
$options['error_handler'] ??= SymfonyErrorHandler::class;
parent::__construct($options);
}
public function getRunner(?object $application): RunnerInterface
{
if ($application instanceof HttpKernelInterface) {
return new HttpKernelRunner($application, Request::createFromGlobals(), $this->options['debug'] ?? false);
}
if ($application instanceof Response) {
return new ResponseRunner($application);
}
if ($application instanceof Command) {
$console = $this->console ??= new Application();
$console->setName($application->getName() ?: $console->getName());
if (!$application->getName() || !$console->has($application->getName())) {
$application->setName($_SERVER['argv'][0]);
$console->add($application);
}
$console->setDefaultCommand($application->getName(), true);
$console->getDefinition()->addOptions($application->getDefinition()->getOptions());
return $this->getRunner($console);
}
if ($application instanceof Application) {
set_time_limit(0);
$defaultEnv = !isset($this->options['env']) ? ($_SERVER[$this->options['env_var_name']] ?? 'dev') : null;
$output = $this->output ??= new ConsoleOutput();
return new ConsoleApplicationRunner($application, $defaultEnv, $this->getInput(), $output);
}
if (isset($this->command)) {
$this->getInput()->bind($this->command->getDefinition());
}
return parent::getRunner($application);
}
protected function getArgument(\ReflectionParameter $parameter, ?string $type): mixed
{
return match ($type) {
Request::class => Request::createFromGlobals(),
InputInterface::class => $this->getInput(),
OutputInterface::class => $this->output ??= new ConsoleOutput(),
Application::class => $this->console ??= new Application(),
Command::class => $this->command ??= new Command(),
default => parent::getArgument($parameter, $type),
};
}
protected static function register(GenericRuntime $runtime): GenericRuntime
{
$self = new self($runtime->options + ['runtimes' => []]);
$self->options['runtimes'] += [
HttpKernelInterface::class => $self,
Request::class => $self,
Response::class => $self,
Application::class => $self,
Command::class => $self,
InputInterface::class => $self,
OutputInterface::class => $self,
];
$runtime->options = $self->options;
return $self;
}
private function getInput(): ArgvInput
{
if (isset($this->input)) {
return $this->input;
}
$input = new ArgvInput();
if (isset($this->options['env'])) {
return $this->input = $input;
}
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
putenv($this->options['env_var_name'].'='.$_SERVER[$this->options['env_var_name']] = $_ENV[$this->options['env_var_name']] = $env);
}
if ($input->hasParameterOption('--no-debug', true)) {
putenv($this->options['debug_var_name'].'='.$_SERVER[$this->options['debug_var_name']] = $_ENV[$this->options['debug_var_name']] = '0');
}
return $this->input = $input;
}
}