Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #54 from etahamer/main
Browse files Browse the repository at this point in the history
PendingShortScheduleCommand::command method will attempt to resolve command name if class name was given
  • Loading branch information
freekmurze authored Jan 10, 2022
2 parents 6fc0840 + 9115fb8 commit 75dc0e5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ protected function shortSchedule(\Spatie\ShortSchedule\ShortSchedule $shortSched

// this command will run every half a second
$shortSchedule->command('another-artisan-command')->everySeconds(0.5);

// this command will run every second and its signature will be retrieved from command automatically
$shortSchedule->command(\Spatie\ShortSchedule\Tests\Unit\TestCommand::class)->everySecond();
}
```

Expand Down Expand Up @@ -89,6 +92,9 @@ protected function shortSchedule(\Spatie\ShortSchedule\ShortSchedule $shortSched
{
// this artisan command will run every second
$shortSchedule->command('artisan-command')->everySecond();

// this artisan command will run every second, its signature will be resolved from container
$shortSchedule->command(\Spatie\ShortSchedule\Tests\Unit\TestCommand::class)->everySecond();
}
```

Expand Down
7 changes: 6 additions & 1 deletion src/PendingShortScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\ShortSchedule;

use Closure;
use Illuminate\Container\Container;
use Illuminate\Support\Arr;
use Spatie\ShortSchedule\RunConstraints\BetweenConstraint;
use Spatie\ShortSchedule\RunConstraints\EnvironmentConstraint;
Expand Down Expand Up @@ -35,8 +36,12 @@ public function everySeconds(float $frequencyInSeconds = 1): self
return $this;
}

public function command(string $artisanCommand):self
public function command(string $artisanCommand): self
{
if (class_exists($artisanCommand)) {
$artisanCommand = app($artisanCommand)->getName();
}

$this->command = PHP_BINARY . " artisan {$artisanCommand}";

return $this;
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/PendingShortScheduleCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace Spatie\ShortSchedule\Tests\Unit;

use Illuminate\Console\Command;
use Orchestra\Testbench\TestCase as Orchestra;
use ReflectionClass;
use Spatie\ShortSchedule\PendingShortScheduleCommand;

class PendingShortScheduleCommandTest extends Orchestra
{
/** @test */
public function it_will_generate_command_from_command_class(): void
{
$pendingCommand = new PendingShortScheduleCommand();
$pendingCommand->command(TestCommand::class);
$reflectionClass = new ReflectionClass($pendingCommand);

$commandProperty = $reflectionClass->getProperty('command');
$commandProperty->setAccessible(true);

$artisanCommand = 'test-command';
$this->assertEquals(PHP_BINARY . " artisan {$artisanCommand}", $commandProperty->getValue($pendingCommand));
}
}

class TestCommand extends Command
{
protected $signature = 'test-command';

public function handle(): void
{
}
}

0 comments on commit 75dc0e5

Please sign in to comment.