Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"mockery/mockery": "^1.2"
"mockery/mockery": "^1.2",
"orchestra/testbench": "^4.0"
},
"autoload": {
"psr-4": {
Expand Down
2,146 changes: 1,962 additions & 184 deletions composer.lock

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions config/blueprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Application Namespace
|--------------------------------------------------------------------------
|
| Blueprint assumes a default Laravel application namespace of 'App'.
| However, you may configure Blueprint to use a custom namespace.
| Ultimately this should match the PSR-4 autoload value set
| within the composer.json file of your application.
|
*/
'namespace' => 'App',


/*
|--------------------------------------------------------------------------
| Component Namespaces
|--------------------------------------------------------------------------
|
| Blueprint promotes following Laravel conventions. As such, it generates
| components under the default namespaces. For example, models are under
| the `App` namespace. However, you may configure Blueprint to use
| a custom namespace when generating these components.
|
*/
'models_namespace' => '',
'controllers_namespace' => 'Http\\Controllers',


/*
|--------------------------------------------------------------------------
| Application Path
|--------------------------------------------------------------------------
|
| Here you may customize the path where Blueprint stores generated
| components. By default, Blueprint will store files under the
| `app` folder However, you may change the path to store
| generated component elsewhere.
|
*/
'app_path' => app_path(),

];
5 changes: 5 additions & 0 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class Blueprint
private $lexers = [];
private $generators = [];

public static function relativeNamespace(string $fullyQualifiedClassName)
{
return ltrim(str_replace(config('blueprint.namespace'), '', $fullyQualifiedClassName), '\\');
}

public function parse($content)
{
$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi', function ($matches) {
Expand Down
11 changes: 11 additions & 0 deletions src/BlueprintServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Blueprint;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;

class BlueprintServiceProvider extends ServiceProvider implements DeferrableProvider
Expand All @@ -17,6 +18,10 @@ public function boot()
if (!defined('STUBS_PATH')) {
define('STUBS_PATH', dirname(__DIR__) . '/stubs');
}

$this->publishes([
__DIR__.'/../config/blueprint.php' => config_path('blueprint.php'),
]);
}

/**
Expand All @@ -26,6 +31,12 @@ public function boot()
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../config/blueprint.php', 'blueprint'
);

File::mixin(new FileMixins());

$this->app->bind('command.blueprint.build',
function ($app) {
return new BlueprintCommand($app['files']);
Expand Down
21 changes: 21 additions & 0 deletions src/FileMixins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php


namespace Blueprint;


class FileMixins
{
private $stubs = [];

public function stub()
{
return function ($path) {
if (!isset($this->stubs[$path])) {
$this->stubs[$path] = $this->get(STUBS_PATH . DIRECTORY_SEPARATOR . $path);
}

return $this->stubs[$path];
};
}
}
46 changes: 20 additions & 26 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Contracts\Generator;
use Blueprint\Models\Controller;
use Blueprint\Models\Statements\DispatchStatement;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function output(array $tree): array
{
$output = [];

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

/** @var \Blueprint\Models\Controller $controller */
foreach ($tree['controllers'] as $controller) {
Expand All @@ -52,7 +53,7 @@ public function output(array $tree): array

protected function populateStub(string $stub, Controller $controller)
{
$stub = str_replace('DummyNamespace', 'App\\Http\\Controllers', $stub);
$stub = str_replace('DummyNamespace', $controller->fullyQualifiedNamespace(), $stub);
$stub = str_replace('DummyClass', $controller->className(), $stub);
$stub = str_replace('// methods...', $this->buildMethods($controller), $stub);
$stub = str_replace('// imports...', $this->buildImports($controller), $stub);
Expand All @@ -62,7 +63,7 @@ protected function populateStub(string $stub, Controller $controller)

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

$methods = '';

Expand All @@ -71,7 +72,7 @@ private function buildMethods(Controller $controller)

if (in_array($name, ['edit', 'update', 'show', 'destroy'])) {
$context = Str::singular($controller->prefix());
$reference = 'App\\' . $context;
$reference = config('blueprint.namespace') . '\\' . $context;
$variable = '$' . Str::camel($context);

// TODO: verify controller prefix references a model
Expand All @@ -88,21 +89,23 @@ private function buildMethods(Controller $controller)
if ($statement instanceof SendStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Mail');
$this->addImport($controller, 'App\\Mail\\' . $statement->mail());
$this->addImport($controller, config('blueprint.namespace') . '\\Mail\\' . $statement->mail());
} elseif ($statement instanceof ValidateStatement) {
$class = $controller->name() . Str::studly($name) . 'Request';
$class_name = $controller->name() . Str::studly($name) . 'Request';

$method = str_replace('\Illuminate\Http\Request $request', '\\App\\Http\\Requests\\' . $class . ' $request', $method);
$method = str_replace('(Request $request', '(' . $class . ' $request', $method);
$fqcn = config('blueprint.namespace') . '\\Http\\Requests\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $class_name;

$this->addImport($controller, 'App\\Http\\Requests\\' . $class);
$method = str_replace('\Illuminate\Http\Request $request', '\\' . $fqcn . ' $request', $method);
$method = str_replace('(Request $request', '(' . $class_name . ' $request', $method);

$this->addImport($controller, $fqcn);
} elseif ($statement instanceof DispatchStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
$this->addImport($controller, 'App\\Jobs\\' . $statement->job());
$this->addImport($controller, config('blueprint.namespace') . '\\Jobs\\' . $statement->job());
} elseif ($statement instanceof FireStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
if (!$statement->isNamedEvent()) {
$this->addImport($controller, 'App\\Events\\' . $statement->event());
$this->addImport($controller, config('blueprint.namespace') . '\\Events\\' . $statement->event());
}
} elseif ($statement instanceof RenderStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
Expand All @@ -112,10 +115,10 @@ private function buildMethods(Controller $controller)
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof EloquentStatement) {
$body .= self::INDENT . $statement->output($controller->prefix(), $name) . PHP_EOL;
$this->addImport($controller, 'App\\' . $this->determineModel($controller->prefix(), $statement->reference()));
$this->addImport($controller, config('blueprint.namespace') . '\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $this->determineModel($controller, $statement->reference()));
} elseif ($statement instanceof QueryStatement) {
$body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL;
$this->addImport($controller, 'App\\' . $this->determineModel($controller->prefix(), $statement->model()));
$this->addImport($controller, config('blueprint.namespace') . '\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $this->determineModel($controller, $statement->model()));
}

$body .= PHP_EOL;
Expand All @@ -133,18 +136,9 @@ private function buildMethods(Controller $controller)

protected function getPath(Controller $controller)
{
return 'app/Http/Controllers/' . $controller->className() . '.php';
}

private function methodStub()
{
static $stub = '';
$path = str_replace('\\', '/', Blueprint::relativeNamespace($controller->fullyQualifiedClassName()));

if (empty($stub)) {
$stub = $this->files->get(STUBS_PATH . '/controller/method.stub');
}

return $stub;
return config('blueprint.app_path') . '/' . $path . '.php';
}

private function addImport(Controller $controller, $class)
Expand All @@ -162,10 +156,10 @@ private function buildImports(Controller $controller)
}, $imports));
}

private function determineModel(string $prefix, ?string $reference)
private function determineModel(Controller $controller, ?string $reference)
{
if (empty($reference) || $reference === 'id') {
return Str::studly(Str::singular($prefix));
return Str::studly(Str::singular($controller->prefix()));
}

if (Str::contains($reference, '.')) {
Expand Down
13 changes: 9 additions & 4 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function output(array $tree): array
{
$output = [];

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

/** @var \Blueprint\Models\Model $model */
foreach ($tree['models'] as $model) {
Expand All @@ -40,12 +40,17 @@ public function output(array $tree): array

protected function getPath(Model $model)
{
return 'database/factories/' . $model->name() . 'Factory.php';
$path = $model->name();
if ($model->namespace()) {
$path = str_replace('\\', '/', $model->namespace()) . '/' . $path;
}

return 'database/factories/' . $path . 'Factory.php';
}

protected function populateStub(string $stub, Model $model)
{
$stub = str_replace('DummyNamespace', 'App', $stub);
$stub = str_replace('DummyModel', $model->fullyQualifiedClassName(), $stub);
$stub = str_replace('DummyClass', $model->name(), $stub);
$stub = str_replace('// definition...', $this->buildDefinition($model), $stub);

Expand All @@ -67,7 +72,7 @@ protected function buildDefinition(Model $model)
$class = Str::studly($column->attributes()[0] ?? $name);

$definition .= self::INDENT . "'{$column->name()}' => ";
$definition .= sprintf("factory(\App\%s::class)", $class);
$definition .= sprintf("factory(%s::class)", '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
$definition .= ',' . PHP_EOL;
} else {
$definition .= self::INDENT . "'{$column->name()}' => ";
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function output(array $tree): array
{
$output = [];

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

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

Expand Down
32 changes: 12 additions & 20 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Contracts\Generator;
use Blueprint\Models\Column;
use Blueprint\Models\Model;
Expand All @@ -21,7 +22,7 @@ public function output(array $tree): array
{
$output = [];

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

/** @var \Blueprint\Models\Model $model */
foreach ($tree['models'] as $model) {
Expand All @@ -39,7 +40,7 @@ public function output(array $tree): array

protected function populateStub(string $stub, Model $model)
{
$stub = str_replace('DummyNamespace', 'App', $stub);
$stub = str_replace('DummyNamespace', $model->fullyQualifiedNamespace(), $stub);
$stub = str_replace('DummyClass', $model->name(), $stub);

$body = $this->buildProperties($model);
Expand All @@ -58,19 +59,19 @@ private function buildProperties(Model $model)

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

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

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

return trim($properties);
Expand All @@ -87,13 +88,13 @@ private function buildRelationships(Model $model)
}

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

/** @var Column $column */
foreach ($columns as $column) {
$name = Str::beforeLast($column->name(), '_id');
$class = Str::studly($column->attributes()[0] ?? $name);
$relationship = sprintf("\$this->belongsTo(\App\%s::class)", $class);
$relationship = sprintf("\$this->belongsTo(%s::class)", '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);

$method = str_replace('DummyName', Str::camel($name), $template);
$method = str_replace('null', $relationship, $method);
Expand All @@ -106,7 +107,9 @@ private function buildRelationships(Model $model)

protected function getPath(Model $model)
{
return 'app/' . $model->name() . '.php';
$path = str_replace('\\', '/', Blueprint::relativeNamespace($model->fullyQualifiedClassName()));

return config('blueprint.app_path') . '/' . $path . '.php';
}

private function fillableColumns(array $columns)
Expand Down Expand Up @@ -174,17 +177,6 @@ private function pretty_print_array(array $data, $assoc = true)
return trim($output);
}

private function getStub(string $stub)
{
static $stubs = [];

if (empty($stubs[$stub])) {
$stubs[$stub] = $this->files->get(STUBS_PATH . '/model/' . $stub . '.stub');
}

return $stubs[$stub];
}

private function addTraits(Model $model, $stub)
{
if (!$model->usesSoftDeletes()) {
Expand Down
Loading