Skip to content

Commit dfbac81

Browse files
[Runtime] make GenericRuntime ... generic
1 parent 49d23d4 commit dfbac81

File tree

15 files changed

+267
-55
lines changed

15 files changed

+267
-55
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
use Symfony\Component\HttpKernel\KernelEvents;
3939
use Symfony\Component\HttpKernel\KernelInterface;
4040
use Symfony\Component\HttpKernel\UriSigner;
41+
use Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner;
42+
use Symfony\Component\Runtime\Runner\Symfony\ResponseRunner;
4143
use Symfony\Component\Runtime\SymfonyRuntime;
4244
use Symfony\Component\String\LazyString;
4345
use Symfony\Component\String\Slugger\AsciiSlugger;
@@ -79,6 +81,8 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
7981
service('argument_resolver'),
8082
])
8183
->tag('container.hot_path')
84+
->tag('container.preload', ['class' => HttpKernelRunner::class])
85+
->tag('container.preload', ['class' => ResponseRunner::class])
8286
->tag('container.preload', ['class' => SymfonyRuntime::class])
8387
->alias(HttpKernelInterface::class, 'http_kernel')
8488

src/Symfony/Component/Runtime/GenericRuntime.php

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ class_exists(ClosureResolver::class);
2222
/**
2323
* A runtime to do bare-metal PHP without using superglobals.
2424
*
25-
* One option named "debug" is supported; it toggles displaying errors
26-
* and defaults to the "APP_ENV" environment variable.
25+
* It supports two options:
26+
* - "debug" it toggles displaying errors and defaults
27+
* to the "APP_ENV" environment variable
28+
* - "runtimes" maps types to a GenericRuntime implementation
29+
* that knows how to deal with each of them
2730
*
2831
* The app-callable can declare arguments among either:
2932
* - "array $context" to get a local array similar to $_SERVER;
@@ -42,28 +45,36 @@ class_exists(ClosureResolver::class);
4245
*/
4346
class GenericRuntime implements RuntimeInterface
4447
{
45-
private $debug;
48+
protected $options;
49+
50+
public function __construct(array $options = [])
51+
{
52+
$this->options = $options;
53+
}
4654

4755
/**
4856
* @param array {
4957
* debug?: ?bool,
58+
* runtime?: ?array
5059
* } $options
5160
*/
52-
public function __construct(array $options = [])
61+
public static function boot(array $options = []): RuntimeInterface
5362
{
54-
$this->debug = $options['debug'] ?? $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? true;
63+
$debug = $options['debug'] ?? $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? true;
5564

56-
if (!\is_bool($this->debug)) {
57-
$this->debug = filter_var($this->debug, \FILTER_VALIDATE_BOOLEAN);
65+
if (!\is_bool($debug)) {
66+
$debug = filter_var($debug, \FILTER_VALIDATE_BOOLEAN);
5867
}
5968

60-
if ($this->debug) {
69+
if ($debug) {
70+
umask(0000);
6171
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '1';
62-
$errorHandler = new BasicErrorHandler($this->debug);
63-
set_error_handler($errorHandler);
72+
set_error_handler(new BasicErrorHandler($debug));
6473
} else {
6574
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0';
6675
}
76+
77+
return new static($options);
6778
}
6879

6980
/**
@@ -95,7 +106,7 @@ public function getResolver(callable $callable): ResolverInterface
95106
return $arguments;
96107
};
97108

98-
if ($this->debug) {
109+
if ($_SERVER['APP_DEBUG']) {
99110
return new DebugClosureResolver($callable, $arguments);
100111
}
101112

@@ -115,15 +126,33 @@ public function getRunner(?object $application): RunnerInterface
115126
return $application;
116127
}
117128

118-
if (!\is_callable($application)) {
119-
throw new \LogicException(sprintf('"%s" doesn\'t know how to handle apps of type "%s".', get_debug_type($this), get_debug_type($application)));
120-
}
121-
122129
if (!$application instanceof \Closure) {
130+
$class = \get_class($application);
131+
132+
if ($runtime = $this->getRuntime($class)) {
133+
return $runtime->getRunner($application);
134+
}
135+
136+
foreach (class_parents($class) as $type) {
137+
if ($runtime = $this->getRuntime($type)) {
138+
return $runtime->getRunner($application);
139+
}
140+
}
141+
142+
foreach (class_implements($class) as $type) {
143+
if ($runtime = $this->getRuntime($type)) {
144+
return $runtime->getRunner($application);
145+
}
146+
}
147+
148+
if (!\is_callable($application)) {
149+
throw new \LogicException(sprintf('"%s" doesn\'t know how to handle apps of type "%s".', get_debug_type($this), get_debug_type($application)));
150+
}
151+
123152
$application = \Closure::fromCallable($application);
124153
}
125154

126-
if ($this->debug && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) {
155+
if ($_SERVER['APP_DEBUG'] && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) {
127156
throw new \ArgumentCountError(sprintf('Zero argument should be required by the runner callable, but at least one is in "%s" on line "%d.', $r->getFileName(), $r->getStartLine()));
128157
}
129158

@@ -163,8 +192,31 @@ protected function getArgument(\ReflectionParameter $parameter, ?string $type)
163192
return $this;
164193
}
165194

166-
$r = $parameter->getDeclaringFunction();
195+
if (!$runtime = $this->getRuntime($type)) {
196+
$r = $parameter->getDeclaringFunction();
197+
198+
throw new \InvalidArgumentException(sprintf('Cannot resolve argument "%s $%s" in "%s" on line "%d": "%s" supports only arguments "array $context", "array $argv" and "array $request", or a runtime named "Symfony\Runtime\%1$sRuntime".', $type, $parameter->name, $r->getFileName(), $r->getStartLine(), get_debug_type($this)));
199+
}
200+
201+
return $runtime->getArgument($parameter, $type);
202+
}
203+
204+
protected static function register(self $runtime): self
205+
{
206+
return $runtime;
207+
}
208+
209+
private function getRuntime(string $type)
210+
{
211+
if (null === $runtime = ($this->options['runtimes'][$type] ?? null)) {
212+
$runtime = 'Symfony\Runtime\\'.$type.'Runtime';
213+
$runtime = class_exists($runtime) ? $runtime : $this->options['runtimes'][$type] = false;
214+
}
215+
216+
if (\is_string($runtime)) {
217+
$runtime = $runtime::register($this);
218+
}
167219

168-
throw new \InvalidArgumentException(sprintf('Cannot resolve argument "%s $%s" in "%s" on line "%d": "%s" supports only arguments "array $context", "array $argv" and "array $request".', $type, $parameter->name, $r->getFileName(), $r->getStartLine(), get_debug_type($this)));
220+
return $runtime;
169221
}
170222
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Runtime\Symfony\Component\Console;
13+
14+
use Symfony\Component\Runtime\SymfonyRuntime;
15+
16+
/**
17+
* @internal
18+
*/
19+
class ApplicationRuntime extends SymfonyRuntime
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Runtime\Symfony\Component\Console\Command;
13+
14+
use Symfony\Component\Runtime\SymfonyRuntime;
15+
16+
/**
17+
* @internal
18+
*/
19+
class CommandRuntime extends SymfonyRuntime
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Runtime\Symfony\Component\Console\Input;
13+
14+
use Symfony\Component\Runtime\SymfonyRuntime;
15+
16+
/**
17+
* @internal
18+
*/
19+
class InputInterfaceRuntime extends SymfonyRuntime
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Runtime\Symfony\Component\Console\Output;
13+
14+
use Symfony\Component\Runtime\SymfonyRuntime;
15+
16+
/**
17+
* @internal
18+
*/
19+
class OutputInterfaceRuntime extends SymfonyRuntime
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Runtime\Symfony\Component\HttpFoundation;
13+
14+
use Symfony\Component\Runtime\SymfonyRuntime;
15+
16+
/**
17+
* @internal
18+
*/
19+
class RequestRuntime extends SymfonyRuntime
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Runtime\Symfony\Component\HttpFoundation;
13+
14+
use Symfony\Component\Runtime\SymfonyRuntime;
15+
16+
/**
17+
* @internal
18+
*/
19+
class ResponseRuntime extends SymfonyRuntime
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Runtime\Symfony\Component\HttKernel;
13+
14+
use Symfony\Component\Runtime\SymfonyRuntime;
15+
16+
/**
17+
* @internal
18+
*/
19+
class HttpKernelInterfaceRuntime extends SymfonyRuntime
20+
{
21+
}

src/Symfony/Component/Runtime/Internal/autoload_runtime.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (!is_object($app = require $_SERVER['SCRIPT_FILENAME'])) {
1111
}
1212

1313
$runtime = $_SERVER['APP_RUNTIME'] ?? %runtime_class%;
14-
$runtime = new $runtime(($_SERVER['APP_RUNTIME_OPTIONS'] ?? []) + %runtime_options%);
14+
$runtime = $runtime::boot(($_SERVER['APP_RUNTIME_OPTIONS'] ?? []) + %runtime_options%);
1515

1616
[$app, $args] = $runtime
1717
->getResolver($app)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy