|
| 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\Runtime\Internal\BasicErrorHandler; |
| 15 | +use Symfony\Component\Runtime\ResolvedCallable\ResolvedCallable; |
| 16 | +use Symfony\Component\Runtime\ResolvedCallable\ResolvedScalar; |
| 17 | +use Symfony\Component\Runtime\StartedApplication\StartedClosure; |
| 18 | + |
| 19 | +// Help opcache.preload discover always-needed symbols |
| 20 | +class_exists(ResolvedCallable::class); |
| 21 | +class_exists(BasicErrorHandler::class); |
| 22 | + |
| 23 | +/** |
| 24 | + * A runtime to do bare-metal PHP without using superglobals. |
| 25 | + * |
| 26 | + * One option named "debug" is supported; it toggles displaying errors. |
| 27 | + * |
| 28 | + * The app-closure returned by the entry script must return either: |
| 29 | + * - "string" to echo the response content, or |
| 30 | + * - "int" to set the exit status code. |
| 31 | + * |
| 32 | + * The app-closure can declare arguments among either: |
| 33 | + * - "array $context" to get a local array similar to $_SERVER; |
| 34 | + * - "array $argv" to get the command line arguments when running on the CLI; |
| 35 | + * - "array $request" to get a local array with keys "query", "data", "files" and |
| 36 | + * "session", which map to $_GET, $_POST, $FILES and &$_SESSION respectively. |
| 37 | + * |
| 38 | + * The runtime sets up a strict error handler that throws |
| 39 | + * exceptions when a PHP warning/notice is raised. |
| 40 | + * |
| 41 | + * @author Nicolas Grekas <p@tchwork.com> |
| 42 | + */ |
| 43 | +class GenericRuntime implements RuntimeInterface |
| 44 | +{ |
| 45 | + private $debug; |
| 46 | + |
| 47 | + public function __construct(array $options = []) |
| 48 | + { |
| 49 | + $this->debug = $options['debug'] ?? true; |
| 50 | + $errorHandler = new BasicErrorHandler($this->debug); |
| 51 | + set_error_handler($errorHandler); |
| 52 | + set_exception_handler([$errorHandler, 'handleException']); |
| 53 | + } |
| 54 | + |
| 55 | + public function resolve(callable $callable): ResolvedCallableInterface |
| 56 | + { |
| 57 | + if (!$callable instanceof \Closure) { |
| 58 | + $callable = \Closure::fromCallable($callable); |
| 59 | + } |
| 60 | + |
| 61 | + $arguments = []; |
| 62 | + $function = new \ReflectionFunction($callable); |
| 63 | + |
| 64 | + try { |
| 65 | + foreach ($function->getParameters() as $parameter) { |
| 66 | + $arguments[] = $this->getArgument($parameter, $parameter->getType()); |
| 67 | + } |
| 68 | + } catch (\InvalidArgumentException $e) { |
| 69 | + if (!$parameter->isOptional()) { |
| 70 | + throw $e; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + $returnType = $function->getReturnType(); |
| 75 | + |
| 76 | + switch ($returnType instanceof \ReflectionNamedType ? $returnType->getName() : '') { |
| 77 | + case 'string': |
| 78 | + return new ResolvedScalar(static function () use ($callable, $arguments): int { |
| 79 | + echo $callable(...$arguments); |
| 80 | + |
| 81 | + return 0; |
| 82 | + }); |
| 83 | + |
| 84 | + case 'int': |
| 85 | + case 'void': |
| 86 | + return new ResolvedScalar(static function () use ($callable, $arguments): int { |
| 87 | + return $callable(...$arguments) ?? 0; |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + return new ResolvedCallable($callable, $arguments); |
| 92 | + } |
| 93 | + |
| 94 | + public function start(object $application): StartedApplicationInterface |
| 95 | + { |
| 96 | + if (!$application instanceof \Closure) { |
| 97 | + throw new \LogicException(sprintf('"%s" doesn\'t know how to handle apps of type "%s".', get_debug_type($this), get_debug_type($application))); |
| 98 | + } |
| 99 | + |
| 100 | + if ($this->debug && (new \ReflectionFunction($application))->getNumberOfRequiredParameters()) { |
| 101 | + throw new \ArgumentCountError('Zero argument should be required by the closure returned by the app, but at least one is.'); |
| 102 | + } |
| 103 | + |
| 104 | + return new StartedClosure($application); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return mixed |
| 109 | + */ |
| 110 | + protected function getArgument(\ReflectionParameter $parameter, ?\ReflectionType $type) |
| 111 | + { |
| 112 | + $type = $type instanceof \ReflectionNamedType ? $type->getName() : ''; |
| 113 | + |
| 114 | + if (RuntimeInterface::class === $type) { |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + if ('array' !== $type) { |
| 119 | + throw new \InvalidArgumentException(sprintf('Cannot resolve argument "%s $%s": "%s" supports only arguments "$context", "$argv" and "$request" with type "array".', $type, $parameter->name, get_debug_type($this))); |
| 120 | + } |
| 121 | + |
| 122 | + switch ($parameter->name) { |
| 123 | + case 'context': |
| 124 | + $context = $_SERVER; |
| 125 | + |
| 126 | + if ($_ENV && !isset($_SERVER['PATH']) && !isset($_SERVER['Path'])) { |
| 127 | + $context += $_ENV; |
| 128 | + } |
| 129 | + |
| 130 | + return $context; |
| 131 | + |
| 132 | + case 'argv': |
| 133 | + return $_SERVER['argv'] ?? []; |
| 134 | + |
| 135 | + case 'request': |
| 136 | + return [ |
| 137 | + 'query' => $_GET, |
| 138 | + 'data' => $_POST, |
| 139 | + 'files' => $_FILES, |
| 140 | + 'session' => &$_SESSION, |
| 141 | + ]; |
| 142 | + } |
| 143 | + |
| 144 | + throw new \InvalidArgumentException(sprintf('Cannot resolve array argument "$%s": "%s" supports only arguments "$context", "$argv" and "$request".', $parameter->name, get_debug_type($this))); |
| 145 | + } |
| 146 | +} |
0 commit comments