Skip to content

Commit afa8b80

Browse files
authored
Introduce Tree class to manage the analyzed tokens (#304)
1 parent 0ea6154 commit afa8b80

33 files changed

+208
-201
lines changed

src/Blueprint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public function analyze(array $tokens)
6767
$registry = array_merge($registry, $lexer->analyze($tokens));
6868
}
6969

70-
return $registry;
70+
return new Tree($registry);
7171
}
7272

73-
public function generate(array $tree, array $only = [], array $skip = []): array
73+
public function generate(Tree $tree, array $only = [], array $skip = []): array
7474
{
7575
$components = [];
7676

src/Contracts/Generator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace Blueprint\Contracts;
44

5+
use Blueprint\Tree;
6+
57
interface Generator
68
{
79
/**
810
* @param \Illuminate\Contracts\Filesystem\Filesystem
911
*/
1012
public function __construct($files);
1113

12-
public function output(array $tree): array;
14+
public function output(Tree $tree): array;
1315

1416
public function types(): array;
1517
}

src/Generators/ControllerGenerator.php

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Blueprint\Blueprint;
66
use Blueprint\Contracts\Generator;
77
use Blueprint\Models\Controller;
8-
use Blueprint\Models\Model;
98
use Blueprint\Models\Statements\DispatchStatement;
109
use Blueprint\Models\Statements\EloquentStatement;
1110
use Blueprint\Models\Statements\FireStatement;
@@ -17,6 +16,7 @@
1716
use Blueprint\Models\Statements\SendStatement;
1817
use Blueprint\Models\Statements\SessionStatement;
1918
use Blueprint\Models\Statements\ValidateStatement;
19+
use Blueprint\Tree;
2020
use Illuminate\Support\Str;
2121

2222
class ControllerGenerator implements Generator
@@ -28,23 +28,24 @@ class ControllerGenerator implements Generator
2828

2929
private $imports = [];
3030

31-
private $models = [];
31+
/** @var Tree */
32+
private $tree;
3233

3334
public function __construct($files)
3435
{
3536
$this->files = $files;
3637
}
3738

38-
public function output(array $tree): array
39+
public function output(Tree $tree): array
3940
{
41+
$this->tree = $tree;
42+
4043
$output = [];
4144

4245
$stub = $this->files->stub('controller/class.stub');
4346

44-
$this->registerModels($tree);
45-
4647
/** @var \Blueprint\Models\Controller $controller */
47-
foreach ($tree['controllers'] as $controller) {
48+
foreach ($tree->controllers() as $controller) {
4849
$this->addImport($controller, 'Illuminate\\Http\\Request');
4950

5051
if ($controller->fullyQualifiedNamespace() !== 'App\\Http\\Controllers') {
@@ -53,7 +54,7 @@ public function output(array $tree): array
5354

5455
$path = $this->getPath($controller);
5556

56-
if (! $this->files->exists(dirname($path))) {
57+
if (!$this->files->exists(dirname($path))) {
5758
$this->files->makeDirectory(dirname($path), 0755, true);
5859
}
5960

@@ -131,7 +132,7 @@ protected function buildMethods(Controller $controller)
131132
$this->addImport($controller, config('blueprint.namespace').'\\Jobs\\'.$statement->job());
132133
} elseif ($statement instanceof FireStatement) {
133134
$body .= self::INDENT.$statement->output().PHP_EOL;
134-
if (! $statement->isNamedEvent()) {
135+
if (!$statement->isNamedEvent()) {
135136
$this->addImport($controller, config('blueprint.namespace').'\\Events\\'.$statement->event());
136137
}
137138
} elseif ($statement instanceof RenderStatement) {
@@ -142,7 +143,7 @@ protected function buildMethods(Controller $controller)
142143
$method = str_replace('* @return \\Illuminate\\Http\\Response', '* @return \\'.$fqcn, $method);
143144

144145
$import = $fqcn;
145-
if (! $statement->collection()) {
146+
if (!$statement->collection()) {
146147
$import .= ' as '.$statement->name().'Resource';
147148
}
148149

@@ -166,7 +167,7 @@ protected function buildMethods(Controller $controller)
166167
$body .= PHP_EOL;
167168
}
168169

169-
if (! empty($body)) {
170+
if (!empty($body)) {
170171
$method = str_replace('//', trim($body), $method);
171172
}
172173

@@ -218,32 +219,12 @@ private function fullyQualifyModelReference(string $sub_namespace, string $model
218219
// Use respond-statement.php as test case.
219220

220221
/** @var \Blueprint\Models\Model $model */
221-
$model = $this->modelForContext($model_name);
222+
$model = $this->tree->modelForContext($model_name);
222223

223224
if (isset($model)) {
224225
return $model->fullyQualifiedClassName();
225226
}
226227

227228
return config('blueprint.namespace').'\\'.($sub_namespace ? $sub_namespace.'\\' : '').$model_name;
228229
}
229-
230-
private function modelForContext(string $context)
231-
{
232-
if (isset($this->models[Str::studly($context)])) {
233-
return $this->models[Str::studly($context)];
234-
}
235-
236-
$matches = array_filter(array_keys($this->models), function ($key) use ($context) {
237-
return Str::endsWith($key, '/'.Str::studly($context));
238-
});
239-
240-
if (count($matches) === 1) {
241-
return $this->models[$matches[0]];
242-
}
243-
}
244-
245-
private function registerModels(array $tree)
246-
{
247-
$this->models = array_merge($tree['cache'] ?? [], $tree['models'] ?? []);
248-
}
249230
}

src/Generators/FactoryGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Blueprint\Contracts\Generator;
66
use Blueprint\Models\Column;
77
use Blueprint\Models\Model;
8+
use Blueprint\Tree;
89
use Illuminate\Support\Str;
910

1011
class FactoryGenerator implements Generator
@@ -21,14 +22,14 @@ public function __construct($files)
2122
$this->files = $files;
2223
}
2324

24-
public function output(array $tree): array
25+
public function output(Tree $tree): array
2526
{
2627
$output = [];
2728

2829
$stub = $this->files->stub('factory.stub');
2930

3031
/** @var \Blueprint\Models\Model $model */
31-
foreach ($tree['models'] as $model) {
32+
foreach ($tree->models() as $model) {
3233
$this->addImport($model, 'Faker\Generator as Faker');
3334
$this->addImport($model, $model->fullyQualifiedClassName());
3435

src/Generators/MigrationGenerator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Blueprint\Contracts\Generator;
66
use Blueprint\Models\Model;
7+
use Blueprint\Tree;
78
use Carbon\Carbon;
89
use Illuminate\Support\Facades\App;
910
use Illuminate\Support\Str;
@@ -41,18 +42,18 @@ public function __construct($files)
4142
$this->files = $files;
4243
}
4344

44-
public function output(array $tree): array
45+
public function output(Tree $tree): array
4546
{
4647
$output = [];
4748

4849
$created_pivot_tables = [];
4950

5051
$stub = $this->files->stub('migration.stub');
5152

52-
$sequential_timestamp = \Carbon\Carbon::now()->subSeconds(count($tree['models']));
53+
$sequential_timestamp = \Carbon\Carbon::now()->subSeconds(count($tree->models()));
5354

5455
/** @var \Blueprint\Models\Model $model */
55-
foreach ($tree['models'] as $model) {
56+
foreach ($tree->models() as $model) {
5657
$path = $this->getPath($model, $sequential_timestamp->addSecond());
5758
$this->files->put($path, $this->populateStub($stub, $model));
5859

src/Generators/ModelGenerator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Blueprint\Contracts\Generator;
77
use Blueprint\Models\Column;
88
use Blueprint\Models\Model;
9+
use Blueprint\Tree;
910
use Illuminate\Support\Str;
1011

1112
class ModelGenerator implements Generator
@@ -18,14 +19,14 @@ public function __construct($files)
1819
$this->files = $files;
1920
}
2021

21-
public function output(array $tree): array
22+
public function output(Tree $tree): array
2223
{
2324
$output = [];
2425

2526
$stub = $this->files->stub('model/class.stub');
2627

2728
/** @var \Blueprint\Models\Model $model */
28-
foreach ($tree['models'] as $model) {
29+
foreach ($tree->models() as $model) {
2930
$path = $this->getPath($model);
3031

3132
if (! $this->files->exists(dirname($path))) {
@@ -211,8 +212,8 @@ protected function addTraits(Model $model, $stub)
211212
return $stub;
212213
}
213214

214-
$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;' . PHP_EOL . 'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub);
215-
$stub = Str::replaceFirst('{', '{' . PHP_EOL . ' use SoftDeletes;' . PHP_EOL, $stub);
215+
$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;'.PHP_EOL.'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub);
216+
$stub = Str::replaceFirst('{', '{'.PHP_EOL.' use SoftDeletes;'.PHP_EOL, $stub);
216217

217218
return $stub;
218219
}

src/Generators/RouteGenerator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Blueprint\Contracts\Generator;
66
use Blueprint\Models\Controller;
7+
use Blueprint\Tree;
78
use Illuminate\Support\Str;
89

910
class RouteGenerator implements Generator
@@ -16,16 +17,16 @@ public function __construct($files)
1617
$this->files = $files;
1718
}
1819

19-
public function output(array $tree): array
20+
public function output(Tree $tree): array
2021
{
21-
if (empty($tree['controllers'])) {
22+
if (empty($tree->controllers())) {
2223
return [];
2324
}
2425

2526
$routes = ['api' => '', 'web' => ''];
2627

2728
/** @var \Blueprint\Models\Controller $controller */
28-
foreach ($tree['controllers'] as $controller) {
29+
foreach ($tree->controllers() as $controller) {
2930
$type = $controller->isApiResource() ? 'api' : 'web';
3031
$routes[$type] .= PHP_EOL.PHP_EOL.$this->buildRoutes($controller);
3132
}

src/Generators/SeederGenerator.php

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

55
use Blueprint\Contracts\Generator;
6-
use Blueprint\Models\Model;
7-
use Illuminate\Support\Str;
6+
use Blueprint\Tree;
87

98
class SeederGenerator implements Generator
109
{
1110
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
1211
private $files;
1312

14-
private $models = [];
13+
/** @var Tree */
14+
private $tree;
1515

1616
public function __construct($files)
1717
{
1818
$this->files = $files;
1919
}
2020

21-
public function output(array $tree): array
21+
public function output(Tree $tree): array
2222
{
23-
if (empty($tree['seeders'])) {
23+
$this->tree = $tree;
24+
25+
if (empty($tree->seeders())) {
2426
return [];
2527
}
2628

27-
$output = [];
29+
$output = [];
2830

2931
$stub = $this->files->stub('seeder.stub');
3032

31-
$this->registerModels($tree);
32-
33-
foreach ($tree['seeders'] as $model) {
33+
foreach ($tree->seeders() as $model) {
3434
$path = $this->getPath($model);
3535
$this->files->put($path, $this->populateStub($stub, $model));
3636

@@ -60,38 +60,11 @@ protected function getClassName(string $model)
6060

6161
protected function build(string $model)
6262
{
63-
return sprintf('factory(\\%s::class, 5)->create();', $this->fqcnForContext($model));
63+
return sprintf('factory(\\%s::class, 5)->create();', $this->tree->fqcnForContext($model));
6464
}
6565

6666
private function getPath($model)
6767
{
68-
return 'database/seeds/' . $model . 'Seeder.php';
69-
}
70-
71-
private function registerModels(array $tree)
72-
{
73-
$this->models = array_merge($tree['cache'] ?? [], $tree['models'] ?? []);
74-
}
75-
76-
private function fqcnForContext(string $context)
77-
{
78-
if (isset($this->models[$context])) {
79-
return $this->models[$context]->fullyQualifiedClassName();
80-
}
81-
82-
$matches = array_filter(array_keys($this->models), function ($key) use ($context) {
83-
return Str::endsWith($key, '\\'.Str::studly($context));
84-
});
85-
86-
if (count($matches) === 1) {
87-
return $this->models[current($matches)]->fullyQualifiedClassName();
88-
}
89-
90-
$fqn = config('blueprint.namespace');
91-
if (config('blueprint.models_namespace')) {
92-
$fqn .= '\\'.config('blueprint.models_namespace');
93-
}
94-
95-
return $fqn.'\\'.$context;
68+
return 'database/seeds/'.$model.'Seeder.php';
9669
}
9770
}

src/Generators/Statements/EventGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
use Blueprint\Blueprint;
66
use Blueprint\Generators\StatementGenerator;
77
use Blueprint\Models\Statements\FireStatement;
8+
use Blueprint\Tree;
89

910
class EventGenerator extends StatementGenerator
1011
{
1112
protected $new_instance = 'new event instance';
1213

13-
public function output(array $tree): array
14+
public function output(Tree $tree): array
1415
{
1516
$output = [];
1617

1718
$stub = $this->files->stub('event.stub');
1819

1920
/** @var \Blueprint\Models\Controller $controller */
20-
foreach ($tree['controllers'] as $controller) {
21+
foreach ($tree->controllers() as $controller) {
2122
foreach ($controller->methods() as $method => $statements) {
2223
foreach ($statements as $statement) {
2324
if (! $statement instanceof FireStatement) {

0 commit comments

Comments
 (0)