22
33namespace Stackkit \LaravelGoogleCloudScheduler ;
44
5+ use Illuminate \Console \Scheduling \Schedule ;
6+ use Illuminate \Container \Container ;
7+ use Illuminate \Contracts \Console \Kernel ;
58use Illuminate \Http \Request ;
69use Illuminate \Support \Facades \Artisan ;
710
@@ -10,12 +13,24 @@ class TaskHandler
1013 private $ command ;
1114 private $ request ;
1215 private $ openId ;
16+ private $ kernel ;
17+ private $ schedule ;
18+ private $ container ;
1319
14- public function __construct (Command $ command , Request $ request , OpenIdVerificator $ openId )
15- {
20+ public function __construct (
21+ Command $ command ,
22+ Request $ request ,
23+ OpenIdVerificator $ openId ,
24+ Kernel $ kernel ,
25+ Schedule $ schedule ,
26+ Container $ container
27+ ) {
1628 $ this ->command = $ command ;
1729 $ this ->request = $ request ;
1830 $ this ->openId = $ openId ;
31+ $ this ->kernel = $ kernel ;
32+ $ this ->schedule = $ schedule ;
33+ $ this ->container = $ container ;
1934 }
2035
2136 /**
@@ -27,9 +42,7 @@ public function handle()
2742
2843 set_time_limit (0 );
2944
30- Artisan::call ($ this ->command ->captureWithoutArtisan ());
31-
32- $ output = Artisan::output ();
45+ $ output = $ this ->runCommand ($ this ->command ->captureWithoutArtisan ());
3346
3447 return $ this ->cleanOutput ($ output );
3548 }
@@ -50,6 +63,54 @@ private function authorizeRequest()
5063 $ this ->openId ->guardAgainstInvalidOpenIdToken ($ decodedToken );
5164 }
5265
66+ private function runCommand ($ command )
67+ {
68+ if ($ this ->isScheduledCommand ($ command )) {
69+ $ scheduledCommand = $ this ->getScheduledCommand ($ command );
70+
71+ if ($ scheduledCommand ->withoutOverlapping && ! $ scheduledCommand ->mutex ->create ($ scheduledCommand )) {
72+ return null ;
73+ }
74+
75+ $ scheduledCommand ->callBeforeCallbacks ($ this ->container );
76+
77+ Artisan::call ($ command );
78+
79+ $ scheduledCommand ->callAfterCallbacks ($ this ->container );
80+ } else {
81+ Artisan::call ($ command );
82+ }
83+
84+ return Artisan::output ();
85+ }
86+
87+ private function isScheduledCommand ($ command )
88+ {
89+ return !is_null ($ this ->getScheduledCommand ($ command ));
90+ }
91+
92+ private function getScheduledCommand ($ command )
93+ {
94+ $ events = $ this ->schedule ->events ();
95+
96+ foreach ($ events as $ event ) {
97+ $ eventCommand = $ this ->commandWithoutArtisan ($ event ->command );
98+
99+ if ($ command === $ eventCommand ) {
100+ return $ event ;
101+ }
102+ }
103+
104+ return null ;
105+ }
106+
107+ private function commandWithoutArtisan ($ command )
108+ {
109+ $ parts = explode ('artisan ' , $ command );
110+
111+ return substr ($ parts [1 ], 2 , strlen ($ parts [1 ]));
112+ }
113+
53114 private function cleanOutput ($ output )
54115 {
55116 return trim ($ output );
0 commit comments