Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}


}