|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Resolver\ClosureResolver; |
| 16 | +use Symfony\Component\Runtime\Resolver\DebugClosureResolver; |
| 17 | +use Symfony\Component\Runtime\Runner\ClosureRunner; |
| 18 | + |
| 19 | +// Help opcache.preload discover always-needed symbols |
| 20 | +class_exists(ClosureResolver::class); |
| 21 | + |
| 22 | +/** |
| 23 | + * A runtime to do bare-metal PHP without using superglobals. |
| 24 | + * |
| 25 | + * One option named "debug" is supported; it toggles displaying errors |
| 26 | + * and defaults to the "APP_ENV" environment variable. |
| 27 | + * |
| 28 | + * The app-callable can declare arguments among either: |
| 29 | + * - "array $context" to get a local array similar to $_SERVER; |
| 30 | + * - "array $argv" to get the command line arguments when running on the CLI; |
| 31 | + * - "array $request" to get a local array with keys "query", "body", "files" and |
| 32 | + * "session", which map to $_GET, $_POST, $FILES and &$_SESSION respectively. |
| 33 | + * |
| 34 | + * It should return a Closure():int|string|null or an instance of RunnerInterface. |
| 35 | + * |
| 36 | + * In debug mode, the runtime registers a strict error handler |
| 37 | + * that throws exceptions when a PHP warning/notice is raised. |
| 38 | + * |
| 39 | + * @author Nicolas Grekas <[email protected]> |
| 40 | + * |
| 41 | + * @experimental in 5.3 |
| 42 | + */ |
| 43 | +class GenericRuntime implements RuntimeInterface |
| 44 | +{ |
| 45 | + private $debug; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param array { |
| 49 | + * debug?: ?bool, |
| 50 | + * } $options |
| 51 | + */ |
| 52 | + public function __construct(array $options = []) |
| 53 | + { |
| 54 | + $this->debug = $options['debug'] ?? $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? true; |
| 55 | + |
| 56 | + if (!\is_bool($this->debug)) { |
| 57 | + $this->debug = filter_var($this->debug, \FILTER_VALIDATE_BOOLEAN); |
| 58 | + } |
| 59 | + |
| 60 | + if ($this->debug) { |
| 61 | + $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '1'; |
| 62 | + $errorHandler = new BasicErrorHandler($this->debug); |
| 63 | + set_error_handler($errorHandler); |
| 64 | + } else { |
| 65 | + $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritdoc} |
| 71 | + */ |
| 72 | + public function getResolver(callable $callable): ResolverInterface |
| 73 | + { |
| 74 | + if (!$callable instanceof \Closure) { |
| 75 | + $callable = \Closure::fromCallable($callable); |
| 76 | + } |
| 77 | + |
| 78 | + $function = new \ReflectionFunction($callable); |
| 79 | + $parameters = $function->getParameters(); |
| 80 | + |
| 81 | + $arguments = function () use ($parameters) { |
| 82 | + $arguments = []; |
| 83 | + |
| 84 | + try { |
| 85 | + foreach ($parameters as $parameter) { |
| 86 | + $type = $parameter->getType(); |
| 87 | + $arguments[] = $this->getArgument($parameter, $type instanceof \ReflectionNamedType ? $type->getName() : null); |
| 88 | + } |
| 89 | + } catch (\InvalidArgumentException $e) { |
| 90 | + if (!$parameter->isOptional()) { |
| 91 | + throw $e; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return $arguments; |
| 96 | + }; |
| 97 | + |
| 98 | + if ($this->debug) { |
| 99 | + return new DebugClosureResolver($callable, $arguments); |
| 100 | + } |
| 101 | + |
| 102 | + return new ClosureResolver($callable, $arguments); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * {@inheritdoc} |
| 107 | + */ |
| 108 | + public function getRunner(?object $application): RunnerInterface |
| 109 | + { |
| 110 | + if (null === $application) { |
| 111 | + $application = static function () { return 0; }; |
| 112 | + } |
| 113 | + |
| 114 | + if ($application instanceof RunnerInterface) { |
| 115 | + return $application; |
| 116 | + } |
| 117 | + |
| 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 | + |
| 122 | + if (!$application instanceof \Closure) { |
| 123 | + $application = \Closure::fromCallable($application); |
| 124 | + } |
| 125 | + |
| 126 | + if ($this->debug && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) { |
| 127 | + 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())); |
| 128 | + } |
| 129 | + |
| 130 | + return new ClosureRunner($application); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @return mixed |
| 135 | + */ |
| 136 | + protected function getArgument(\ReflectionParameter $parameter, ?string $type) |
| 137 | + { |
| 138 | + if ('array' === $type) { |
| 139 | + switch ($parameter->name) { |
| 140 | + case 'context': |
| 141 | + $context = $_SERVER; |
| 142 | + |
| 143 | + if ($_ENV && !isset($_SERVER['PATH']) && !isset($_SERVER['Path'])) { |
| 144 | + $context += $_ENV; |
| 145 | + } |
| 146 | + |
| 147 | + return $context; |
| 148 | + |
| 149 | + case 'argv': |
| 150 | + return $_SERVER['argv'] ?? []; |
| 151 | + |
| 152 | + case 'request': |
| 153 | + return [ |
| 154 | + 'query' => $_GET, |
| 155 | + 'body' => $_POST, |
| 156 | + 'files' => $_FILES, |
| 157 | + 'session' => &$_SESSION, |
| 158 | + ]; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if (RuntimeInterface::class === $type) { |
| 163 | + return $this; |
| 164 | + } |
| 165 | + |
| 166 | + $r = $parameter->getDeclaringFunction(); |
| 167 | + |
| 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))); |
| 169 | + } |
| 170 | +} |
0 commit comments