22
22
use Symfony \Component \Console \Completion \CompletionInput ;
23
23
use Symfony \Component \Console \Completion \CompletionSuggestions ;
24
24
use Symfony \Component \Console \Completion \Suggestion ;
25
+ use Symfony \Component \Console \Event \ConsoleAlarmEvent ;
25
26
use Symfony \Component \Console \Event \ConsoleCommandEvent ;
26
27
use Symfony \Component \Console \Event \ConsoleErrorEvent ;
27
28
use Symfony \Component \Console \Event \ConsoleSignalEvent ;
@@ -88,6 +89,7 @@ class Application implements ResetInterface
88
89
private bool $ initialized = false ;
89
90
private ?SignalRegistry $ signalRegistry = null ;
90
91
private array $ signalsToDispatchEvent = [];
92
+ private ?int $ alarmInterval = null ;
91
93
92
94
public function __construct (
93
95
private string $ name = 'UNKNOWN ' ,
@@ -97,7 +99,7 @@ public function __construct(
97
99
$ this ->defaultCommand = 'list ' ;
98
100
if (\defined ('SIGINT ' ) && SignalRegistry::isSupported ()) {
99
101
$ this ->signalRegistry = new SignalRegistry ();
100
- $ this ->signalsToDispatchEvent = [\SIGINT , \SIGQUIT , \SIGTERM , \SIGUSR1 , \SIGUSR2 ];
102
+ $ this ->signalsToDispatchEvent = [\SIGINT , \SIGQUIT , \SIGTERM , \SIGUSR1 , \SIGUSR2 , \ SIGALRM ];
101
103
}
102
104
}
103
105
@@ -128,6 +130,19 @@ public function setSignalsToDispatchEvent(int ...$signalsToDispatchEvent): void
128
130
$ this ->signalsToDispatchEvent = $ signalsToDispatchEvent ;
129
131
}
130
132
133
+ public function setAlarmInterval (?int $ interval ): void
134
+ {
135
+ $ this ->alarmInterval = $ interval ;
136
+ $ this ->scheduleAlarm ();
137
+ }
138
+
139
+ private function scheduleAlarm (): void
140
+ {
141
+ if (null !== $ this ->alarmInterval ) {
142
+ $ this ->getSignalRegistry ()->scheduleAlarm ($ this ->alarmInterval );
143
+ }
144
+ }
145
+
131
146
/**
132
147
* Runs the current application.
133
148
*
@@ -975,34 +990,47 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
975
990
976
991
$ commandSignals = $ command instanceof SignalableCommandInterface ? $ command ->getSubscribedSignals () : [];
977
992
if ($ commandSignals || $ this ->dispatcher && $ this ->signalsToDispatchEvent ) {
978
- if (!$ this ->signalRegistry ) {
979
- throw new RuntimeException ('Unable to subscribe to signal events. Make sure that the "pcntl" extension is installed and that "pcntl_*" functions are not disabled by your php.ini \'s "disable_functions" directive. ' );
980
- }
993
+ $ signalRegistry = $ this ->getSignalRegistry ();
981
994
982
995
if (Terminal::hasSttyAvailable ()) {
983
996
$ sttyMode = shell_exec ('stty -g ' );
984
997
985
998
foreach ([\SIGINT , \SIGQUIT , \SIGTERM ] as $ signal ) {
986
- $ this -> signalRegistry ->register ($ signal , static fn () => shell_exec ('stty ' .$ sttyMode ));
999
+ $ signalRegistry ->register ($ signal , static fn () => shell_exec ('stty ' .$ sttyMode ));
987
1000
}
988
1001
}
989
1002
990
1003
if ($ this ->dispatcher ) {
991
1004
// We register application signals, so that we can dispatch the event
992
1005
foreach ($ this ->signalsToDispatchEvent as $ signal ) {
993
- $ event = new ConsoleSignalEvent ($ command , $ input , $ output , $ signal );
994
-
995
- $ this ->signalRegistry ->register ($ signal , function ($ signal ) use ($ event , $ command , $ commandSignals ) {
996
- $ this ->dispatcher ->dispatch ($ event , ConsoleEvents::SIGNAL );
997
- $ exitCode = $ event ->getExitCode ();
1006
+ $ signalEvent = new ConsoleSignalEvent ($ command , $ input , $ output , $ signal );
1007
+ $ alarmEvent = \SIGALRM === $ signal ? new ConsoleAlarmEvent ($ command , $ input , $ output ) : null ;
1008
+
1009
+ $ signalRegistry ->register ($ signal , function ($ signal ) use ($ signalEvent , $ alarmEvent , $ command , $ commandSignals , $ input , $ output ) {
1010
+ $ this ->dispatcher ->dispatch ($ signalEvent , ConsoleEvents::SIGNAL );
1011
+ $ exitCode = $ signalEvent ->getExitCode ();
1012
+
1013
+ if (null !== $ alarmEvent ) {
1014
+ if (false !== $ exitCode ) {
1015
+ $ alarmEvent ->setExitCode ($ exitCode );
1016
+ } else {
1017
+ $ alarmEvent ->abortExit ();
1018
+ }
1019
+ $ this ->dispatcher ->dispatch ($ alarmEvent , ConsoleEvents::ALARM );
1020
+ $ exitCode = $ alarmEvent ->getExitCode ();
1021
+ }
998
1022
999
1023
// If the command is signalable, we call the handleSignal() method
1000
1024
if (\in_array ($ signal , $ commandSignals , true )) {
1001
1025
$ exitCode = $ command ->handleSignal ($ signal , $ exitCode );
1002
1026
}
1003
1027
1028
+ if (\SIGALRM === $ signal ) {
1029
+ $ this ->scheduleAlarm ();
1030
+ }
1031
+
1004
1032
if (false !== $ exitCode ) {
1005
- $ event = new ConsoleTerminateEvent ($ command , $ event -> getInput () , $ event -> getOutput () , $ exitCode , $ signal );
1033
+ $ event = new ConsoleTerminateEvent ($ command , $ input , $ output , $ exitCode , $ signal );
1006
1034
$ this ->dispatcher ->dispatch ($ event , ConsoleEvents::TERMINATE );
1007
1035
1008
1036
exit ($ event ->getExitCode ());
@@ -1015,7 +1043,11 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
1015
1043
}
1016
1044
1017
1045
foreach ($ commandSignals as $ signal ) {
1018
- $ this ->signalRegistry ->register ($ signal , function (int $ signal ) use ($ command ): void {
1046
+ $ signalRegistry ->register ($ signal , function (int $ signal ) use ($ command ): void {
1047
+ if (\SIGALRM === $ signal ) {
1048
+ $ this ->scheduleAlarm ();
1049
+ }
1050
+
1019
1051
if (false !== $ exitCode = $ command ->handleSignal ($ signal )) {
1020
1052
exit ($ exitCode );
1021
1053
}
0 commit comments