Skip to content

Commit a4e4728

Browse files
committed
Create macro for file stubs
1 parent 9c29b1d commit a4e4728

21 files changed

+159
-170
lines changed

src/BlueprintServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Blueprint;
44

55
use Illuminate\Contracts\Support\DeferrableProvider;
6+
use Illuminate\Support\Facades\File;
67
use Illuminate\Support\ServiceProvider;
78

89
class BlueprintServiceProvider extends ServiceProvider implements DeferrableProvider
@@ -34,6 +35,8 @@ public function register()
3435
__DIR__.'/../config/blueprint.php', 'blueprint'
3536
);
3637

38+
File::mixin(new FileMixins());
39+
3740
$this->app->bind('command.blueprint.build',
3841
function ($app) {
3942
return new BlueprintCommand($app['files']);

src/FileMixins.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace Blueprint;
5+
6+
7+
class FileMixins
8+
{
9+
private $stubs = [];
10+
11+
public function stub()
12+
{
13+
return function ($path) {
14+
if (!isset($this->stubs[$path])) {
15+
$this->stubs[$path] = $this->get(STUBS_PATH . DIRECTORY_SEPARATOR . $path);
16+
}
17+
18+
return $this->stubs[$path];
19+
};
20+
}
21+
}

src/Generators/ControllerGenerator.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function output(array $tree): array
3434
{
3535
$output = [];
3636

37-
$stub = $this->files->get(STUBS_PATH . '/controller/class.stub');
37+
$stub = $this->files->stub('controller/class.stub');
3838

3939
/** @var \Blueprint\Models\Controller $controller */
4040
foreach ($tree['controllers'] as $controller) {
@@ -63,7 +63,7 @@ protected function populateStub(string $stub, Controller $controller)
6363

6464
private function buildMethods(Controller $controller)
6565
{
66-
$template = $this->methodStub();
66+
$template = $this->files->stub('controller/method.stub');
6767

6868
$methods = '';
6969

@@ -118,7 +118,7 @@ private function buildMethods(Controller $controller)
118118
$this->addImport($controller, config('blueprint.namespace') . '\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $this->determineModel($controller, $statement->reference()));
119119
} elseif ($statement instanceof QueryStatement) {
120120
$body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL;
121-
$this->addImport($controller, config('blueprint.namespace') . '\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $this->determineModel($controller, $statement->model()));
121+
$this->addImport($controller, config('blueprint.namespace') . '\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $this->determineModel($controller, $statement->model()));
122122
}
123123

124124
$body .= PHP_EOL;
@@ -141,17 +141,6 @@ protected function getPath(Controller $controller)
141141
return config('blueprint.app_path') . '/' . $path . '.php';
142142
}
143143

144-
private function methodStub()
145-
{
146-
static $stub = '';
147-
148-
if (empty($stub)) {
149-
$stub = $this->files->get(STUBS_PATH . '/controller/method.stub');
150-
}
151-
152-
return $stub;
153-
}
154-
155144
private function addImport(Controller $controller, $class)
156145
{
157146
$this->imports[$controller->name()][] = $class;

src/Generators/FactoryGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function output(array $tree): array
2222
{
2323
$output = [];
2424

25-
$stub = $this->files->get(STUBS_PATH . '/factory.stub');
25+
$stub = $this->files->stub('factory.stub');
2626

2727
/** @var \Blueprint\Models\Model $model */
2828
foreach ($tree['models'] as $model) {

src/Generators/MigrationGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function output(array $tree): array
2323
{
2424
$output = [];
2525

26-
$stub = $this->files->get(STUBS_PATH . '/migration.stub');
26+
$stub = $this->files->stub('migration.stub');
2727

2828
$sequential_timestamp = \Carbon\Carbon::now()->subSeconds(count($tree['models']));
2929

src/Generators/ModelGenerator.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function output(array $tree): array
2222
{
2323
$output = [];
2424

25-
$stub = $this->files->get(STUBS_PATH . '/model/class.stub');
25+
$stub = $this->files->stub('model/class.stub');
2626

2727
/** @var \Blueprint\Models\Model $model */
2828
foreach ($tree['models'] as $model) {
@@ -59,19 +59,19 @@ private function buildProperties(Model $model)
5959

6060
$columns = $this->fillableColumns($model->columns());
6161
if (!empty($columns)) {
62-
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->getStub('fillable'));
62+
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/fillable.stub'));
6363
} else {
64-
$properties .= $this->getStub('fillable');
64+
$properties .= $this->files->stub('model/fillable.stub');
6565
}
6666

6767
$columns = $this->castableColumns($model->columns());
6868
if (!empty($columns)) {
69-
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns), $this->getStub('casts'));
69+
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns), $this->files->stub('model/casts.stub'));
7070
}
7171

7272
$columns = $this->dateColumns($model->columns());
7373
if (!empty($columns)) {
74-
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->getStub('dates'));
74+
$properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/dates.stub'));
7575
}
7676

7777
return trim($properties);
@@ -88,7 +88,7 @@ private function buildRelationships(Model $model)
8888
}
8989

9090
$methods = '';
91-
$template = $this->getStub('method');
91+
$template = $this->files->stub('model/method.stub');
9292

9393
/** @var Column $column */
9494
foreach ($columns as $column) {
@@ -177,17 +177,6 @@ private function pretty_print_array(array $data, $assoc = true)
177177
return trim($output);
178178
}
179179

180-
private function getStub(string $stub)
181-
{
182-
static $stubs = [];
183-
184-
if (empty($stubs[$stub])) {
185-
$stubs[$stub] = $this->files->get(STUBS_PATH . '/model/' . $stub . '.stub');
186-
}
187-
188-
return $stubs[$stub];
189-
}
190-
191180
private function addTraits(Model $model, $stub)
192181
{
193182
if (!$model->usesSoftDeletes()) {

src/Generators/Statements/EventGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function output(array $tree): array
2121
{
2222
$output = [];
2323

24-
$stub = $this->files->get(STUBS_PATH . '/event.stub');
24+
$stub = $this->files->stub('event.stub');
2525

2626
/** @var \Blueprint\Models\Controller $controller */
2727
foreach ($tree['controllers'] as $controller) {
@@ -76,7 +76,7 @@ private function buildConstructor(FireStatement $fireStatement)
7676
static $constructor = null;
7777

7878
if (is_null($constructor)) {
79-
$constructor = str_replace('new instance', 'new event instance', $this->files->get(STUBS_PATH . '/partials/constructor.stub'));
79+
$constructor = str_replace('new instance', 'new event instance', $this->files->stub('partials/constructor.stub'));
8080
}
8181

8282
if (empty($fireStatement->data())) {

src/Generators/Statements/FormRequestGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function output(array $tree): array
2828
{
2929
$output = [];
3030

31-
$stub = $this->files->get(STUBS_PATH . '/form-request.stub');
31+
$stub = $this->files->stub('form-request.stub');
3232

3333
$this->registerModels($tree['models'] ?? []);
3434

src/Generators/Statements/JobGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function output(array $tree): array
2121
{
2222
$output = [];
2323

24-
$stub = $this->files->get(STUBS_PATH . '/job.stub');
24+
$stub = $this->files->stub('job.stub');
2525

2626
/** @var \Blueprint\Models\Controller $controller */
2727
foreach ($tree['controllers'] as $controller) {
@@ -72,7 +72,7 @@ private function buildConstructor(DispatchStatement $dispatchStatement)
7272
static $constructor = null;
7373

7474
if (is_null($constructor)) {
75-
$constructor = str_replace('new instance', 'new job instance', $this->files->get(STUBS_PATH . '/partials/constructor.stub'));
75+
$constructor = str_replace('new instance', 'new job instance', $this->files->stub('partials/constructor.stub'));
7676
}
7777

7878
if (empty($dispatchStatement->data())) {

src/Generators/Statements/MailGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function output(array $tree): array
2121
{
2222
$output = [];
2323

24-
$stub = $this->files->get(STUBS_PATH . '/mail.stub');
24+
$stub = $this->files->stub('mail.stub');
2525

2626
/** @var \Blueprint\Models\Controller $controller */
2727
foreach ($tree['controllers'] as $controller) {
@@ -72,7 +72,7 @@ private function buildConstructor(SendStatement $sendStatement)
7272
static $constructor = null;
7373

7474
if (is_null($constructor)) {
75-
$constructor = str_replace('new instance', 'new message instance', $this->files->get(STUBS_PATH . '/partials/constructor.stub'));
75+
$constructor = str_replace('new instance', 'new message instance', $this->files->stub('partials/constructor.stub'));
7676
}
7777

7878
if (empty($sendStatement->data())) {

0 commit comments

Comments
 (0)