Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit c310e00

Browse files
authored
Merge pull request #38 from jgnovais/20190315/AdjustMakeOperationCommand
Turn possible to pass a list of jobs on the make:operation
2 parents b775f07 + 7ec5f8a commit c310e00

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/Commands/OperationMakeCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function getArguments()
8181
return [
8282
['operation', InputArgument::REQUIRED, 'The operation\'s name.'],
8383
['service', InputArgument::OPTIONAL, 'The service in which the operation should be implemented.'],
84+
['jobs', InputArgument::IS_ARRAY, 'A list of Jobs Operation calls']
8485
];
8586
}
8687

src/Generators/OperationGenerator.php

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,7 @@ public function generate($operation, $service, $isQueueable = false, array $jobs
3737

3838
$content = file_get_contents($this->getStub($isQueueable));
3939

40-
$useJobs = ''; // stores the `use` statements of the jobs
41-
$runJobs = ''; // stores the `$this->run` statements of the jobs
42-
43-
foreach ($jobs as $index => $job) {
44-
$useJobs .= 'use '.$job['namespace'].'\\'.$job['className'].";\n";
45-
$runJobs .= "\t\t".'$this->run('.$job['className'].'::class);';
46-
47-
// only add carriage returns when it's not the last job
48-
if ($index != count($jobs) - 1) {
49-
$runJobs .= "\n\n";
50-
}
51-
}
40+
list($useJobs, $runJobs) = self::getUsesAndRunners($jobs);
5241

5342
$content = str_replace(
5443
['{{operation}}', '{{namespace}}', '{{foundation_namespace}}', '{{use_jobs}}', '{{run_jobs}}'],
@@ -121,4 +110,47 @@ private function getTestStub()
121110
{
122111
return __DIR__.'/stubs/operation-test.stub';
123112
}
113+
114+
/**
115+
* Get de use to import the right class
116+
* Get de job run command
117+
* @param $job
118+
* @return array
119+
* @author jgnovais <[email protected]>
120+
*/
121+
static private function getUseAndJobRunCommand($job)
122+
{
123+
$str = str_replace_last('\\','#', $job);
124+
$explode = explode('#', $str);
125+
126+
$use = 'use '.$explode[0].'\\'.$explode['1'].";\n";
127+
$runJobs = "\t\t".'$this->run('.$explode['1'].'::class);';
128+
129+
return array($use, $runJobs);
130+
}
131+
132+
/**
133+
* Returns all users and all $this->run() generated
134+
* @param $jobs
135+
* @return array
136+
* @author jgnovais <[email protected]>
137+
*/
138+
static private function getUsesAndRunners($jobs)
139+
{
140+
$useJobs = '';
141+
$runJobs = '';
142+
foreach ($jobs as $index => $job) {
143+
144+
list($useLine, $runLine) = self::getUseAndJobRunCommand($job);
145+
$useJobs .= $useLine;
146+
$runJobs .= $runLine;
147+
// only add carriage returns when it's not the last job
148+
if ($index != count($jobs) - 1) {
149+
$runJobs .= "\n\n";
150+
}
151+
}
152+
return array($useJobs, $runJobs);
153+
}
154+
155+
124156
}

0 commit comments

Comments
 (0)