Skip to content

Commit e1fb622

Browse files
authored
Optimize addCrontab() and addCommand() (#43)
* Optimize addCrontab() and addCommand() * Fix adds Command
1 parent 6ebbf76 commit e1fb622

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

README-CN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ $app = AppFactory::create();
179179

180180
$app->addCommand('echo', function(){
181181
$this->get(StdoutLoggerInterface::class)->info('A new command called echo!');
182-
});
182+
})->setDescription('Echo command.');
183183

184184
$app->run();
185185
```
@@ -243,7 +243,7 @@ $app = AppFactory::create();
243243

244244
$app->addCrontab('* * * * * *', function(){
245245
$this->get(StdoutLoggerInterface::class)->info('execute every second!');
246-
});
246+
})->setOnOneServer(true)->setMemo('Test crontab.');
247247

248248
$app->run();
249249
```

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ $app = AppFactory::create();
173173

174174
$app->addCommand('echo', function(){
175175
$this->get(StdoutLoggerInterface::class)->info('A new command called echo!');
176-
});
176+
})->setDescription('Echo command.');
177177

178178
$app->run();
179179
```
@@ -234,7 +234,7 @@ $app = AppFactory::create();
234234

235235
$app->addCrontab('* * * * * *', function(){
236236
$this->get(StdoutLoggerInterface::class)->info('execute every second!');
237-
});
237+
})->setOnOneServer(true)->setMemo('Test crontab.');
238238

239239
$app->run();
240240
```

src/App.php

+26-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Hyperf\Nano;
1313

1414
use Closure;
15+
use Hyperf\Command\Command;
1516
use Hyperf\Contract\ConfigInterface;
1617
use Hyperf\Contract\ContainerInterface;
1718
use Hyperf\Crontab\Crontab;
@@ -182,40 +183,47 @@ public function addGroup($prefix, callable $callback, array $options = [])
182183
* Add a new command.
183184
* @param null|callable|string $command
184185
*/
185-
public function addCommand(string $name, $command = null)
186+
public function addCommand(string $name, $command = null): Command
186187
{
187188
if ($command === null) {
188189
$command = $name;
189190
}
190191

191192
if (is_string($command)) {
192-
$this->appendConfig('command' . $this->serverName, $command);
193-
return;
193+
$this->appendConfig('commands', $command);
194+
return $this->container->get($command);
194195
}
195196

196197
$command = Closure::fromCallable($command);
198+
/** @var CommandFactory $commandFactory */
197199
$commandFactory = $this->container->get(CommandFactory::class);
198200
$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+
}
204212
);
205213
}
206214

207215
/**
208216
* Add a new crontab.
209217
* @param callable|string $crontab
210218
*/
211-
public function addCrontab(string $rule, $crontab)
219+
public function addCrontab(string $rule, $crontab): Crontab
212220
{
213221
$this->config->set('crontab.enable', true);
214222
$this->ensureConfigHasValue('processes', CrontabDispatcherProcess::class);
215223

216224
if ($crontab instanceof Crontab) {
217225
$this->appendConfig('crontab.crontab', $crontab);
218-
return;
226+
return $crontab;
219227
}
220228

221229
$callback = \Closure::fromCallable($crontab);
@@ -225,12 +233,17 @@ public function addCrontab(string $rule, $crontab)
225233
$this->ensureConfigHasValue('processes', CrontabDispatcherProcess::class);
226234
$this->config->set('crontab.enable', true);
227235

228-
$this->appendConfig(
229-
'crontab.crontab',
236+
return tap(
230237
(new Crontab())
231238
->setName(uniqid())
232239
->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+
}
234247
);
235248
}
236249

0 commit comments

Comments
 (0)