Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitba4d6bc

Browse files
committed
feature#18140 [Console] Add console.ERROR event and deprecate console.EXCEPTION (wouterj)
This PR was merged into the 3.3-dev branch.Discussion----------[Console] Add console.ERROR event and deprecate console.EXCEPTION| Q | A || --- | --- || Branch | master || Bug fix? | yes || New feature? | yes || BC breaks? | no || Deprecations? | yes || Tests pass? | yes || Fixed tickets | - || License | MIT || Doc PR | todo |## The ProblemThe current `console.EXCEPTION` event is only dispatched for exceptions during the execution of `Command#execute()`. All other exceptions (e.g. the ones thrown by listeners to events) are catched by the `try ... catch` loop in `Application#doRunCommand()`. This means that there is _no way to override exception handling_.## The SolutionThis PR adds a `console.ERROR` event which has the same scope as the default `try ... catch` loop. This allows to customize all exception handling.In order to keep BC, a new event was created and `console.EXCEPTION` was deprecated.Commits-------c02a4c9 Added a console.ERROR event
2 parents42e5b4e +c02a4c9 commitba4d6bc

File tree

12 files changed

+298
-55
lines changed

12 files changed

+298
-55
lines changed

‎UPGRADE-3.3.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ Debug
7373

7474
* The`ContextErrorException` class is deprecated.`\ErrorException` will be used instead in 4.0.
7575

76+
Console
77+
-------
78+
79+
* The`console.exception` event and the related`ConsoleExceptionEvent` class
80+
have been deprecated in favor of the`console.error` event and the`ConsoleErrorEvent`
81+
class. The deprecated event and class will be removed in 4.0.
82+
7683
DependencyInjection
7784
-------------------
7885

‎UPGRADE-4.0.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Console
5858
$commandTester->execute();
5959
```
6060

61+
* The`console.exception` event and the related`ConsoleExceptionEvent` class have
62+
been removed in favor of the`console.error` event and the`ConsoleErrorEvent` class.
63+
6164
Debug
6265
-----
6366

‎src/Symfony/Component/Console/Application.php‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
useSymfony\Component\Console\Helper\Helper;
3434
useSymfony\Component\Console\Helper\FormatterHelper;
3535
useSymfony\Component\Console\Event\ConsoleCommandEvent;
36+
useSymfony\Component\Console\Event\ConsoleErrorEvent;
3637
useSymfony\Component\Console\Event\ConsoleExceptionEvent;
3738
useSymfony\Component\Console\Event\ConsoleTerminateEvent;
3839
useSymfony\Component\Console\Exception\CommandNotFoundException;
@@ -118,16 +119,40 @@ public function run(InputInterface $input = null, OutputInterface $output = null
118119
$this->configureIO($input,$output);
119120

120121
try {
122+
$e =null;
121123
$exitCode =$this->doRun($input,$output);
122124
}catch (\Exception$e) {
125+
$exception =$e;
126+
}catch (\Throwable$e) {
127+
$exception =newFatalThrowableError($e);
128+
}
129+
130+
if (null !==$e &&null !==$this->dispatcher) {
131+
$event =newConsoleErrorEvent($this->runningCommand,$input,$output,$e,$e->getCode());
132+
$this->dispatcher->dispatch(ConsoleEvents::ERROR,$event);
133+
134+
$e =$event->getError();
135+
136+
if ($event->isErrorHandled()) {
137+
$e =null;
138+
$exitCode =0;
139+
}else {
140+
$exitCode =$e->getCode();
141+
}
142+
143+
$event =newConsoleTerminateEvent($this->runningCommand,$input,$output,$exitCode);
144+
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE,$event);
145+
}
146+
147+
if (null !==$e) {
123148
if (!$this->catchExceptions) {
124149
throw$e;
125150
}
126151

127152
if ($outputinstanceof ConsoleOutputInterface) {
128-
$this->renderException($e,$output->getErrorOutput());
153+
$this->renderException($exception,$output->getErrorOutput());
129154
}else {
130-
$this->renderException($e,$output);
155+
$this->renderException($exception,$output);
131156
}
132157

133158
$exitCode =$e->getCode();
@@ -863,17 +888,17 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
863888
}catch (\Throwable$x) {
864889
$e =newFatalThrowableError($x);
865890
}
891+
866892
if (null !==$e) {
867-
$event =newConsoleExceptionEvent($command,$input,$output,$e,$e->getCode());
893+
$event =newConsoleExceptionEvent($command,$input,$output,$e,$e->getCode(),false);
868894
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION,$event);
869895

870896
if ($e !==$event->getException()) {
897+
@trigger_error('The "console.exception" event is deprecated since version 3.3 and will be removed in 4.0. Use the "console.error" event instead.',E_USER_DEPRECATED);
898+
871899
$x =$e =$event->getException();
872900
}
873901

874-
$event =newConsoleTerminateEvent($command,$input,$output,$e->getCode());
875-
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE,$event);
876-
877902
throw$x;
878903
}
879904
}else {

‎src/Symfony/Component/Console/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ CHANGELOG
88
* added`AddConsoleCommandPass` (originally in FrameworkBundle)
99
*[BC BREAK]`Input::getOption()` no longer returns the default value for options
1010
with value optional explicitly passed empty
11+
* added console.error event to catch exceptions thrown by other listeners
12+
* deprecated console.exception event in favor of console.error
1113

1214
3.2.0
1315
------

‎src/Symfony/Component/Console/ConsoleEvents.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,30 @@ final class ConsoleEvents
4040
constTERMINATE ='console.terminate';
4141

4242
/**
43-
* The EXCEPTION event occurs when an uncaught exception appears.
43+
* The EXCEPTION event occurs when an uncaught exception appears
44+
* while executing Command#run().
4445
*
4546
* This event allows you to deal with the exception or
4647
* to modify the thrown exception.
4748
*
4849
* @Event("Symfony\Component\Console\Event\ConsoleExceptionEvent")
4950
*
5051
* @var string
52+
*
53+
* @deprecated The console.exception event is deprecated since version 3.3 and will be removed in 4.0. Use the console.error event instead.
5154
*/
5255
constEXCEPTION ='console.exception';
56+
57+
/**
58+
* The ERROR event occurs when an uncaught exception appears or
59+
* a throwable error.
60+
*
61+
* This event allows you to deal with the exception/error or
62+
* to modify the thrown exception.
63+
*
64+
* @Event("Symfony\Component\Console\Event\ConsoleErrorEvent")
65+
*
66+
* @var string
67+
*/
68+
constERROR ='console.error';
5369
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
namespaceSymfony\Component\Console\Event;
13+
14+
useSymfony\Component\Console\Command\Command;
15+
useSymfony\Component\Console\Exception\InvalidArgumentException;
16+
useSymfony\Component\Console\Input\InputInterface;
17+
useSymfony\Component\Console\Output\OutputInterface;
18+
useSymfony\Component\Debug\Exception\FatalThrowableError;
19+
20+
/**
21+
* Allows to handle throwables thrown while running a command.
22+
*
23+
* @author Wouter de Jong <wouter@wouterj.nl>
24+
*/
25+
class ConsoleErrorEventextends ConsoleExceptionEvent
26+
{
27+
private$error;
28+
private$handled =false;
29+
30+
publicfunction__construct(Command$command,InputInterface$input,OutputInterface$output,$error,$exitCode)
31+
{
32+
if (!$errorinstanceof \Throwable && !$errorinstanceof \Exception) {
33+
thrownewInvalidArgumentException(sprintf('The error passed to ConsoleErrorEvent must be an instance of \Throwable or \Exception, "%s" was passed instead.',is_object($error) ?get_class($error) :gettype($error)));
34+
}
35+
36+
$exception =$error;
37+
if (!$errorinstanceof \Exception) {
38+
$exception =newFatalThrowableError($error);
39+
}
40+
parent::__construct($command,$input,$output,$exception,$exitCode,false);
41+
42+
$this->error =$error;
43+
}
44+
45+
/**
46+
* Returns the thrown error/exception.
47+
*
48+
* @return \Throwable
49+
*/
50+
publicfunctiongetError()
51+
{
52+
return$this->error;
53+
}
54+
55+
/**
56+
* Replaces the thrown error/exception.
57+
*
58+
* @param \Throwable $error
59+
*/
60+
publicfunctionsetError($error)
61+
{
62+
if (!$errorinstanceof \Throwable && !$errorinstanceof \Exception) {
63+
thrownewInvalidArgumentException(sprintf('The error passed to ConsoleErrorEvent must be an instance of \Throwable or \Exception, "%s" was passed instead.',is_object($error) ?get_class($error) :gettype($error)));
64+
}
65+
66+
$this->error =$error;
67+
}
68+
69+
/**
70+
* Marks the error/exception as handled.
71+
*
72+
* If it is not marked as handled, the error/exception will be displayed in
73+
* the command output.
74+
*/
75+
publicfunctionmarkErrorAsHandled()
76+
{
77+
$this->handled =true;
78+
}
79+
80+
/**
81+
* Whether the error/exception is handled by a listener or not.
82+
*
83+
* If it is not yet handled, the error/exception will be displayed in the
84+
* command output.
85+
*
86+
* @return bool
87+
*/
88+
publicfunctionisErrorHandled()
89+
{
90+
return$this->handled;
91+
}
92+
93+
/**
94+
* @deprecated Since version 3.3, to be removed in 4.0. Use getError() instead
95+
*/
96+
publicfunctiongetException()
97+
{
98+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use ConsoleErrorEvent::getError() instead.',__METHOD__),E_USER_DEPRECATED);
99+
100+
returnparent::getException();
101+
}
102+
103+
/**
104+
* @deprecated Since version 3.3, to be removed in 4.0. Use setError() instead
105+
*/
106+
publicfunctionsetException(\Exception$exception)
107+
{
108+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use ConsoleErrorEvent::setError() instead.',__METHOD__),E_USER_DEPRECATED);
109+
110+
parent::setException($exception);
111+
}
112+
}

‎src/Symfony/Component/Console/Event/ConsoleEvent.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ConsoleEvent extends Event
2828
private$input;
2929
private$output;
3030

31-
publicfunction__construct(Command$command,InputInterface$input,OutputInterface$output)
31+
publicfunction__construct(Command$command =null,InputInterface$input,OutputInterface$output)
3232
{
3333
$this->command =$command;
3434
$this->input =$input;
@@ -38,7 +38,7 @@ public function __construct(Command $command, InputInterface $input, OutputInter
3838
/**
3939
* Gets the command that is executed.
4040
*
41-
* @return Command A Command instance
41+
* @return Command|null A Command instance
4242
*/
4343
publicfunctiongetCommand()
4444
{

‎src/Symfony/Component/Console/Event/ConsoleExceptionEvent.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@
1919
* Allows to handle exception thrown in a command.
2020
*
2121
* @author Fabien Potencier <fabien@symfony.com>
22+
*
23+
* @deprecated ConsoleExceptionEvent is deprecated since version 3.3 and will be removed in 4.0. Use ConsoleErrorEvent instead.
2224
*/
2325
class ConsoleExceptionEventextends ConsoleEvent
2426
{
2527
private$exception;
2628
private$exitCode;
29+
private$handled =false;
2730

28-
publicfunction__construct(Command$command,InputInterface$input,OutputInterface$output,\Exception$exception,$exitCode)
31+
publicfunction__construct(Command$command,InputInterface$input,OutputInterface$output,\Exception$exception,$exitCode,$deprecation =true)
2932
{
33+
if ($deprecation) {
34+
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use the ConsoleErrorEvent instead.',__CLASS__),E_USER_DEPRECATED);
35+
}
36+
3037
parent::__construct($command,$input,$output);
3138

32-
$this->setException($exception);
39+
$this->exception =$exception;
3340
$this->exitCode = (int)$exitCode;
3441
}
3542

‎src/Symfony/Component/Console/Event/ConsoleTerminateEvent.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ConsoleTerminateEvent extends ConsoleEvent
2929
*/
3030
private$exitCode;
3131

32-
publicfunction__construct(Command$command,InputInterface$input,OutputInterface$output,$exitCode)
32+
publicfunction__construct(Command$command =null,InputInterface$input,OutputInterface$output,$exitCode)
3333
{
3434
parent::__construct($command,$input,$output);
3535

‎src/Symfony/Component/Console/EventListener/ExceptionListener.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
usePsr\Log\LoggerInterface;
1515
useSymfony\Component\Console\Event\ConsoleEvent;
1616
useSymfony\Component\Console\ConsoleEvents;
17-
useSymfony\Component\Console\Event\ConsoleExceptionEvent;
17+
useSymfony\Component\Console\Event\ConsoleErrorEvent;
1818
useSymfony\Component\Console\Event\ConsoleTerminateEvent;
1919
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
2020

@@ -31,15 +31,15 @@ public function __construct(LoggerInterface $logger = null)
3131
$this->logger =$logger;
3232
}
3333

34-
publicfunctiononConsoleException(ConsoleExceptionEvent$event)
34+
publicfunctiononConsoleError(ConsoleErrorEvent$event)
3535
{
3636
if (null ===$this->logger) {
3737
return;
3838
}
3939

40-
$exception =$event->getException();
40+
$error =$event->getError();
4141

42-
$this->logger->error('Exception thrown while running command "{command}". Message: "{message}"',array('exception' =>$exception,'command' =>$this->getInputString($event),'message' =>$exception->getMessage()));
42+
$this->logger->error('Error thrown while running command "{command}". Message: "{message}"',array('error' =>$error,'command' =>$this->getInputString($event),'message' =>$error->getMessage()));
4343
}
4444

4545
publicfunctiononConsoleTerminate(ConsoleTerminateEvent$event)
@@ -60,7 +60,7 @@ public function onConsoleTerminate(ConsoleTerminateEvent $event)
6060
publicstaticfunctiongetSubscribedEvents()
6161
{
6262
returnarray(
63-
ConsoleEvents::EXCEPTION =>array('onConsoleException', -128),
63+
ConsoleEvents::ERROR =>array('onConsoleError', -128),
6464
ConsoleEvents::TERMINATE =>array('onConsoleTerminate', -128),
6565
);
6666
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp