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

Commit

Permalink
Merge pull request #38 from jgnovais/20190315/AdjustMakeOperationCommand
Browse files Browse the repository at this point in the history
Turn possible to pass a list of jobs on the make:operation
  • Loading branch information
Mulkave authored Feb 17, 2020
2 parents b775f07 + 7ec5f8a commit c310e00
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Commands/OperationMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function getArguments()
return [
['operation', InputArgument::REQUIRED, 'The operation\'s name.'],
['service', InputArgument::OPTIONAL, 'The service in which the operation should be implemented.'],
['jobs', InputArgument::IS_ARRAY, 'A list of Jobs Operation calls']
];
}

Expand Down
56 changes: 44 additions & 12 deletions src/Generators/OperationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,7 @@ public function generate($operation, $service, $isQueueable = false, array $jobs

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

$useJobs = ''; // stores the `use` statements of the jobs
$runJobs = ''; // stores the `$this->run` statements of the jobs

foreach ($jobs as $index => $job) {
$useJobs .= 'use '.$job['namespace'].'\\'.$job['className'].";\n";
$runJobs .= "\t\t".'$this->run('.$job['className'].'::class);';

// only add carriage returns when it's not the last job
if ($index != count($jobs) - 1) {
$runJobs .= "\n\n";
}
}
list($useJobs, $runJobs) = self::getUsesAndRunners($jobs);

$content = str_replace(
['{{operation}}', '{{namespace}}', '{{foundation_namespace}}', '{{use_jobs}}', '{{run_jobs}}'],
Expand Down Expand Up @@ -121,4 +110,47 @@ private function getTestStub()
{
return __DIR__.'/stubs/operation-test.stub';
}

/**
* Get de use to import the right class
* Get de job run command
* @param $job
* @return array
* @author jgnovais <jgnx@hotmail.com>
*/
static private function getUseAndJobRunCommand($job)
{
$str = str_replace_last('\\','#', $job);
$explode = explode('#', $str);

$use = 'use '.$explode[0].'\\'.$explode['1'].";\n";
$runJobs = "\t\t".'$this->run('.$explode['1'].'::class);';

return array($use, $runJobs);
}

/**
* Returns all users and all $this->run() generated
* @param $jobs
* @return array
* @author jgnovais <jgnx@hotmail.com>
*/
static private function getUsesAndRunners($jobs)
{
$useJobs = '';
$runJobs = '';
foreach ($jobs as $index => $job) {

list($useLine, $runLine) = self::getUseAndJobRunCommand($job);
$useJobs .= $useLine;
$runJobs .= $runLine;
// only add carriage returns when it's not the last job
if ($index != count($jobs) - 1) {
$runJobs .= "\n\n";
}
}
return array($useJobs, $runJobs);
}


}

0 comments on commit c310e00

Please sign in to comment.