Skip to content

Commit 0ea6154

Browse files
authored
Extract common logic to abstract generator (#303)
1 parent df59ded commit 0ea6154

File tree

5 files changed

+83
-178
lines changed

5 files changed

+83
-178
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Blueprint\Generators;
4+
5+
use Blueprint\Contracts\Generator;
6+
7+
abstract class StatementGenerator implements Generator
8+
{
9+
/**
10+
* @var \Illuminate\Contracts\Filesystem\Filesystem
11+
*/
12+
protected $files;
13+
14+
/**
15+
* @var string
16+
*/
17+
protected $new_instance = 'new instance';
18+
19+
public function __construct($files)
20+
{
21+
$this->files = $files;
22+
}
23+
24+
protected function buildConstructor($statement)
25+
{
26+
static $constructor = null;
27+
28+
if (is_null($constructor)) {
29+
$constructor = str_replace('new instance', $this->new_instance, $this->files->stub('partials/constructor.stub'));
30+
}
31+
32+
if (empty($statement->data())) {
33+
return trim($constructor);
34+
}
35+
36+
$stub = $this->buildProperties($statement->data()) . PHP_EOL . PHP_EOL;
37+
$stub .= str_replace('__construct()', '__construct(' . $this->buildParameters($statement->data()) . ')', $constructor);
38+
$stub = str_replace('//', $this->buildAssignments($statement->data()), $stub);
39+
40+
return $stub;
41+
}
42+
43+
protected function buildProperties(array $data)
44+
{
45+
return trim(array_reduce($data, function ($output, $property) {
46+
$output .= ' public $' . $property . ';' . PHP_EOL . PHP_EOL;
47+
48+
return $output;
49+
}, ''));
50+
}
51+
52+
protected function buildAssignments(array $data)
53+
{
54+
return trim(array_reduce($data, function ($output, $property) {
55+
$output .= ' $this->' . $property . ' = $' . $property . ';' . PHP_EOL;
56+
57+
return $output;
58+
}, ''));
59+
}
60+
61+
protected function buildParameters(array $data)
62+
{
63+
$parameters = array_map(function ($parameter) {
64+
return '$' . $parameter;
65+
}, $data);
66+
67+
return implode(', ', $parameters);
68+
}
69+
}

src/Generators/Statements/EventGenerator.php

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@
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\FireStatement;
88

9-
class EventGenerator implements Generator
9+
class EventGenerator extends StatementGenerator
1010
{
11-
/**
12-
* @var \Illuminate\Contracts\Filesystem\Filesystem
13-
*/
14-
private $files;
15-
16-
public function __construct($files)
17-
{
18-
$this->files = $files;
19-
}
11+
protected $new_instance = 'new event instance';
2012

2113
public function output(array $tree): array
2214
{
@@ -74,50 +66,4 @@ protected function populateStub(string $stub, FireStatement $fireStatement)
7466

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

src/Generators/Statements/MailGenerator.php

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@
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\SendStatement;
88

9-
class MailGenerator implements Generator
9+
class MailGenerator extends StatementGenerator
1010
{
11-
/**
12-
* @var \Illuminate\Contracts\Filesystem\Filesystem
13-
*/
14-
private $files;
15-
16-
public function __construct($files)
17-
{
18-
$this->files = $files;
19-
}
11+
protected $new_instance = 'new message instance';
2012

2113
public function output(array $tree): array
2214
{
@@ -74,50 +66,4 @@ protected function populateStub(string $stub, SendStatement $sendStatement)
7466

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

src/Generators/Statements/NotificationGenerator.php

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,12 @@
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\SendStatement;
88

9-
class NotificationGenerator implements Generator
9+
class NotificationGenerator extends StatementGenerator
1010
{
11-
/**
12-
* @
13-
\Illuminate\Contracts\Filesystem\Filesystem
14-
*/
15-
private $files;
16-
17-
public function __construct($files)
18-
{
19-
$this->files = $files;
20-
}
11+
protected $new_instance = 'new message instance';
2112

2213
public function output(array $tree): array
2314
{
@@ -29,11 +20,11 @@ public function output(array $tree): array
2920
foreach ($tree['controllers'] as $controller) {
3021
foreach ($controller->methods() as $method => $statements) {
3122
foreach ($statements as $statement) {
32-
if (! $statement instanceof SendStatement) {
23+
if (!$statement instanceof SendStatement) {
3324
continue;
3425
}
3526

36-
if (! $statement->isNotification()) {
27+
if (!$statement->isNotification()) {
3728
continue;
3829
}
3930

@@ -43,7 +34,7 @@ public function output(array $tree): array
4334
continue;
4435
}
4536

46-
if (! $this->files->exists(dirname($path))) {
37+
if (!$this->files->exists(dirname($path))) {
4738
$this->files->makeDirectory(dirname($path), 0755, true);
4839
}
4940

@@ -64,61 +55,15 @@ public function types(): array
6455

6556
protected function getPath(string $name)
6657
{
67-
return Blueprint::appPath().'/Notification/'.$name.'.php';
58+
return Blueprint::appPath() . '/Notification/' . $name . '.php';
6859
}
6960

7061
protected function populateStub(string $stub, SendStatement $sendStatement)
7162
{
72-
$stub = str_replace('DummyNamespace', config('blueprint.namespace').'\\Notification', $stub);
63+
$stub = str_replace('DummyNamespace', config('blueprint.namespace') . '\\Notification', $stub);
7364
$stub = str_replace('DummyClass', $sendStatement->mail(), $stub);
7465
$stub = str_replace('// properties...', $this->buildConstructor($sendStatement), $stub);
7566

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

src/Models/Statements/FireStatement.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace Blueprint\Models\Statements;
54

65
class FireStatement

0 commit comments

Comments
 (0)