Skip to content

Commit d3d33a0

Browse files
authored
Refactor to abstract class and specific traits (#532)
1 parent 94b570d commit d3d33a0

26 files changed

+274
-590
lines changed

src/Blueprint.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Blueprint\Contracts\Generator;
66
use Blueprint\Contracts\Lexer;
7-
use Illuminate\Support\Facades\App;
87
use Illuminate\Support\Str;
98
use Symfony\Component\Yaml\Yaml;
109

src/Concerns/HandlesImports.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Blueprint\Concerns;
4+
5+
use Blueprint\Contracts\Model;
6+
7+
trait HandlesImports
8+
{
9+
protected $imports = [];
10+
11+
protected function addImport(Model $model, $class)
12+
{
13+
$this->imports[$model->name()][] = $class;
14+
}
15+
16+
protected function buildImports(Model $model)
17+
{
18+
return collect($this->imports[$model->name()])
19+
->map(function ($class) {
20+
return "use {$class};";
21+
})
22+
->unique()
23+
->sort()
24+
->implode(PHP_EOL);
25+
}
26+
}

src/Concerns/HandlesTraits.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Blueprint\Concerns;
4+
5+
use Blueprint\Contracts\Model;
6+
7+
trait HandlesTraits
8+
{
9+
protected $traits = [];
10+
11+
protected function addTrait(Model $model, $trait)
12+
{
13+
$this->traits[$model->name()][] = $trait;
14+
}
15+
16+
protected function buildTraits(Model $model)
17+
{
18+
if (empty($this->traits[$model->name()])) {
19+
return '';
20+
}
21+
22+
$traits = collect($this->traits[$model->name()])
23+
->unique()
24+
->sort()
25+
->implode(', ');
26+
27+
return "use {$traits};";
28+
}
29+
}

src/Contracts/Model.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Blueprint\Contracts;
4+
5+
interface Model
6+
{
7+
public function name();
8+
9+
public function namespace();
10+
11+
public function fullyQualifiedNamespace();
12+
13+
public function fullyQualifiedClassName();
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Blueprint\Generators;
4+
5+
use Blueprint\Blueprint;
6+
use Blueprint\Contracts\Model;
7+
use Illuminate\Filesystem\Filesystem;
8+
9+
class AbstractClassGenerator
10+
{
11+
const INDENT = ' ';
12+
13+
protected $filesystem;
14+
15+
private $tree;
16+
17+
protected $output = [];
18+
19+
public function __construct(Filesystem $filesystem)
20+
{
21+
$this->filesystem = $filesystem;
22+
}
23+
24+
public function types(): array
25+
{
26+
return $this->types;
27+
}
28+
29+
protected function getPath(Model $model)
30+
{
31+
$path = str_replace('\\', '/', Blueprint::relativeNamespace($model->fullyQualifiedClassName()));
32+
return sprintf('%s/%s.php', $this->basePath ?? Blueprint::appPath(), $path);
33+
}
34+
35+
protected function create(string $path, $content)
36+
{
37+
if (!$this->filesystem->exists(dirname($path))) {
38+
$this->filesystem->makeDirectory(dirname($path), 0755, true);
39+
}
40+
41+
$this->filesystem->put($path, $content);
42+
43+
$this->output['created'][] = $path;
44+
}
45+
}

src/Generators/ControllerGenerator.php

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace Blueprint\Generators;
44

55
use Blueprint\Blueprint;
6+
use Blueprint\Concerns\HandlesImports;
7+
use Blueprint\Concerns\HandlesTraits;
68
use Blueprint\Contracts\Generator;
79
use Blueprint\Models\Controller;
10+
use Blueprint\Models\Model;
811
use Blueprint\Models\Statements\DispatchStatement;
912
use Blueprint\Models\Statements\EloquentStatement;
1013
use Blueprint\Models\Statements\FireStatement;
@@ -17,63 +20,32 @@
1720
use Blueprint\Models\Statements\SessionStatement;
1821
use Blueprint\Models\Statements\ValidateStatement;
1922
use Blueprint\Tree;
20-
use Illuminate\Filesystem\Filesystem;
2123
use Illuminate\Support\Str;
2224

23-
class ControllerGenerator implements Generator
25+
class ControllerGenerator extends AbstractClassGenerator implements Generator
2426
{
25-
const INDENT = ' ';
27+
use HandlesImports, HandlesTraits;
2628

27-
/**
28-
* @var Filesystem
29-
*/
30-
protected $filesystem;
31-
32-
private $imports = [];
33-
34-
/**
35-
* @var Tree
36-
*/
37-
private $tree;
38-
39-
public function __construct(Filesystem $filesystem)
40-
{
41-
$this->filesystem = $filesystem;
42-
}
29+
protected $types = ['controllers'];
4330

4431
public function output(Tree $tree): array
4532
{
4633
$this->tree = $tree;
4734

48-
$output = [];
49-
5035
$stub = $this->filesystem->stub('controller.class.stub');
5136

5237
/** @var \Blueprint\Models\Controller $controller */
5338
foreach ($tree->controllers() as $controller) {
5439
$this->addImport($controller, 'Illuminate\\Http\\Request');
55-
5640
if ($controller->fullyQualifiedNamespace() !== 'App\\Http\\Controllers') {
5741
$this->addImport($controller, 'App\\Http\\Controllers\\Controller');
5842
}
59-
6043
$path = $this->getPath($controller);
6144

62-
if (!$this->filesystem->exists(dirname($path))) {
63-
$this->filesystem->makeDirectory(dirname($path), 0755, true);
64-
}
65-
66-
$this->filesystem->put($path, $this->populateStub($stub, $controller));
67-
68-
$output['created'][] = $path;
45+
$this->create($path, $this->populateStub($stub, $controller));
6946
}
7047

71-
return $output;
72-
}
73-
74-
public function types(): array
75-
{
76-
return ['controllers'];
48+
return $this->output;
7749
}
7850

7951
protected function populateStub(string $stub, Controller $controller)
@@ -149,7 +121,7 @@ protected function buildMethods(Controller $controller)
149121
$body .= self::INDENT . $statement->output() . PHP_EOL;
150122

151123
if ($statement->paginate()) {
152-
if (! Str::contains($body, '::all();')) {
124+
if (!Str::contains($body, '::all();')) {
153125
$queryStatement = new QueryStatement('all', [$statement->reference()]);
154126
$body = implode(PHP_EOL, [
155127
self::INDENT . $queryStatement->output($statement->reference()),
@@ -196,34 +168,6 @@ protected function buildMethods(Controller $controller)
196168
return trim($methods);
197169
}
198170

199-
protected function getPath(Controller $controller)
200-
{
201-
$path = str_replace('\\', '/', Blueprint::relativeNamespace($controller->fullyQualifiedClassName()));
202-
203-
return Blueprint::appPath() . '/' . $path . '.php';
204-
}
205-
206-
protected function buildImports(Controller $controller)
207-
{
208-
$imports = array_unique($this->imports[$controller->name()]);
209-
sort($imports);
210-
211-
return implode(
212-
PHP_EOL,
213-
array_map(
214-
function ($class) {
215-
return 'use ' . $class . ';';
216-
},
217-
$imports
218-
)
219-
);
220-
}
221-
222-
private function addImport(Controller $controller, $class)
223-
{
224-
$this->imports[$controller->name()][] = $class;
225-
}
226-
227171
private function determineModel(Controller $controller, ?string $reference)
228172
{
229173
if (empty($reference) || $reference === 'id') {

src/Generators/FactoryGenerator.php

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,27 @@
33
namespace Blueprint\Generators;
44

55
use Blueprint\Blueprint;
6+
use Blueprint\Concerns\HandlesImports;
7+
use Blueprint\Concerns\HandlesTraits;
68
use Blueprint\Contracts\Generator;
9+
use Blueprint\Contracts\Model as BlueprintModel;
710
use Blueprint\Models\Column;
811
use Blueprint\Models\Model;
912
use Blueprint\Tree;
10-
use Illuminate\Filesystem\Filesystem;
1113
use Illuminate\Support\Str;
1214
use Shift\Faker\Registry as FakerRegistry;
1315

14-
class FactoryGenerator implements Generator
16+
class FactoryGenerator extends AbstractClassGenerator implements Generator
1517
{
16-
const INDENT = ' ';
17-
18-
/**
19-
* @var Filesystem
20-
*/
21-
protected $filesystem;
22-
23-
/**
24-
* @var Tree
25-
*/
26-
private $tree;
18+
use HandlesImports, HandlesTraits;
2719

28-
private $imports = [];
20+
const INDENT = ' ';
2921

30-
public function __construct(Filesystem $filesystem)
31-
{
32-
$this->filesystem = $filesystem;
33-
}
22+
protected $types = ['factories'];
3423

3524
public function output(Tree $tree): array
3625
{
3726
$this->tree = $tree;
38-
39-
$output = [];
4027
$stub = $this->filesystem->stub('factory.stub');
4128

4229
/**
@@ -47,28 +34,17 @@ public function output(Tree $tree): array
4734

4835
$path = $this->getPath($model);
4936

50-
if (!$this->filesystem->exists(dirname($path))) {
51-
$this->filesystem->makeDirectory(dirname($path), 0755, true);
52-
}
53-
54-
$this->filesystem->put($path, $this->populateStub($stub, $model));
55-
56-
$output['created'][] = $path;
37+
$this->create($path, $this->populateStub($stub, $model));
5738
}
5839

59-
return $output;
40+
return $this->output;
6041
}
6142

62-
public function types(): array
43+
protected function getPath(BlueprintModel $blueprintModel)
6344
{
64-
return ['factories'];
65-
}
66-
67-
protected function getPath(Model $model)
68-
{
69-
$path = $model->name();
70-
if ($model->namespace()) {
71-
$path = str_replace('\\', '/', $model->namespace()) . '/' . $path;
45+
$path = $blueprintModel->name();
46+
if ($blueprintModel->namespace()) {
47+
$path = str_replace('\\', '/', $blueprintModel->namespace()) . '/' . $path;
7248
}
7349

7450
return 'database/factories/' . $path . 'Factory.php';
@@ -174,7 +150,7 @@ protected function buildDefinition(Model $model)
174150
$definition
175151
);
176152
} elseif (in_array($column->dataType(), ['json', 'jsonb'])) {
177-
$default = $column->defaultValue() ?? "{}";
153+
$default = $column->defaultValue() ?? '{}';
178154
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => '{$default}'," . PHP_EOL;
179155
} elseif ($column->dataType() === 'morphs') {
180156
if ($column->isNullable()) {
@@ -212,27 +188,6 @@ protected function buildDefinition(Model $model)
212188
return trim($definition);
213189
}
214190

215-
protected function buildImports(Model $model)
216-
{
217-
$imports = array_unique($this->imports[$model->name()]);
218-
sort($imports);
219-
220-
return implode(
221-
PHP_EOL,
222-
array_map(
223-
function ($class) {
224-
return 'use ' . $class . ';';
225-
},
226-
$imports
227-
)
228-
);
229-
}
230-
231-
private function addImport(Model $model, $class)
232-
{
233-
$this->imports[$model->name()][] = $class;
234-
}
235-
236191
private function fillableColumns(array $columns): array
237192
{
238193
if (config('blueprint.fake_nullables')) {

0 commit comments

Comments
 (0)