Skip to content

Commit 0e93ed4

Browse files
authored
Fix StatementGenerator (#310)
1 parent 7c8ccda commit 0e93ed4

File tree

3 files changed

+5
-59
lines changed

3 files changed

+5
-59
lines changed

src/Generators/StatementGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function buildConstructor($statement)
3535

3636
$stub = $this->buildProperties($statement->data()) . PHP_EOL . PHP_EOL;
3737
$stub .= str_replace('__construct()', '__construct(' . $this->buildParameters($statement->data()) . ')', $constructor);
38-
$stub = str_replace('//', $this->buildAssignments($statement->data()), $stub);
38+
$stub = trim(str_replace('//', $this->buildAssignments($statement->data()), $stub));
3939

4040
return $stub;
4141
}

src/Generators/Statements/JobGenerator.php

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
namespace Blueprint\Generators\Statements;
44

55
use Blueprint\Blueprint;
6-
use Blueprint\Contracts\Generator;
6+
use Blueprint\Generators\StatementGenerator;
77
use Blueprint\Models\Statements\DispatchStatement;
88
use Blueprint\Tree;
99

10-
class JobGenerator implements Generator
10+
class JobGenerator extends StatementGenerator
1111
{
12-
/**
13-
* @var \Illuminate\Contracts\Filesystem\Filesystem
14-
*/
15-
private $files;
16-
17-
public function __construct($files)
18-
{
19-
$this->files = $files;
20-
}
12+
protected $new_instance = 'new job instance';
2113

2214
public function output(Tree $tree): array
2315
{
@@ -71,50 +63,4 @@ protected function populateStub(string $stub, DispatchStatement $dispatchStateme
7163

7264
return $stub;
7365
}
74-
75-
protected function buildConstructor(DispatchStatement $dispatchStatement)
76-
{
77-
static $constructor = null;
78-
79-
if (is_null($constructor)) {
80-
$constructor = str_replace('new instance', 'new job instance', $this->files->stub('partials/constructor.stub'));
81-
}
82-
83-
if (empty($dispatchStatement->data())) {
84-
return trim($constructor);
85-
}
86-
87-
$stub = $this->buildProperties($dispatchStatement->data()).PHP_EOL.PHP_EOL;
88-
$stub .= str_replace('__construct()', '__construct('.$this->buildParameters($dispatchStatement->data()).')', $constructor);
89-
$stub = str_replace('//', $this->buildAssignments($dispatchStatement->data()), $stub);
90-
91-
return $stub;
92-
}
93-
94-
private function buildProperties(array $data)
95-
{
96-
return trim(array_reduce($data, function ($output, $property) {
97-
$output .= ' public $'.$property.';'.PHP_EOL.PHP_EOL;
98-
99-
return $output;
100-
}, ''));
101-
}
102-
103-
private function buildParameters(array $data)
104-
{
105-
$parameters = array_map(function ($parameter) {
106-
return '$'.$parameter;
107-
}, $data);
108-
109-
return implode(', ', $parameters);
110-
}
111-
112-
private function buildAssignments(array $data)
113-
{
114-
return trim(array_reduce($data, function ($output, $property) {
115-
$output .= ' $this->'.$property.' = $'.$property.';'.PHP_EOL;
116-
117-
return $output;
118-
}, ''));
119-
}
12066
}

stubs/partials/constructor.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
public function __construct()
77
{
88
//
9-
}
9+
}

0 commit comments

Comments
 (0)