From 07fd2760fbbd09c7156d81eb5dedfde796e5b353 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Fri, 8 Nov 2019 10:51:02 +0100 Subject: [PATCH 1/4] add setUp: void return type --- tests/Feature/BlueprintTest.php | 28 ++++++++------- .../Generator/FactoryGeneratorTest.php | 9 +++-- .../Generator/MigrationGeneratorTest.php | 7 ++-- .../Feature/Generator/ModelGeneratorTest.php | 5 ++- tests/Feature/Lexers/ModelLexerTest.php | 35 +++++++++---------- 5 files changed, 41 insertions(+), 43 deletions(-) diff --git a/tests/Feature/BlueprintTest.php b/tests/Feature/BlueprintTest.php index 39ecf35d..5bcfd516 100644 --- a/tests/Feature/BlueprintTest.php +++ b/tests/Feature/BlueprintTest.php @@ -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 { @@ -15,7 +15,7 @@ class BlueprintTest extends TestCase */ private $subject; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -53,10 +53,10 @@ public function it_parses_controllers() 'controllers' => [ 'UserController' => [ 'index' => [ - 'action' => 'detail' + 'action' => 'detail', ], 'create' => [ - 'action' => 'additional detail' + 'action' => 'additional detail', ], ], 'RoleController' => [ @@ -100,7 +100,7 @@ public function it_parses_the_readme_example() 'title' => 'string', 'content' => 'bigtext', 'published_at' => 'nullable timestamp', - 'timestamps' => 'timestamps' + 'timestamps' => 'timestamps', ], ], 'controllers' => [ @@ -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) + ); } /** @@ -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)); } @@ -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); @@ -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); @@ -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)); } -} \ No newline at end of file +} diff --git a/tests/Feature/Generator/FactoryGeneratorTest.php b/tests/Feature/Generator/FactoryGeneratorTest.php index 5e78efa2..516c731e 100644 --- a/tests/Feature/Generator/FactoryGeneratorTest.php +++ b/tests/Feature/Generator/FactoryGeneratorTest.php @@ -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 { @@ -15,7 +15,7 @@ class FactoryGeneratorTest extends TestCase /** @var FactoryGenerator */ private $subject; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -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'], ]; } -} \ No newline at end of file +} diff --git a/tests/Feature/Generator/MigrationGeneratorTest.php b/tests/Feature/Generator/MigrationGeneratorTest.php index bf6adb22..051435de 100644 --- a/tests/Feature/Generator/MigrationGeneratorTest.php +++ b/tests/Feature/Generator/MigrationGeneratorTest.php @@ -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 { @@ -16,7 +16,7 @@ class MigrationGeneratorTest extends TestCase /** @var MigrationGenerator */ private $subject; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -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 [ diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 2869fc4e..2e57c3fc 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -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 { @@ -15,7 +15,7 @@ class ModelGeneratorTest extends TestCase /** @var ModelGenerator */ private $subject; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -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 [ diff --git a/tests/Feature/Lexers/ModelLexerTest.php b/tests/Feature/Lexers/ModelLexerTest.php index 9e4405d1..12a348f7 100644 --- a/tests/Feature/Lexers/ModelLexerTest.php +++ b/tests/Feature/Lexers/ModelLexerTest.php @@ -2,8 +2,8 @@ namespace Tests\Feature\Lexers; -use Blueprint\Lexers\ModelLexer; use Tests\TestCase; +use Blueprint\Lexers\ModelLexer; class ModelLexerTest extends TestCase { @@ -12,7 +12,7 @@ class ModelLexerTest extends TestCase */ private $subject; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -36,11 +36,11 @@ public function it_returns_models() 'models' => [ 'ModelOne' => [ 'id' => 'id', - 'name' => 'string nullable' + 'name' => 'string nullable', ], 'ModelTwo' => [ 'count' => 'integer', - 'timestamps' => 'timestamps' + 'timestamps' => 'timestamps', ], ], ]; @@ -85,8 +85,8 @@ public function it_defaults_the_id_column() $tokens = [ 'models' => [ 'Model' => [ - 'title' => 'string nullable' - ] + 'title' => 'string nullable', + ], ], ]; @@ -120,7 +120,7 @@ public function it_disables_timestamps() 'models' => [ 'Model' => [ 'timestamps' => false, - ] + ], ], ]; @@ -142,8 +142,8 @@ public function it_defaults_to_string_datatype() $tokens = [ 'models' => [ 'Model' => [ - 'title' => 'nullable' - ] + 'title' => 'nullable', + ], ], ]; @@ -178,8 +178,8 @@ public function it_accepts_lowercase_keywords() 'Model' => [ 'sequence' => 'unsignedbiginteger autoincrement', 'content' => 'longtext', - 'saved_at' => 'timestamptz usecurrent' - ] + 'saved_at' => 'timestamptz usecurrent', + ], ], ]; @@ -212,7 +212,6 @@ public function it_accepts_lowercase_keywords() $this->assertEquals(['useCurrent'], $columns['saved_at']->modifiers()); } - /** * @test * @dataProvider dataTypeAttributesDataProvider @@ -222,8 +221,8 @@ public function it_handles_data_type_attributes($definition, $data_type, $attrib $tokens = [ 'models' => [ 'Model' => [ - 'column' => $definition - ] + 'column' => $definition, + ], ], ]; @@ -256,8 +255,8 @@ public function it_handles_modifier_attributes($definition, $modifier, $attribut $tokens = [ 'models' => [ 'Model' => [ - 'column' => $definition . ' nullable' - ] + 'column' => $definition . ' nullable', + ], ], ]; @@ -300,11 +299,11 @@ public function modifierAttributesProvider() return [ ['default:5', 'default', 5], ['default:0.00', 'default', 0.00], - ["default:string", 'default', 'string'], + ['default:string', 'default', 'string'], ["default:'empty'", 'default', "'empty'"], ['default:""', 'default', '""'], ['charset:utf8', 'charset', 'utf8'], ['collation:utf8_unicode', 'collation', 'utf8_unicode'], ]; } -} \ No newline at end of file +} From 8950704920cdfc6c4388740d81619411c9d8f842 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Fri, 8 Nov 2019 11:32:00 +0100 Subject: [PATCH 2/4] add blueprint config to specify which generators and lexers to register --- config/blueprint.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 config/blueprint.php diff --git a/config/blueprint.php b/config/blueprint.php new file mode 100644 index 00000000..74d451d6 --- /dev/null +++ b/config/blueprint.php @@ -0,0 +1,13 @@ + [ + \Blueprint\Generators\MigrationGenerator::class, + \Blueprint\Generators\ModelGenerator::class, + \Blueprint\Generators\FactoryGenerator::class, + ], + + 'lexers' => [ + \Blueprint\Lexers\ModelLexer::class, + ], +]; From 466e6bef8611ca8e40a5295236077aa476230f52 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Fri, 8 Nov 2019 11:32:40 +0100 Subject: [PATCH 3/4] add blueprint singleton --- src/Blueprint.php | 20 ++++++++++++---- src/BlueprintServiceProvider.php | 39 +++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/Blueprint.php b/src/Blueprint.php index e5fbd45b..0a2d8f70 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -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); @@ -23,7 +35,7 @@ public function analyze(array $tokens) { $registry = [ 'models' => [], - 'controllers' => [] + 'controllers' => [], ]; foreach ($this->lexers as $lexer) { @@ -53,4 +65,4 @@ public function registerGenerator(Generator $generator) { $this->generators[] = $generator; } -} \ No newline at end of file +} diff --git a/src/BlueprintServiceProvider.php b/src/BlueprintServiceProvider.php index 772f18a1..5f506408 100644 --- a/src/BlueprintServiceProvider.php +++ b/src/BlueprintServiceProvider.php @@ -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']); } @@ -45,5 +63,4 @@ public function provides() { return ['command.blueprint.build']; } - -} \ No newline at end of file +} From 5bc4b2d6cbc0ba6cdbe644451a94f26ce4f65342 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Fri, 8 Nov 2019 11:32:54 +0100 Subject: [PATCH 4/4] use singleton --- src/BlueprintCommand.php | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/BlueprintCommand.php b/src/BlueprintCommand.php index 4824aed7..70b322bd 100644 --- a/src/BlueprintCommand.php +++ b/src/BlueprintCommand.php @@ -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 @@ -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) @@ -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); @@ -68,7 +60,6 @@ public function handle() }); } - /** * Get the console command arguments. * @@ -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'; }