8000 Use debug to handle errors with PHP 5 · symfony/symfony@1cf2545 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cf2545

Browse files
committed
Use debug to handle errors with PHP 5
1 parent 3d2d0aa commit 1cf2545

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
3737
use Symfony\Component\Console\Exception\CommandNotFoundException;
3838
use Symfony\Component\Console\Exception\LogicException;
39+
use Symfony\Component\Debug\Debug;
40+
use Symfony\Component\Debug\ErrorHandler;
3941
use Symfony\Component\Debug\Exception\FatalThrowableError;
4042
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4143

@@ -120,6 +122,21 @@ public function run(InputInterface $input = null, OutputInterface $output = null
120122
$this->configureIO($input, $output);
121123

122124
try {
125+
if (PHP_MAJOR_VERSION < 7 && $this->catchErrors) {
126+
Debug::enable();
127+
$handler = set_exception_handler('var_dump');
128+
$handler = is_array($handler) ? $handler[0] : null;
129+
restore_exception_handler();
130+
131+
if (!$handler instanceof ErrorHandler) {
132+
throw new \RuntimeException(sprintf('Expected instance of "%s". Got "%s".', ErrorHandler::class, get_class($handler)));
133+
}
134+
135+
$handler->setExceptionHandler(function ($e) use ($output) {
136+
$this->renderThrowable($e, $output);
137+
});
138+
}
139+
123140
$exitCode = $this->doRun($input, $output);
124141
} catch (\Exception $e) {
125142
if (!$this->catchExceptions) {
@@ -291,6 +308,10 @@ public function areErrorsCaught()
291308
*/
292309
public function setCatchErrors($boolean)
293310
{
311+
if (PHP_VERSION_ID < 70000 && $this->catchErrors && !class_exists(Debug::class)) {
312+
throw new \LogicException('The "symfony/debug" component is required for using "catchErrors" along with php < 7.');
313+
}
314+
294315
$this->catchErrors = (bool) $boolean;
295316
}
296317

0 commit comments

Comments
 (0)
0