Skip to content

Commit b826722

Browse files
authored
Make generator classes extendable (#297)
1 parent 66de68d commit b826722

10 files changed

+57
-57
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected function populateStub(string $stub, Controller $controller)
8080
return $stub;
8181
}
8282

83-
private function buildMethods(Controller $controller)
83+
protected function buildMethods(Controller $controller)
8484
{
8585
$template = $this->files->stub('controller/method.stub');
8686

@@ -183,12 +183,7 @@ protected function getPath(Controller $controller)
183183
return Blueprint::appPath().'/'.$path.'.php';
184184
}
185185

186-
private function addImport(Controller $controller, $class)
187-
{
188-
$this->imports[$controller->name()][] = $class;
189-
}
190-
191-
private function buildImports(Controller $controller)
186+
protected function buildImports(Controller $controller)
192187
{
193188
$imports = array_unique($this->imports[$controller->name()]);
194189
sort($imports);
@@ -198,6 +193,11 @@ private function buildImports(Controller $controller)
198193
}, $imports));
199194
}
200195

196+
private function addImport(Controller $controller, $class)
197+
{
198+
$this->imports[$controller->name()][] = $class;
199+
}
200+
201201
private function determineModel(Controller $controller, ?string $reference)
202202
{
203203
if (empty($reference) || $reference === 'id') {

src/Generators/FactoryGenerator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,7 @@ protected function buildDefinition(Model $model)
185185
return trim($definition);
186186
}
187187

188-
private function addImport(Model $model, $class)
189-
{
190-
$this->imports[$model->name()][] = $class;
191-
}
192-
193-
private function buildImports(Model $model)
188+
protected function buildImports(Model $model)
194189
{
195190
$imports = array_unique($this->imports[$model->name()]);
196191
sort($imports);
@@ -200,6 +195,11 @@ private function buildImports(Model $model)
200195
}, $imports));
201196
}
202197

198+
private function addImport(Model $model, $class)
199+
{
200+
$this->imports[$model->name()][] = $class;
201+
}
202+
203203
private function fillableColumns(array $columns): array
204204
{
205205
if (config('blueprint.fake_nullables')) {

src/Generators/ModelGenerator.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function populateStub(string $stub, Model $model)
6161
return $stub;
6262
}
6363

64-
private function buildClassPhpDoc(Model $model)
64+
protected function buildClassPhpDoc(Model $model)
6565
{
6666
if (! config('blueprint.generate_phpdocs')) {
6767
return '';
@@ -93,7 +93,7 @@ private function buildClassPhpDoc(Model $model)
9393
return $phpDoc;
9494
}
9595

96-
private function buildProperties(Model $model)
96+
protected function buildProperties(Model $model)
9797
{
9898
$properties = '';
9999

@@ -130,7 +130,7 @@ private function buildProperties(Model $model)
130130
return trim($properties);
131131
}
132132

133-
private function buildRelationships(Model $model)
133+
protected function buildRelationships(Model $model)
134134
{
135135
$methods = '';
136136
$template = $this->files->stub('model/method.stub');
@@ -205,6 +205,18 @@ protected function getPath(Model $model)
205205
return Blueprint::appPath().'/'.$path.'.php';
206206
}
207207

208+
protected function addTraits(Model $model, $stub)
209+
{
210+
if (!$model->usesSoftDeletes()) {
211+
return $stub;
212+
}
213+
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);
216+
217+
return $stub;
218+
}
219+
208220
private function fillableColumns(array $columns)
209221
{
210222
return array_diff(array_keys($columns), [
@@ -283,18 +295,6 @@ private function pretty_print_array(array $data, $assoc = true)
283295
return trim(str_replace("\n", PHP_EOL, $output));
284296
}
285297

286-
private function addTraits(Model $model, $stub)
287-
{
288-
if (! $model->usesSoftDeletes()) {
289-
return $stub;
290-
}
291-
292-
$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;'.PHP_EOL.'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub);
293-
$stub = Str::replaceFirst('{', '{'.PHP_EOL.' use SoftDeletes;'.PHP_EOL, $stub);
294-
295-
return $stub;
296-
}
297-
298298
private function phpDataType(string $dataType)
299299
{
300300
static $php_data_types = [

src/Generators/SeederGenerator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public function types(): array
4545
return ['seeders'];
4646
}
4747

48-
private function getPath($model)
49-
{
50-
return 'database/seeds/'.$model.'Seeder.php';
51-
}
52-
5348
protected function populateStub(string $stub, string $model)
5449
{
5550
$stub = str_replace('DummyClass', $this->getClassName($model), $stub);
@@ -58,16 +53,21 @@ protected function populateStub(string $stub, string $model)
5853
return $stub;
5954
}
6055

61-
private function getClassName(string $model)
56+
protected function getClassName(string $model)
6257
{
6358
return $model.'Seeder';
6459
}
6560

66-
private function build(string $model)
61+
protected function build(string $model)
6762
{
6863
return sprintf('factory(\\%s::class, 5)->create();', $this->fqcnForContext($model));
6964
}
7065

66+
private function getPath($model)
67+
{
68+
return 'database/seeds/' . $model . 'Seeder.php';
69+
}
70+
7171
private function registerModels(array $tree)
7272
{
7373
$this->models = array_merge($tree['cache'] ?? [], $tree['models'] ?? []);

src/Generators/Statements/EventGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function populateStub(string $stub, FireStatement $fireStatement)
7575
return $stub;
7676
}
7777

78-
private function buildConstructor(FireStatement $fireStatement)
78+
protected function buildConstructor(FireStatement $fireStatement)
7979
{
8080
static $constructor = null;
8181

src/Generators/Statements/FormRequestGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function populateStub(string $stub, string $name, $context, ValidateSt
8282
return $stub;
8383
}
8484

85-
private function buildRules(string $context, ValidateStatement $validateStatement)
85+
protected function buildRules(string $context, ValidateStatement $validateStatement)
8686
{
8787
return trim(array_reduce($validateStatement->data(), function ($output, $field) use ($context) {
8888
[$qualifier, $column] = $this->splitField($field);

src/Generators/Statements/JobGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function populateStub(string $stub, DispatchStatement $dispatchStateme
7171
return $stub;
7272
}
7373

74-
private function buildConstructor(DispatchStatement $dispatchStatement)
74+
protected function buildConstructor(DispatchStatement $dispatchStatement)
7575
{
7676
static $constructor = null;
7777

src/Generators/Statements/MailGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function populateStub(string $stub, SendStatement $sendStatement)
7575
return $stub;
7676
}
7777

78-
private function buildConstructor(SendStatement $sendStatement)
78+
protected function buildConstructor(SendStatement $sendStatement)
7979
{
8080
static $constructor = null;
8181

src/Generators/Statements/ResourceGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function populateStub(string $stub, ResourceStatement $resource)
8383
return $stub;
8484
}
8585

86-
private function buildData(ResourceStatement $resource)
86+
protected function buildData(ResourceStatement $resource)
8787
{
8888
$context = Str::singular($resource->reference());
8989

src/Generators/TestGenerator.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,23 @@ protected function addTrait(Controller $controller, $trait)
489489
$this->traits[$controller->name()][] = $trait;
490490
}
491491

492+
protected function addImport(Controller $controller, $class)
493+
{
494+
$this->imports[$controller->name()][] = $class;
495+
}
496+
497+
protected function buildImports(Controller $controller)
498+
{
499+
$this->addImport($controller, 'Tests\\TestCase');
500+
501+
$imports = array_unique($this->imports[$controller->name()]);
502+
sort($imports);
503+
504+
return implode(PHP_EOL, array_map(function ($class) {
505+
return 'use ' . $class . ';';
506+
}, $imports));
507+
}
508+
492509
private function buildTraits(Controller $controller)
493510
{
494511
if (empty($this->traits[$controller->name()])) {
@@ -510,23 +527,6 @@ private function testCaseStub()
510527
return $this->stubs['test-case'];
511528
}
512529

513-
protected function addImport(Controller $controller, $class)
514-
{
515-
$this->imports[$controller->name()][] = $class;
516-
}
517-
518-
protected function buildImports(Controller $controller)
519-
{
520-
$this->addImport($controller, 'Tests\\TestCase');
521-
522-
$imports = array_unique($this->imports[$controller->name()]);
523-
sort($imports);
524-
525-
return implode(PHP_EOL, array_map(function ($class) {
526-
return 'use '.$class.';';
527-
}, $imports));
528-
}
529-
530530
private function determineModel(string $prefix, ?string $reference)
531531
{
532532
if (empty($reference) || $reference === 'id') {

0 commit comments

Comments
 (0)