Skip to content

Commit 9643005

Browse files
authored
Merge pull request #6 from leeovery/0.2
Publish config file so user can control name of WorkCommand.
2 parents 426dc6f + cde844f commit 9643005

File tree

4 files changed

+135
-31
lines changed

4 files changed

+135
-31
lines changed

config/safequeue.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Worker Command Name
8+
|--------------------------------------------------------------------------
9+
|
10+
| Configure the signature / name of the Work Command here. The default
11+
| is to rename the command to 'doctrine:queue:work', however you can
12+
| rename it to whatever you want by changing this value.
13+
|
14+
| To override the Laravel 'queue:work' command name just set this
15+
| to a false value or 'queue:work'.
16+
|
17+
*/
18+
'command_name' => 'doctrine:queue:work',
19+
20+
];

src/Console/WorkCommand.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88

99
class WorkCommand extends IlluminateWorkCommand
1010
{
11-
/**
12-
* The console command name. This is now used to rename the laravel
13-
* command. It's also used in the tests to ensure the command
14-
* is renamed properly.
15-
*
16-
* @var string
17-
*/
18-
protected $name = 'doctrine:queue:work';
11+
const SIGNATURE_REGEX_PATTERN = '/([\w:-]+)(?=\s|\{)/i';
1912

2013
/**
2114
* The console command description.
@@ -26,17 +19,30 @@ class WorkCommand extends IlluminateWorkCommand
2619

2720
/**
2821
* WorkCommand constructor.
22+
*
2923
* @param Worker $worker
24+
* @param array $config
3025
*/
31-
public function __construct(Worker $worker)
26+
public function __construct(Worker $worker, $config)
3227
{
33-
$this->renameCommandInSignature();
28+
$this->renameCommandInSignature($config['command_name']);
3429

3530
parent::__construct($worker);
3631
}
3732

38-
public function renameCommandInSignature()
33+
public function renameCommandInSignature($commandName)
3934
{
40-
$this->signature = str_replace('queue:work', $this->name, $this->signature);
35+
if ($commandName) {
36+
/**
37+
* RegEx matches signature from the Laravel Worker Command that we're extending
38+
* from. Captures 1+ word characters (a-zA-Z0-9_) and/or literal colon (:),
39+
* and\or literal hyphen (-), up to the first white-space character or a
40+
* literal opening curly brace ({). The match is then replaced with the
41+
* command name from config, and the result is a renamed signature.
42+
*/
43+
$this->signature = preg_replace(
44+
self::SIGNATURE_REGEX_PATTERN, $commandName, $this->signature, 1
45+
);
46+
}
4147
}
4248
}

src/DoctrineQueueProvider.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,31 @@
1111
*/
1212
class DoctrineQueueProvider extends ServiceProvider
1313
{
14+
protected $defer = true;
15+
1416
/**
1517
* Register the service provider.
1618
*
1719
* @return void
1820
*/
1921
public function register()
2022
{
23+
$this->publishes([
24+
__DIR__ . '/../config/safequeue.php' => config_path('safequeue.php'),
25+
], 'safequeue');
26+
27+
$this->mergeConfigFrom(
28+
__DIR__ . '/../config/safequeue.php', 'safequeue'
29+
);
30+
2131
$this->registerWorker();
2232
}
2333

34+
public function boot()
35+
{
36+
$this->commands('command.safeQueue.work');
37+
}
38+
2439
/**
2540
* @return void
2641
*/
@@ -40,9 +55,23 @@ protected function registerWorker()
4055
protected function registerWorkCommand()
4156
{
4257
$this->app->singleton('command.safeQueue.work', function ($app) {
43-
return new WorkCommand($app['safeQueue.worker']);
58+
return new WorkCommand(
59+
$app['safeQueue.worker'],
60+
$app['config']->get('safequeue')
61+
);
4462
});
63+
}
4564

46-
$this->commands('command.safeQueue.work');
65+
/**
66+
* Get the services provided by the provider.
67+
*
68+
* @return array
69+
*/
70+
public function provides()
71+
{
72+
return [
73+
'safeQueue.worker',
74+
'command.safeQueue.work'
75+
];
4776
}
4877
}

tests/Console/WorkCommandTest.php

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,91 @@ class WorkCommandTest extends \PHPUnit_Framework_TestCase
2121
private $command;
2222

2323
/**
24-
* @var ReflectionClass
24+
* @var string
2525
*/
26-
private $reflectedCommand;
26+
private $defaultCommandName;
2727

2828
/**
29-
* @var string
29+
* @var array
30+
*/
31+
private $testNewCommandNames;
32+
33+
/**
34+
* @var array
3035
*/
31-
private $intendedCommandName;
36+
private $configStub;
3237

3338
protected function setUp()
3439
{
3540
$this->worker = m::mock(Worker::class);
36-
$this->command = new WorkCommand($this->worker);
37-
$this->intendedCommandName = 'doctrine:queue:work';
38-
39-
// Use reflection to peek at the worker
40-
$this->reflectedCommand = new ReflectionClass(get_class($this->command));
41+
$this->defaultCommandName = 'doctrine:queue:work';
42+
$this->configStub = ['command_name' => $this->defaultCommandName];
43+
$this->command = new WorkCommand($this->worker, $this->configStub);
44+
$this->testNewCommandNames = [
45+
'queue:work',
46+
'custom-queue:work',
47+
'doctrine-queue-work',
48+
'doctrine123-queue-work',
49+
];
4150
}
4251

4352
public function testHasCorrectWorker()
4453
{
45-
$reflectionProperty = $this->reflectedCommand->getProperty('worker');
46-
$reflectionProperty->setAccessible(true);
47-
48-
$worker = $reflectionProperty->getValue($this->command);
54+
$worker = $this->getPropertyViaReflection('worker');
4955

5056
$this->assertSame($this->worker, $worker);
5157
}
5258

53-
public function testCommandNameIsCorrect()
59+
/**
60+
* @param $propertyName
61+
* @return mixed
62+
*/
63+
private function getPropertyViaReflection($propertyName)
5464
{
55-
$reflectionProperty = $this->reflectedCommand->getProperty('signature');
65+
// Use reflection to peek at the worker
66+
$reflectedCommand = new ReflectionClass(get_class($this->command));
67+
$reflectionProperty = $reflectedCommand->getProperty($propertyName);
5668
$reflectionProperty->setAccessible(true);
5769

58-
$signature = $reflectionProperty->getValue($this->command);
70+
return $reflectionProperty->getValue($this->command);
71+
}
72+
73+
public function testCommandNameIsCorrectAsDefault()
74+
{
75+
$signature = $this->getPropertyViaReflection('signature');
76+
77+
$this->assertEquals($this->defaultCommandName, $this->getCommandNameFromSignature($signature));
78+
}
79+
80+
public function testCommandNameCanBeConfigured()
81+
{
82+
foreach($this->testNewCommandNames as $newCommandName) {
83+
84+
$this->command = new WorkCommand($this->worker, [
85+
'command_name' => $newCommandName
86+
]);
87+
88+
$signature = $this->getPropertyViaReflection('signature');
89+
90+
$this->assertEquals($newCommandName, $this->getCommandNameFromSignature($signature));
91+
}
92+
}
93+
94+
public function testCommandNameCanConfiguredToLaravelDefaultBySettingConfigValueToFalse()
95+
{
96+
$this->command = new WorkCommand($this->worker, [
97+
'command_name' => false
98+
]);
99+
100+
$signature = $this->getPropertyViaReflection('signature');
101+
102+
$this->assertEquals('queue:work', $this->getCommandNameFromSignature($signature));
103+
}
104+
105+
private function getCommandNameFromSignature($signature)
106+
{
107+
preg_match(WorkCommand::SIGNATURE_REGEX_PATTERN, $signature, $matches);
59108

60-
$this->assertContains($this->intendedCommandName, $signature);
109+
return $matches[1];
61110
}
62111
}

0 commit comments

Comments
 (0)