[go: up one dir, main page]

Skip to content

Commit

Permalink
Optimize addCrontab() and addCommand() (#43)
Browse files Browse the repository at this point in the history
* Optimize addCrontab() and addCommand()

* Fix adds Command
  • Loading branch information
huangdijia authored Jul 5, 2022
1 parent 6ebbf76 commit e1fb622
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ $app = AppFactory::create();

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

$app->run();
```
Expand Down Expand Up @@ -243,7 +243,7 @@ $app = AppFactory::create();

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

$app->run();
```
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ $app = AppFactory::create();

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

$app->run();
```
Expand Down Expand Up @@ -234,7 +234,7 @@ $app = AppFactory::create();

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

$app->run();
```
Expand Down
39 changes: 26 additions & 13 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Hyperf\Nano;

use Closure;
use Hyperf\Command\Command;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Crontab\Crontab;
Expand Down Expand Up @@ -182,40 +183,47 @@ public function addGroup($prefix, callable $callback, array $options = [])
* Add a new command.
* @param null|callable|string $command
*/
public function addCommand(string $name, $command = null)
public function addCommand(string $name, $command = null): Command
{
if ($command === null) {
$command = $name;
}

if (is_string($command)) {
$this->appendConfig('command' . $this->serverName, $command);
return;
$this->appendConfig('commands', $command);
return $this->container->get($command);
}

$command = Closure::fromCallable($command);
/** @var CommandFactory $commandFactory */
$commandFactory = $this->container->get(CommandFactory::class);
$handler = $commandFactory->create($name, $command->bindTo($this->bound, $this->bound));
$handlerId = spl_object_hash($handler);
$this->container->set($handlerId, $handler);
$this->appendConfig(
'commands',
$handlerId

return tap(
$handler,
function ($handler) {
$handlerId = spl_object_hash($handler);
$this->container->set($handlerId, $handler);
$this->appendConfig(
'commands',
$handlerId
);
}
);
}

/**
* Add a new crontab.
* @param callable|string $crontab
*/
public function addCrontab(string $rule, $crontab)
public function addCrontab(string $rule, $crontab): Crontab
{
$this->config->set('crontab.enable', true);
$this->ensureConfigHasValue('processes', CrontabDispatcherProcess::class);

if ($crontab instanceof Crontab) {
$this->appendConfig('crontab.crontab', $crontab);
return;
return $crontab;
}

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

$this->appendConfig(
'crontab.crontab',
return tap(
(new Crontab())
->setName(uniqid())
->setRule($rule)
->setCallback([CronFactory::class, 'execute', [$callbackId]])
->setCallback([CronFactory::class, 'execute', [$callbackId]]),
function ($crontab) {
$this->appendConfig(
'crontab.crontab',
$crontab
);
}
);
}

Expand Down

0 comments on commit e1fb622

Please sign in to comment.