|
| 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\HttpKernel\Exception; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 16 | + */ |
| 17 | +class ControllerDoesNotReturnResponseException extends \LogicException |
| 18 | +{ |
| 19 | + public function __construct(string $message, callable $controller, string $file, int $line) |
| 20 | + { |
| 21 | + parent::__construct($message); |
| 22 | + |
| 23 | + if (!$controllerDefinition = $this->parseControllerDefinition($controller)) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + $this->file = $controllerDefinition['file']; |
| 28 | + $this->line = $controllerDefinition['line']; |
| 29 | + $r = new \ReflectionProperty(\Exception::class, 'trace'); |
| 30 | + $r->setAccessible(true); |
| 31 | + $r->setValue($this, array_merge(array( |
| 32 | + array( |
| 33 | + 'line' => $line, |
| 34 | + 'file' => $file, |
| 35 | + ), |
| 36 | + ), $this->getTrace())); |
| 37 | + } |
| 38 | + |
| 39 | + private function parseControllerDefinition(callable $controller): ?array |
| 40 | + { |
| 41 | + if (is_string($controller) && false !== strpos($controller, '::')) { |
| 42 | + $controller = explode('::', $controller); |
| 43 | + } |
| 44 | + |
| 45 | + if (is_array($controller)) { |
| 46 | + try { |
| 47 | + $r = new \ReflectionMethod($controller[0], $controller[1]); |
| 48 | + |
| 49 | + return array( |
| 50 | + 'file' => $r->getFileName(), |
| 51 | + 'line' => $r->getEndLine(), |
| 52 | + ); |
| 53 | + } catch (\ReflectionException $e) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if ($controller instanceof \Closure) { |
| 59 | + $r = new \ReflectionFunction($controller); |
| 60 | + |
| 61 | + return array( |
| 62 | + 'file' => $r->getFileName(), |
| 63 | + 'line' => $r->getEndLine(), |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + if (is_object($controller)) { |
| 68 | + $r = new \ReflectionClass($controller); |
| 69 | + |
| 70 | + return array( |
| 71 | + 'file' => $r->getFileName(), |
| 72 | + 'line' => $r->getEndLine(), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return null; |
| 77 | + } |
| 78 | +} |
0 commit comments