From 674b6da0bdd59741c3af9b9a9ddf48dad2dff00f Mon Sep 17 00:00:00 2001 From: Kennedy Tedesco Date: Sat, 24 Sep 2016 13:12:41 -0300 Subject: [PATCH 1/3] [5.3] Option to schedule tasks using the command class instead of it's signature --- src/Illuminate/Console/Scheduling/Schedule.php | 4 ++++ tests/Console/ConsoleEventSchedulerTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index f5ee9cf813bc..71f99172d481 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -37,6 +37,10 @@ public function call($callback, array $parameters = []) */ public function command($command, array $parameters = []) { + if(class_exists($command)) { + $command = (new $command)->getName(); + } + $binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)); $artisan = defined('ARTISAN_BINARY') ? ProcessUtils::escapeArgument(ARTISAN_BINARY) : 'artisan'; diff --git a/tests/Console/ConsoleEventSchedulerTest.php b/tests/Console/ConsoleEventSchedulerTest.php index 9e08544c6239..ea9adc9b62d2 100644 --- a/tests/Console/ConsoleEventSchedulerTest.php +++ b/tests/Console/ConsoleEventSchedulerTest.php @@ -47,4 +47,20 @@ public function testCommandCreatesNewArtisanCommand() $this->assertEquals($binary.' artisan queue:listen --tries=3', $events[1]->command); $this->assertEquals($binary.' artisan queue:listen --tries=3', $events[2]->command); } + + public function testCreateNewArtisanCommandUsingCommandClass() + { + $escape = '\\' === DIRECTORY_SEPARATOR ? '"' : '\''; + + $schedule = new Schedule; + $schedule->command(ConsoleCommandStub::class, ['--force']); + + $events = $schedule->events(); + $binary = $escape.PHP_BINARY.$escape; + $this->assertEquals($binary.' artisan foo:bar --force', $events[0]->command); + } +} + +class ConsoleCommandStub extends Illuminate\Console\Command { + protected $signature = 'foo:bar'; } From 3b89d53455f131e1172c26476638d07675a8a550 Mon Sep 17 00:00:00 2001 From: Kennedy Tedesco Date: Sat, 24 Sep 2016 13:22:38 -0300 Subject: [PATCH 2/3] CS changes --- src/Illuminate/Console/Scheduling/Schedule.php | 2 +- tests/Console/ConsoleEventSchedulerTest.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index 71f99172d481..658ab4b20f76 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -37,7 +37,7 @@ public function call($callback, array $parameters = []) */ public function command($command, array $parameters = []) { - if(class_exists($command)) { + if (class_exists($command)) { $command = (new $command)->getName(); } diff --git a/tests/Console/ConsoleEventSchedulerTest.php b/tests/Console/ConsoleEventSchedulerTest.php index ea9adc9b62d2..0248c3f0a6d2 100644 --- a/tests/Console/ConsoleEventSchedulerTest.php +++ b/tests/Console/ConsoleEventSchedulerTest.php @@ -61,6 +61,7 @@ public function testCreateNewArtisanCommandUsingCommandClass() } } -class ConsoleCommandStub extends Illuminate\Console\Command { +class ConsoleCommandStub extends Illuminate\Console\Command +{ protected $signature = 'foo:bar'; } From 6219249b56f38784aead83ee4381f34c255f6e44 Mon Sep 17 00:00:00 2001 From: Kennedy Tedesco Date: Sun, 25 Sep 2016 20:27:11 -0300 Subject: [PATCH 3/3] Resolving the instance --- .../Console/Scheduling/Schedule.php | 2 +- tests/Console/ConsoleEventSchedulerTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index 658ab4b20f76..24742d1d3e19 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -38,7 +38,7 @@ public function call($callback, array $parameters = []) public function command($command, array $parameters = []) { if (class_exists($command)) { - $command = (new $command)->getName(); + $command = app($command)->getName(); } $binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)); diff --git a/tests/Console/ConsoleEventSchedulerTest.php b/tests/Console/ConsoleEventSchedulerTest.php index 0248c3f0a6d2..435fb811310f 100644 --- a/tests/Console/ConsoleEventSchedulerTest.php +++ b/tests/Console/ConsoleEventSchedulerTest.php @@ -61,7 +61,26 @@ public function testCreateNewArtisanCommandUsingCommandClass() } } +class FooClassStub +{ + protected $schedule; + + public function __construct(Schedule $schedule) + { + $this->schedule = $schedule; + } +} + class ConsoleCommandStub extends Illuminate\Console\Command { protected $signature = 'foo:bar'; + + protected $foo; + + public function __construct(FooClassStub $foo) + { + parent::__construct(); + + $this->foo = $foo; + } }