12
12
namespace Hyperf \Nano ;
13
13
14
14
use Closure ;
15
+ use Hyperf \Command \Command ;
15
16
use Hyperf \Contract \ConfigInterface ;
16
17
use Hyperf \Contract \ContainerInterface ;
17
18
use Hyperf \Crontab \Crontab ;
@@ -182,40 +183,47 @@ public function addGroup($prefix, callable $callback, array $options = [])
182
183
* Add a new command.
183
184
* @param null|callable|string $command
184
185
*/
185
- public function addCommand (string $ name , $ command = null )
186
+ public function addCommand (string $ name , $ command = null ): Command
186
187
{
187
188
if ($ command === null ) {
188
189
$ command = $ name ;
189
190
}
190
191
191
192
if (is_string ($ command )) {
192
- $ this ->appendConfig ('command ' . $ this -> serverName , $ command );
193
- return ;
193
+ $ this ->appendConfig ('commands ' , $ command );
194
+ return $ this -> container -> get ( $ command ) ;
194
195
}
195
196
196
197
$ command = Closure::fromCallable ($ command );
198
+ /** @var CommandFactory $commandFactory */
197
199
$ commandFactory = $ this ->container ->get (CommandFactory::class);
198
200
$ handler = $ commandFactory ->create ($ name , $ command ->bindTo ($ this ->bound , $ this ->bound ));
199
- $ handlerId = spl_object_hash ($ handler );
200
- $ this ->container ->set ($ handlerId , $ handler );
201
- $ this ->appendConfig (
202
- 'commands ' ,
203
- $ handlerId
201
+
202
+ return tap (
203
+ $ handler ,
204
+ function ($ handler ) {
205
+ $ handlerId = spl_object_hash ($ handler );
206
+ $ this ->container ->set ($ handlerId , $ handler );
207
+ $ this ->appendConfig (
208
+ 'commands ' ,
209
+ $ handlerId
210
+ );
211
+ }
204
212
);
205
213
}
206
214
207
215
/**
208
216
* Add a new crontab.
209
217
* @param callable|string $crontab
210
218
*/
211
- public function addCrontab (string $ rule , $ crontab )
219
+ public function addCrontab (string $ rule , $ crontab ): Crontab
212
220
{
213
221
$ this ->config ->set ('crontab.enable ' , true );
214
222
$ this ->ensureConfigHasValue ('processes ' , CrontabDispatcherProcess::class);
215
223
216
224
if ($ crontab instanceof Crontab) {
217
225
$ this ->appendConfig ('crontab.crontab ' , $ crontab );
218
- return ;
226
+ return $ crontab ;
219
227
}
220
228
221
229
$ callback = \Closure::fromCallable ($ crontab );
@@ -225,12 +233,17 @@ public function addCrontab(string $rule, $crontab)
225
233
$ this ->ensureConfigHasValue ('processes ' , CrontabDispatcherProcess::class);
226
234
$ this ->config ->set ('crontab.enable ' , true );
227
235
228
- $ this ->appendConfig (
229
- 'crontab.crontab ' ,
236
+ return tap (
230
237
(new Crontab ())
231
238
->setName (uniqid ())
232
239
->setRule ($ rule )
233
- ->setCallback ([CronFactory::class, 'execute ' , [$ callbackId ]])
240
+ ->setCallback ([CronFactory::class, 'execute ' , [$ callbackId ]]),
241
+ function ($ crontab ) {
242
+ $ this ->appendConfig (
243
+ 'crontab.crontab ' ,
244
+ $ crontab
245
+ );
246
+ }
234
247
);
235
248
}
236
249
0 commit comments