Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 837615b

Browse files
committed
Deprecate things that prevent \Throwable from bubbling down
1 parent 0472dbf commit 837615b

33 files changed

+251
-42
lines changed

src/Symfony/Bridge/Doctrine/DataCollector/DoctrineDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* DoctrineDataCollector.
2424
*
2525
* @author Fabien Potencier <[email protected]>
26+
*
27+
* @final since Symfony 4.4
2628
*/
2729
class DoctrineDataCollector extends DataCollector
2830
{

src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* TwigDataCollector.
2626
*
2727
* @author Fabien Potencier <[email protected]>
28+
*
29+
* @final since Symfony 4.4
2830
*/
2931
class TwigDataCollector extends DataCollector implements LateDataCollectorInterface
3032
{

src/Symfony/Bundle/FrameworkBundle/DataCollector/RouterDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* RouterDataCollector.
2020
*
2121
* @author Fabien Potencier <[email protected]>
22+
*
23+
* @final since Symfony 4.4
2224
*/
2325
class RouterDataCollector extends BaseRouterDataCollector
2426
{

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
/**
3636
* @author Fabien Potencier <[email protected]>
37+
*
38+
* @final since Symfony 4.4
3739
*/
3840
class SecurityDataCollector extends DataCollector implements LateDataCollectorInterface
3941
{

src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
/**
2222
* @author Aaron Scherer <[email protected]>
2323
* @author Tobias Nyholm <[email protected]>
24+
*
25+
* @final since Symfony 4.4
2426
*/
2527
class CacheDataCollector extends DataCollector implements LateDataCollectorInterface
2628
{

src/Symfony/Component/Console/Application.php

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ public function run(InputInterface $input = null, OutputInterface $output = null
132132
$e = class_exists(ErrorException::class) ? new ErrorException($e) : (class_exists(LegacyFatalThrowableError::class) ? new LegacyFatalThrowableError($e) : new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine()));
133133
}
134134
if ($output instanceof ConsoleOutputInterface) {
135-
$this->renderException($e, $output->getErrorOutput());
135+
$this->renderThrowable($e, $output->getErrorOutput());
136136
} else {
137-
$this->renderException($e, $output);
137+
$this->renderThrowable($e, $output);
138138
}
139139
};
140140
if ($phpHandler = set_exception_handler($renderException)) {
@@ -792,9 +792,13 @@ public static function getAbbreviations($names)
792792

793793
/**
794794
* Renders a caught exception.
795+
*
796+
* @deprecated since Symfony 4.4, use "renderThrowable()" instead
795797
*/
796798
public function renderException(\Exception $e, OutputInterface $output)
797799
{
800+
@trigger_error(sprintf('The "%s::renderException()" method is deprecated since Symfony 4.4, use "renderThrowable()" instead.', __CLASS__));
801+
798802
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
799803

800804
$this->doRenderException($e, $output);
@@ -805,8 +809,124 @@ public function renderException(\Exception $e, OutputInterface $output)
805809
}
806810
}
807811

812+
public function renderThrowable(\Throwable $e, OutputInterface $output): void
813+
{
814+
if (__CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, 'renderException'))->getDeclaringClass()->getName()) {
815+
@trigger_error(sprintf('The "%s::renderException()" method is deprecated since Symfony 4.4, use "renderThrowable()" instead.', __CLASS__));
816+
817+
if (!$e instanceof \Exception) {
818+
$e = new ErrorException($e);
819+
}
820+
821+
$this->renderException($e, $output);
822+
823+
return;
824+
}
825+
826+
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
827+
828+
$this->doRenderThrowable($e, $output);
829+
830+
if (null !== $this->runningCommand) {
831+
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);
832+
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
833+
}
834+
}
835+
836+
/**
837+
* @deprecated since Symfony 4.4, use "doRenderThrowable()" instead
838+
*/
808839
protected function doRenderException(\Exception $e, OutputInterface $output)
809840
{
841+
@trigger_error(sprintf('The "%s::doRenderException()" method is deprecated since Symfony 4.4, use "doRenderThrowable()" instead.', __CLASS__));
842+
843+
do {
844+
$message = trim($e->getMessage());
845+
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
846+
$class = \get_class($e);
847+
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
848+
$title = sprintf(' [%s%s] ', $class, 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
849+
$len = Helper::strlen($title);
850+
} else {
851+
$len = 0;
852+
}
853+
854+
if (false !== strpos($message, "class@anonymous\0")) {
855+
$message = preg_replace_callback('/class@anonymous\x00.*?\.php0x?[0-9a-fA-F]++/', function ($m) {
856+
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
857+
}, $message);
858+
}
859+
860+
$width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX;
861+
$lines = [];
862+
foreach ('' !== $message ? preg_split('/\r?\n/', $message) : [] as $line) {
863+
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
864+
// pre-format lines to get the right string length
865+
$lineLength = Helper::strlen($line) + 4;
866+
$lines[] = [$line, $lineLength];
867+
868+
$len = max($lineLength, $len);
869+
}
870+
}
871+
872+
$messages = [];
873+
if (!$e instanceof ExceptionInterface || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
874+
$messages[] = sprintf('<comment>%s</comment>', OutputFormatter::escape(sprintf('In %s line %s:', basename($e->getFile()) ?: 'n/a', $e->getLine() ?: 'n/a')));
875+
}
876+
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
877+
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
878+
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
879+
}
880+
foreach ($lines as $line) {
881+
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
882+
}
883+
$messages[] = $emptyLine;
884+
$messages[] = '';
885+
886+
$output->writeln($messages, OutputInterface::VERBOSITY_QUIET);
887+
888+
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
889+
$output->writeln('<comment>Exception trace:</comment>', OutputInterface::VERBOSITY_QUIET);
890+
891+
// exception related properties
892+
$trace = $e->getTrace();
893+
894+
array_unshift($trace, [
895+
'function' => '',
896+
'file' => $e->getFile() ?: 'n/a',
897+
'line' => $e->getLine() ?: 'n/a',
898+
'args' => [],
899+
]);
900+
901+
for ($i = 0, $count = \count($trace); $i < $count; ++$i) {
902+
$class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
903+
$type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
904+
$function = isset($trace[$i]['function']) ? $trace[$i]['function'] : '';
905+
$file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
906+
$line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
907+
908+
$output->writeln(sprintf(' %s%s at <info>%s:%s</info>', $class, $function ? $type.$function.'()' : '', $file, $line), OutputInterface::VERBOSITY_QUIET);
909+
}
910+
911+
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
912+
}
913+
} while ($e = $e->getPrevious());
914+
}
915+
916+
protected function doRenderThrowable(\Throwable $e, OutputInterface $output): void
917+
{
918+
if (__CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, 'doRenderException'))->getDeclaringClass()->getName()) {
919+
@trigger_error(sprintf('The "%s::doRenderException()" method is deprecated since Symfony 4.4, use "doRenderThrowable()" instead.', __CLASS__));
920+
921+
if (!$e instanceof \Exception) {
922+
$e = new ErrorException($e);
923+
}
924+
925+
$this->doRenderException($e, $output);
926+
927+
return;
928+
}
929+
810930
do {
811931
$message = trim($e->getMessage());
812932
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {

src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
/**
2020
* @author Jérémy Romey <[email protected]>
21+
*
22+
* @final since Symfony 4.4
2123
*/
2224
final class HttpClientDataCollector extends DataCollector
2325
{

src/Symfony/Component/HttpKernel/DataCollector/AjaxDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* AjaxDataCollector.
1919
*
2020
* @author Bart van den Burg <[email protected]>
21+
*
22+
* @final since Symfony 4.4
2123
*/
2224
class AjaxDataCollector extends DataCollector
2325
{

src/Symfony/Component/HttpKernel/DataCollector/ConfigDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
/**
2121
* @author Fabien Potencier <[email protected]>
22+
*
23+
* @final since Symfony 4.4
2224
*/
2325
class ConfigDataCollector extends DataCollector implements LateDataCollectorInterface
2426
{

src/Symfony/Component/HttpKernel/DataCollector/DataCollectorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface DataCollectorInterface extends ResetInterface
2525
/**
2626
* Collects data for the given Request and Response.
2727
*/
28-
public function collect(Request $request, Response $response, \Exception $exception = null);
28+
public function collect(Request $request, Response $response/*, \Throwable $exception = null*/);
2929

3030
/**
3131
* Returns the name of the collector.

src/Symfony/Component/HttpKernel/DataCollector/EventDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* EventDataCollector.
2424
*
2525
* @author Fabien Potencier <[email protected]>
26+
*
27+
* @final since Symfony 4.4
2628
*/
2729
class EventDataCollector extends DataCollector implements LateDataCollectorInterface
2830
{

src/Symfony/Component/HttpKernel/DataCollector/ExceptionDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* ExceptionDataCollector.
2020
*
2121
* @author Fabien Potencier <[email protected]>
22+
*
23+
* @final since Symfony 4.4
2224
*/
2325
class ExceptionDataCollector extends DataCollector
2426
{

src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* LogDataCollector.
2222
*
2323
* @author Fabien Potencier <[email protected]>
24+
*
25+
* @final since Symfony 4.4
2426
*/
2527
class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
2628
{

src/Symfony/Component/HttpKernel/DataCollector/MemoryDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* MemoryDataCollector.
1919
*
2020
* @author Fabien Potencier <[email protected]>
21+
*
22+
* @final since Symfony 4.4
2123
*/
2224
class MemoryDataCollector extends DataCollector implements LateDataCollectorInterface
2325
{

src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
/**
2424
* @author Fabien Potencier <[email protected]>
25+
*
26+
* @final since Symfony 4.4
2527
*/
2628
class RequestDataCollector extends DataCollector implements EventSubscriberInterface, LateDataCollectorInterface
2729
{

src/Symfony/Component/HttpKernel/DataCollector/RouterDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
/**
2020
* @author Fabien Potencier <[email protected]>
21+
*
22+
* @internal since Symfony 4.4
2123
*/
2224
class RouterDataCollector extends DataCollector
2325
{

src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
/**
2121
* @author Fabien Potencier <[email protected]>
22+
*
23+
* @final since Symfony 4.4
2224
*/
2325
class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
2426
{

src/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Event;
1313

14+
use Symfony\Component\ErrorHandler\Exception\ErrorException;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpKernel\HttpKernelInterface;
1617

@@ -19,33 +20,41 @@
1920
*/
2021
class GetResponseForExceptionEvent extends RequestEvent
2122
{
22-
/**
23-
* The exception object.
24-
*
25-
* @var \Exception
26-
*/
27-
private $exception;
23+
private $throwable;
2824

2925
/**
3026
* @var bool
3127
*/
3228
private $allowCustomResponseCode = false;
3329

34-
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Exception $e)
30+
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e)
3531
{
3632
parent::__construct($kernel, $request, $requestType);
3733

38-
$this->setException($e);
34+
$this->throwable = $e;
3935
}
4036

4137
/**
4238
* Returns the thrown exception.
4339
*
40+
* @deprecated since Symfony 4.4, use "getThrowable()" instead
41+
*
4442
* @return \Exception The thrown exception
4543
*/
4644
public function getException()
4745
{
48-
return $this->exception;
46+
@trigger_error(sprintf('The "%s::getException()" method is deprecated since Symfony 4.4, use "getThrowable()" instead.', \get_class($this)));
47+
48+
if (!$this->throwable instanceof \Exception) {
49+
return new ErrorException($this->throwable);
50+
}
51+
52+
return $this->throwable;
53+
}
54+
55+
public function getThrowable(): \Throwable
56+
{
57+
return $this->throwable;
4958
}
5059

5160
/**
@@ -57,7 +66,7 @@ public function getException()
5766
*/
5867
public function setException(\Exception $exception)
5968
{
60-
$this->exception = $exception;
69+
$this->throwable = $exception;
6170
}
6271

6372
/**

src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ public function configure(Event $event = null)
130130
$e = new ErrorException($e);
131131
}
132132

133-
$app->renderException($e, $output);
133+
if (method_exists($app, 'renderThrowable')) {
134+
$app->renderThrowable($e, $output);
135+
} else {
136+
$app->renderException($e, $output);
137+
}
134138
};
135139
}
136140
}

0 commit comments

Comments
 (0)