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

Commit8be9f16

Browse files
committed
Added a console.ERROR event
1 parentb3b3dac commit8be9f16

File tree

12 files changed

+300
-55
lines changed

12 files changed

+300
-55
lines changed

‎UPGRADE-3.3.md‎

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

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

14+
Console
15+
-------
16+
17+
* The`console.exception` event and the related`ConsoleExceptionEvent` class
18+
have been deprecated in favor of the`console.error` event and the`ConsoleErrorEvent`
19+
class. The deprecated event and class will be removed in 4.0.
20+
1421
DependencyInjection
1522
-------------------
1623

‎UPGRADE-4.0.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Console
1212
* Setting unknown style options is not supported anymore and throws an
1313
exception.
1414

15+
* The`console.exception` event and the related`ConsoleExceptionEvent` class have
16+
been removed in favor of the`console.error` event and the`ConsoleErrorEvent` class.
17+
1518
Debug
1619
-----
1720

‎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
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* added`ExceptionListener`
88
* added`AddConsoleCommandPass` (originally in FrameworkBundle)
9+
* added console.error event to catch exceptions thrown by other listeners
10+
* deprecated console.exception event in favor of console.error
911

1012
3.2.0
1113
------

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,32 @@ 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. The event listener method receives
63+
* a Symfony\Component\Console\Event\ConsoleExceptionEvent
64+
* instance.
65+
*
66+
* @Event
67+
*
68+
* @var string
69+
*/
70+
constERROR ='console.error';
5371
}
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