Skip to content
Closed
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
13 changes: 13 additions & 0 deletions config/blueprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'generators' => [
\Blueprint\Generators\MigrationGenerator::class,
\Blueprint\Generators\ModelGenerator::class,
\Blueprint\Generators\FactoryGenerator::class,
],

'lexers' => [
\Blueprint\Lexers\ModelLexer::class,
],
];
20 changes: 16 additions & 4 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

namespace Blueprint;


use Blueprint\Contracts\Generator;
use Blueprint\Contracts\Lexer;
use Symfony\Component\Yaml\Yaml;
use Blueprint\Contracts\Generator;

class Blueprint
{
private $lexers = [];
private $generators = [];

public function boot()
{
collect(config('blueprint.lexers'))->each(function ($lexer) {
$this->registerLexer(new $lexer());
});

collect(config('blueprint.generators'))->each(function ($generatorClass) {
$generator = resolve('blueprint.generators')[$generatorClass];

$this->registerGenerator($generator);
});
}

public function parse($content)
{
$content = preg_replace('/^(\s+)(id|timestamps)$/m', '$1$2: $2', $content);
Expand All @@ -23,7 +35,7 @@ public function analyze(array $tokens)
{
$registry = [
'models' => [],
'controllers' => []
'controllers' => [],
];

foreach ($this->lexers as $lexer) {
Expand Down Expand Up @@ -53,4 +65,4 @@ public function registerGenerator(Generator $generator)
{
$this->generators[] = $generator;
}
}
}
19 changes: 5 additions & 14 deletions src/BlueprintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Blueprint;

use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;

class BlueprintCommand extends Command
Expand All @@ -27,7 +27,7 @@ class BlueprintCommand extends Command
protected $files;

/**
* @param Filesystem $files
* @param Filesystem $files
* @param \Illuminate\Contracts\View\Factory $view
*/
public function __construct(Filesystem $files)
Expand All @@ -42,18 +42,10 @@ public function __construct(Filesystem $files)
*
* @return mixed
*/
public function handle()
public function handle(Blueprint $blueprint)
{
$contents = $this->files->get($this->argument('draft'));

$blueprint = new Blueprint();

$blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());

$blueprint->registerGenerator(new \Blueprint\Generators\MigrationGenerator($this->files));
$blueprint->registerGenerator(new \Blueprint\Generators\ModelGenerator($this->files));
$blueprint->registerGenerator(new \Blueprint\Generators\FactoryGenerator($this->files));

$tokens = $blueprint->parse($contents);
$registry = $blueprint->analyze($tokens);
$generated = $blueprint->generate($registry);
Expand All @@ -68,7 +60,6 @@ public function handle()
});
}


/**
* Get the console command arguments.
*
Expand All @@ -93,9 +84,9 @@ protected function getOptions()

private function outputStyle($action)
{
if ($action === 'deleted') {
if ('deleted' === $action) {
return 'error';
} elseif ($action === 'updated') {
} elseif ('updated' === $action) {
return 'warning';
}

Expand Down
39 changes: 28 additions & 11 deletions src/BlueprintServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,50 @@

namespace Blueprint;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\ServiceProvider;
use Blueprint\Generators\ModelGenerator;
use Blueprint\Generators\FactoryGenerator;
use Blueprint\Generators\MigrationGenerator;
use Illuminate\Contracts\Support\DeferrableProvider;

class BlueprintServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
public function boot(Blueprint $blueprint)
{
// ...
if (!defined('STUBS_PATH')) {
$this->publishes([
__DIR__ . '/../config/blueprint.php' => config_path('blueprint.php'),
], 'config');

if (! defined('STUBS_PATH')) {
define('STUBS_PATH', dirname(__DIR__) . '/stubs');
}

$blueprint->boot();
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('command.blueprint.build',
$this->mergeConfigFrom(__DIR__ . '/../config/blueprint.php', 'blueprint');

$this->app->bind('blueprint.generators', function ($app) {
return [
FactoryGenerator::class => new FactoryGenerator($app->make(Filesystem::class)),
MigrationGenerator::class => new MigrationGenerator($app->make(Filesystem::class)),
ModelGenerator::class => new ModelGenerator($app->make(Filesystem::class)),
];
});

$this->app->singleton(Blueprint::class, Blueprint::class);

$this->app->bind(
'command.blueprint.build',
function ($app) {
return new BlueprintCommand($app['files']);
}
Expand All @@ -45,5 +63,4 @@ public function provides()
{
return ['command.blueprint.build'];
}

}
}
28 changes: 15 additions & 13 deletions tests/Feature/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Tests\Feature;

use Tests\TestCase;
use Blueprint\Blueprint;
use Blueprint\Contracts\Generator;
use Blueprint\Contracts\Lexer;
use Blueprint\Contracts\Generator;
use Symfony\Component\Yaml\Exception\ParseException;
use Tests\TestCase;

class BlueprintTest extends TestCase
{
Expand All @@ -15,7 +15,7 @@ class BlueprintTest extends TestCase
*/
private $subject;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -53,10 +53,10 @@ public function it_parses_controllers()
'controllers' => [
'UserController' => [
'index' => [
'action' => 'detail'
'action' => 'detail',
],
'create' => [
'action' => 'additional detail'
'action' => 'additional detail',
],
],
'RoleController' => [
Expand Down Expand Up @@ -100,7 +100,7 @@ public function it_parses_the_readme_example()
'title' => 'string',
'content' => 'bigtext',
'published_at' => 'nullable timestamp',
'timestamps' => 'timestamps'
'timestamps' => 'timestamps',
],
],
'controllers' => [
Expand Down Expand Up @@ -141,11 +141,13 @@ public function analyze_return_default_tree_for_empty_tokens()
{
$tokens = [];

$this->assertEquals([
$this->assertEquals(
[
'models' => [],
'controllers' => []
'controllers' => [],
],
$this->subject->analyze($tokens));
$this->subject->analyze($tokens)
);
}

/**
Expand All @@ -164,7 +166,7 @@ public function analyze_uses_register_lexers_to_analyze_tokens()
$this->assertEquals([
'models' => [],
'controllers' => [],
'mock' => 'lexer'
'mock' => 'lexer',
], $this->subject->analyze($tokens));
}

Expand All @@ -180,7 +182,7 @@ public function generate_uses_registered_generators_and_returns_generated_files(
->andReturn([
'created' => ['one/new.php'],
'updated' => ['one/existing.php'],
'deleted' => ['one/trashed.php']
'deleted' => ['one/trashed.php'],
]);

$generatorTwo = \Mockery::mock(Generator::class);
Expand All @@ -189,7 +191,7 @@ public function generate_uses_registered_generators_and_returns_generated_files(
->andReturn([
'created' => ['two/new.php'],
'updated' => ['two/existing.php'],
'deleted' => ['two/trashed.php']
'deleted' => ['two/trashed.php'],
]);

$this->subject->registerGenerator($generatorOne);
Expand All @@ -201,4 +203,4 @@ public function generate_uses_registered_generators_and_returns_generated_files(
'deleted' => ['one/trashed.php', 'two/trashed.php'],
], $this->subject->generate($tree));
}
}
}
9 changes: 4 additions & 5 deletions tests/Feature/Generator/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Tests\Feature\Generators;

use Tests\TestCase;
use Blueprint\Blueprint;
use Blueprint\Generators\FactoryGenerator;
use Tests\TestCase;

class FactoryGeneratorTest extends TestCase
{
Expand All @@ -15,7 +15,7 @@ class FactoryGeneratorTest extends TestCase
/** @var FactoryGenerator */
private $subject;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -60,11 +60,10 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
}


public function modelTreeDataProvider()
{
return [
['definitions/post.bp', 'database/factories/PostFactory.php', 'factories/post.php']
['definitions/post.bp', 'database/factories/PostFactory.php', 'factories/post.php'],
];
}
}
}
7 changes: 3 additions & 4 deletions tests/Feature/Generator/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Tests\Feature\Generators;

use Blueprint\Blueprint;
use Blueprint\Generators\MigrationGenerator;
use Carbon\Carbon;
use Tests\TestCase;
use Blueprint\Blueprint;
use Blueprint\Generators\MigrationGenerator;

class MigrationGeneratorTest extends TestCase
{
Expand All @@ -16,7 +16,7 @@ class MigrationGeneratorTest extends TestCase
/** @var MigrationGenerator */
private $subject;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -66,7 +66,6 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
}


public function modelTreeDataProvider()
{
return [
Expand Down
5 changes: 2 additions & 3 deletions tests/Feature/Generator/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Tests\Feature\Generators;

use Tests\TestCase;
use Blueprint\Blueprint;
use Blueprint\Generators\ModelGenerator;
use Tests\TestCase;

class ModelGeneratorTest extends TestCase
{
Expand All @@ -15,7 +15,7 @@ class ModelGeneratorTest extends TestCase
/** @var ModelGenerator */
private $subject;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -72,7 +72,6 @@ public function output_writes_migration_for_model_tree($definition, $path, $mode
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
}


public function modelTreeDataProvider()
{
return [
Expand Down
Loading