diff --git a/src/BlueprintServiceProvider.php b/src/BlueprintServiceProvider.php index 5e9f81d2..e524cf47 100644 --- a/src/BlueprintServiceProvider.php +++ b/src/BlueprintServiceProvider.php @@ -63,6 +63,7 @@ public function register() $this->app->singleton(Blueprint::class, function ($app) { $blueprint = new Blueprint(); + $blueprint->registerLexer(new \Blueprint\Lexers\ConfigLexer($app)); $blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer()); $blueprint->registerLexer(new \Blueprint\Lexers\SeederLexer()); $blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new \Blueprint\Lexers\StatementLexer())); diff --git a/src/Lexers/ConfigLexer.php b/src/Lexers/ConfigLexer.php new file mode 100644 index 00000000..1f49bc57 --- /dev/null +++ b/src/Lexers/ConfigLexer.php @@ -0,0 +1,36 @@ +app = $app ?? Container::getInstance(); + } + + public function analyze(array $tokens): array + { + if (array_key_exists('config', $tokens) && is_array($tokens['config'])) { + $this->analyzeValue($tokens['config']); + } + + return []; + } + + private function analyzeValue(array $config): void + { + $this->app['config']->set( + 'blueprint', + array_merge( + $this->app['config']->get('blueprint'), + $config + ) + ); + } +} diff --git a/tests/Feature/Lexers/ConfigLexerTest.php b/tests/Feature/Lexers/ConfigLexerTest.php new file mode 100644 index 00000000..1df029d3 --- /dev/null +++ b/tests/Feature/Lexers/ConfigLexerTest.php @@ -0,0 +1,114 @@ +subject = new ConfigLexer(); + + $this->modelGenerator = new ModelGenerator($this->filesystem); + $this->controllerGenerator = new ControllerGenerator($this->filesystem); + + $this->blueprint = new Blueprint(); + $this->blueprint->registerLexer(new ModelLexer()); + $this->blueprint->registerLexer(new ConfigLexer()); + $this->blueprint->registerLexer(new ControllerLexer(new StatementLexer())); + $this->blueprint->registerGenerator($this->modelGenerator); + } + + /** + * @test + */ + public function it_updates_config(): void + { + $tokens = ['config' => ['key' => 'value']]; + + $this->subject->analyze($tokens); + + $this->assertSame($tokens['config']['key'], config('blueprint.key')); + } + + /** + * @test + */ + public function it_uses_app_path_and_namespace_from_inline_configuration(): void + { + $this->filesystem->expects('stub') + ->with('model.class.stub') + ->andReturn($this->stub('model.class.stub')); + + $this->filesystem->expects('stub') + ->with('model.fillable.stub') + ->andReturn($this->stub('model.fillable.stub')); + + $this->filesystem->expects('stub') + ->with('model.casts.stub') + ->andReturn($this->stub('model.casts.stub')); + + $this->filesystem->expects('stub') + ->with('model.method.stub') + ->andReturn($this->stub('model.method.stub')); + + $this->filesystem->expects('exists') + ->with('atum/Models') + ->andReturnFalse(); + $this->filesystem->expects('makeDirectory') + ->with('atum/Models', 0755, true); + $this->filesystem->expects('put') + ->with('atum/Models/Comment.php', $this->fixture('models/model-configured.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/relationships-configured.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => ['atum/Models/Comment.php']], $this->modelGenerator->output($tree)); + } + + /** + * @test + */ + public function it_uses_controller_namespace_config_from_yaml_override() + { + $this->filesystem->expects('stub') + ->with('controller.class.stub') + ->andReturn($this->stub('controller.class.stub')); + $this->filesystem->expects('stub') + ->with('controller.method.stub') + ->andReturn($this->stub('controller.method.stub')); + + $this->filesystem->expects('exists') + ->with('shift/Other/Http') + ->andReturnFalse(); + $this->filesystem->expects('makeDirectory') + ->with('shift/Other/Http', 0755, true); + $this->filesystem->expects('put') + ->with('shift/Other/Http/UserController.php', $this->fixture('controllers/controller-configured.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/controller-configured.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => ['shift/Other/Http/UserController.php']], $this->controllerGenerator->output($tree)); + } +} diff --git a/tests/fixtures/drafts/controller-configured.yaml b/tests/fixtures/drafts/controller-configured.yaml new file mode 100644 index 00000000..f7cf6b87 --- /dev/null +++ b/tests/fixtures/drafts/controller-configured.yaml @@ -0,0 +1,16 @@ +controllers: + User: + index: + query: all:users + render: user.index with:users + + store: + validate: name + save: user + flash: user.name + redirect: post.index + +config: + app_path: shift + namespace: Some\App + controllers_namespace: Other\Http diff --git a/tests/fixtures/drafts/relationships-configured.yaml b/tests/fixtures/drafts/relationships-configured.yaml new file mode 100644 index 00000000..39d28ba7 --- /dev/null +++ b/tests/fixtures/drafts/relationships-configured.yaml @@ -0,0 +1,9 @@ +models: + Comment: + post_id: id + author_id: id:user + +config: + use_constraints: true + app_path: atum + namespace: Some\App