Skip to content

Commit 78d0cc8

Browse files
authored
Add blueprint:init console command (#438)
1 parent d4d54cc commit 78d0cc8

40 files changed

+303
-214
lines changed

config/blueprint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
'on_delete' => 'cascade',
7676
'on_update' => 'cascade',
77-
77+
7878

7979
/*
8080
|--------------------------------------------------------------------------

src/Blueprint.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Blueprint
1515

1616
public static function relativeNamespace(string $fullyQualifiedClassName)
1717
{
18-
$namespace = config('blueprint.namespace').'\\';
18+
$namespace = config('blueprint.namespace') . '\\';
1919
$reference = ltrim($fullyQualifiedClassName, '\\');
2020

2121
if (Str::startsWith($reference, $namespace)) {
@@ -54,19 +54,19 @@ public function parse($content, $strip_dashes = true)
5454
}
5555

5656
$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi', function ($matches) {
57-
return $matches[1].strtolower($matches[2]).': '.$matches[2];
57+
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
5858
}, $content);
5959

6060
$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?): true$/mi', function ($matches) {
61-
return $matches[1].strtolower($matches[2]).': '.$matches[2];
61+
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
6262
}, $content);
6363

6464
$content = preg_replace_callback('/^(\s+)resource?$/mi', function ($matches) {
65-
return $matches[1].'resource: web';
65+
return $matches[1] . 'resource: web';
6666
}, $content);
6767

6868
$content = preg_replace_callback('/^(\s+)uuid(: true)?$/mi', function ($matches) {
69-
return $matches[1].'id: uuid primary';
69+
return $matches[1] . 'id: uuid primary';
7070
}, $content);
7171

7272
return Yaml::parse($content);

src/BlueprintServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Blueprint\Commands\BuildCommand;
88
use Blueprint\Commands\EraseCommand;
99
use Blueprint\Commands\NewCommand;
10+
use Blueprint\Commands\InitCommand;
1011
use Blueprint\Commands\TraceCommand;
1112
use Blueprint\Contracts\Generator;
1213
use Blueprint\FileMixins;
@@ -67,6 +68,9 @@ public function register()
6768
$this->app->bind('command.blueprint.new', function ($app) {
6869
return new NewCommand($app['files']);
6970
});
71+
$this->app->bind('command.blueprint.init', function ($app) {
72+
return new InitCommand();
73+
});
7074

7175
$this->app->singleton(Blueprint::class, function ($app) {
7276
$blueprint = new Blueprint();
@@ -86,6 +90,7 @@ public function register()
8690
'command.blueprint.erase',
8791
'command.blueprint.trace',
8892
'command.blueprint.new',
93+
'command.blueprint.init',
8994
]);
9095
}
9196

@@ -101,6 +106,7 @@ public function provides()
101106
'command.blueprint.erase',
102107
'command.blueprint.trace',
103108
'command.blueprint.new',
109+
'command.blueprint.init',
104110
Blueprint::class,
105111
];
106112
}

src/Commands/BuildCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function handle()
5353
$file = $this->argument('draft') ?? $this->defaultDraftFile();
5454

5555
if (!$this->files->exists($file)) {
56-
$this->error('Draft file could not be found: '.($file ?: 'draft.yaml'));
56+
$this->error('Draft file could not be found: ' . ($file ?: 'draft.yaml'));
5757
return 1;
5858
}
5959

@@ -65,9 +65,9 @@ public function handle()
6565
$generated = $this->builder->execute($blueprint, $this->files, $file, $only, $skip, $overwriteMigrations);
6666

6767
collect($generated)->each(function ($files, $action) {
68-
$this->line(Str::studly($action).':', $this->outputStyle($action));
68+
$this->line(Str::studly($action) . ':', $this->outputStyle($action));
6969
collect($files)->each(function ($file) {
70-
$this->line('- '.$file);
70+
$this->line('- ' . $file);
7171
});
7272

7373
$this->line('');

src/Commands/EraseCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function handle()
6161
}
6262

6363
collect($files)->each(function ($file) {
64-
$this->line('- '.$file);
64+
$this->line('- ' . $file);
6565
});
6666

6767
$this->line('');

src/Commands/InitCommand.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Blueprint\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class InitCommand extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'blueprint:init';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'An alias for "blueprint:new" command';
22+
23+
/**
24+
* Execute the console command.
25+
*
26+
* @return mixed
27+
*/
28+
public function handle()
29+
{
30+
$this->call(\Blueprint\Commands\NewCommand::class);
31+
}
32+
}

src/Generators/ControllerGenerator.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ protected function buildMethods(Controller $controller)
9393
if (in_array($name, ['edit', 'update', 'show', 'destroy'])) {
9494
$context = Str::singular($controller->prefix());
9595
$reference = $this->fullyQualifyModelReference($controller->namespace(), Str::camel($context));
96-
$variable = '$'.Str::camel($context);
96+
$variable = '$' . Str::camel($context);
9797

9898
// TODO: verify controller prefix references a model
9999
$search = ' * @return \\Illuminate\\Http\\Response';
100-
$method = str_replace($search, ' * @param \\'.$reference.' '.$variable.PHP_EOL.$search, $method);
100+
$method = str_replace($search, ' * @param \\' . $reference . ' ' . $variable . PHP_EOL . $search, $method);
101101

102102
$search = '(Request $request';
103-
$method = str_replace($search, $search.', '.$context.' '.$variable, $method);
103+
$method = str_replace($search, $search . ', ' . $context . ' ' . $variable, $method);
104104
$this->addImport($controller, $reference);
105105
}
106106

@@ -109,50 +109,50 @@ protected function buildMethods(Controller $controller)
109109

110110
foreach ($statements as $statement) {
111111
if ($statement instanceof SendStatement) {
112-
$body .= self::INDENT.$statement->output().PHP_EOL;
112+
$body .= self::INDENT . $statement->output() . PHP_EOL;
113113
if ($statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_FACADE) {
114114
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Notification');
115-
$this->addImport($controller, config('blueprint.namespace').'\\Notification\\'.$statement->mail());
115+
$this->addImport($controller, config('blueprint.namespace') . '\\Notification\\' . $statement->mail());
116116
} elseif ($statement->type() === SendStatement::TYPE_MAIL) {
117117
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Mail');
118-
$this->addImport($controller, config('blueprint.namespace').'\\Mail\\'.$statement->mail());
118+
$this->addImport($controller, config('blueprint.namespace') . '\\Mail\\' . $statement->mail());
119119
}
120120
} elseif ($statement instanceof ValidateStatement) {
121121
$using_validation = true;
122-
$class_name = $controller->name().Str::studly($name).'Request';
122+
$class_name = $controller->name() . Str::studly($name) . 'Request';
123123

124-
$fqcn = config('blueprint.namespace').'\\Http\\Requests\\'.($controller->namespace() ? $controller->namespace().'\\' : '').$class_name;
124+
$fqcn = config('blueprint.namespace') . '\\Http\\Requests\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $class_name;
125125

126-
$method = str_replace('\Illuminate\Http\Request $request', '\\'.$fqcn.' $request', $method);
127-
$method = str_replace('(Request $request', '('.$class_name.' $request', $method);
126+
$method = str_replace('\Illuminate\Http\Request $request', '\\' . $fqcn . ' $request', $method);
127+
$method = str_replace('(Request $request', '(' . $class_name . ' $request', $method);
128128

129129
$this->addImport($controller, $fqcn);
130130
} elseif ($statement instanceof DispatchStatement) {
131-
$body .= self::INDENT.$statement->output().PHP_EOL;
132-
$this->addImport($controller, config('blueprint.namespace').'\\Jobs\\'.$statement->job());
131+
$body .= self::INDENT . $statement->output() . PHP_EOL;
132+
$this->addImport($controller, config('blueprint.namespace') . '\\Jobs\\' . $statement->job());
133133
} elseif ($statement instanceof FireStatement) {
134-
$body .= self::INDENT.$statement->output().PHP_EOL;
134+
$body .= self::INDENT . $statement->output() . PHP_EOL;
135135
if (!$statement->isNamedEvent()) {
136-
$this->addImport($controller, config('blueprint.namespace').'\\Events\\'.$statement->event());
136+
$this->addImport($controller, config('blueprint.namespace') . '\\Events\\' . $statement->event());
137137
}
138138
} elseif ($statement instanceof RenderStatement) {
139-
$body .= self::INDENT.$statement->output().PHP_EOL;
139+
$body .= self::INDENT . $statement->output() . PHP_EOL;
140140
} elseif ($statement instanceof ResourceStatement) {
141-
$fqcn = config('blueprint.namespace').'\\Http\\Resources\\'.($controller->namespace() ? $controller->namespace().'\\' : '').$statement->name();
142-
$method = str_replace('* @return \\Illuminate\\Http\\Response', '* @return \\'.$fqcn, $method);
141+
$fqcn = config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name();
142+
$method = str_replace('* @return \\Illuminate\\Http\\Response', '* @return \\' . $fqcn, $method);
143143
$this->addImport($controller, $fqcn);
144-
$body .= self::INDENT.$statement->output().PHP_EOL;
144+
$body .= self::INDENT . $statement->output() . PHP_EOL;
145145
} elseif ($statement instanceof RedirectStatement) {
146-
$body .= self::INDENT.$statement->output().PHP_EOL;
146+
$body .= self::INDENT . $statement->output() . PHP_EOL;
147147
} elseif ($statement instanceof RespondStatement) {
148-
$body .= self::INDENT.$statement->output().PHP_EOL;
148+
$body .= self::INDENT . $statement->output() . PHP_EOL;
149149
} elseif ($statement instanceof SessionStatement) {
150-
$body .= self::INDENT.$statement->output().PHP_EOL;
150+
$body .= self::INDENT . $statement->output() . PHP_EOL;
151151
} elseif ($statement instanceof EloquentStatement) {
152-
$body .= self::INDENT.$statement->output($controller->prefix(), $name, $using_validation).PHP_EOL;
152+
$body .= self::INDENT . $statement->output($controller->prefix(), $name, $using_validation) . PHP_EOL;
153153
$this->addImport($controller, $this->determineModel($controller, $statement->reference()));
154154
} elseif ($statement instanceof QueryStatement) {
155-
$body .= self::INDENT.$statement->output($controller->prefix()).PHP_EOL;
155+
$body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL;
156156
$this->addImport($controller, $this->determineModel($controller, $statement->model()));
157157
}
158158

@@ -164,10 +164,10 @@ protected function buildMethods(Controller $controller)
164164
}
165165

166166
if (Blueprint::supportsReturnTypeHits()) {
167-
$method = str_replace(')'.PHP_EOL, '): \Illuminate\Http\Response'.PHP_EOL, $method);
167+
$method = str_replace(')' . PHP_EOL, '): \Illuminate\Http\Response' . PHP_EOL, $method);
168168
}
169169

170-
$methods .= PHP_EOL.$method;
170+
$methods .= PHP_EOL . $method;
171171
}
172172

173173
return trim($methods);
@@ -177,7 +177,7 @@ protected function getPath(Controller $controller)
177177
{
178178
$path = str_replace('\\', '/', Blueprint::relativeNamespace($controller->fullyQualifiedClassName()));
179179

180-
return Blueprint::appPath().'/'.$path.'.php';
180+
return Blueprint::appPath() . '/' . $path . '.php';
181181
}
182182

183183
protected function buildImports(Controller $controller)
@@ -186,7 +186,7 @@ protected function buildImports(Controller $controller)
186186
sort($imports);
187187

188188
return implode(PHP_EOL, array_map(function ($class) {
189-
return 'use '.$class.';';
189+
return 'use ' . $class . ';';
190190
}, $imports));
191191
}
192192

@@ -221,6 +221,6 @@ private function fullyQualifyModelReference(string $sub_namespace, string $model
221221
return $model->fullyQualifiedClassName();
222222
}
223223

224-
return config('blueprint.namespace').'\\'.($sub_namespace ? $sub_namespace.'\\' : '').$model_name;
224+
return config('blueprint.namespace') . '\\' . ($sub_namespace ? $sub_namespace . '\\' : '') . $model_name;
225225
}
226226
}

0 commit comments

Comments
 (0)