Skip to content

Commit eff605f

Browse files
Support inline configuration (#575)
1 parent 73d8b49 commit eff605f

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

src/BlueprintServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function register()
6363

6464
$this->app->singleton(Blueprint::class, function ($app) {
6565
$blueprint = new Blueprint();
66+
$blueprint->registerLexer(new \Blueprint\Lexers\ConfigLexer($app));
6667
$blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
6768
$blueprint->registerLexer(new \Blueprint\Lexers\SeederLexer());
6869
$blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new \Blueprint\Lexers\StatementLexer()));

src/Lexers/ConfigLexer.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Blueprint\Lexers;
4+
5+
use Blueprint\Contracts\Lexer;
6+
use Illuminate\Container\Container;
7+
8+
class ConfigLexer implements Lexer
9+
{
10+
private $app;
11+
12+
public function __construct(Container $app = null)
13+
{
14+
$this->app = $app ?? Container::getInstance();
15+
}
16+
17+
public function analyze(array $tokens): array
18+
{
19+
if (array_key_exists('config', $tokens) && is_array($tokens['config'])) {
20+
$this->analyzeValue($tokens['config']);
21+
}
22+
23+
return [];
24+
}
25+
26+
private function analyzeValue(array $config): void
27+
{
28+
$this->app['config']->set(
29+
'blueprint',
30+
array_merge(
31+
$this->app['config']->get('blueprint'),
32+
$config
33+
)
34+
);
35+
}
36+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Tests\Feature\Lexers;
4+
5+
use Blueprint\Blueprint;
6+
use Blueprint\Generators\ControllerGenerator;
7+
use Blueprint\Generators\ModelGenerator;
8+
use Blueprint\Lexers\ConfigLexer;
9+
use Blueprint\Lexers\ControllerLexer;
10+
use Blueprint\Lexers\ModelLexer;
11+
use Blueprint\Lexers\StatementLexer;
12+
use Tests\TestCase;
13+
14+
/**
15+
* @see ConfigLexer
16+
*/
17+
class ConfigLexerTest extends TestCase
18+
{
19+
private $blueprint;
20+
21+
/**
22+
* @var ConfigLexer
23+
*/
24+
private $subject;
25+
26+
protected function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
$this->subject = new ConfigLexer();
31+
32+
$this->modelGenerator = new ModelGenerator($this->filesystem);
33+
$this->controllerGenerator = new ControllerGenerator($this->filesystem);
34+
35+
$this->blueprint = new Blueprint();
36+
$this->blueprint->registerLexer(new ModelLexer());
37+
$this->blueprint->registerLexer(new ConfigLexer());
38+
$this->blueprint->registerLexer(new ControllerLexer(new StatementLexer()));
39+
$this->blueprint->registerGenerator($this->modelGenerator);
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function it_updates_config(): void
46+
{
47+
$tokens = ['config' => ['key' => 'value']];
48+
49+
$this->subject->analyze($tokens);
50+
51+
$this->assertSame($tokens['config']['key'], config('blueprint.key'));
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function it_uses_app_path_and_namespace_from_inline_configuration(): void
58+
{
59+
$this->filesystem->expects('stub')
60+
->with('model.class.stub')
61+
->andReturn($this->stub('model.class.stub'));
62+
63+
$this->filesystem->expects('stub')
64+
->with('model.fillable.stub')
65+
->andReturn($this->stub('model.fillable.stub'));
66+
67+
$this->filesystem->expects('stub')
68+
->with('model.casts.stub')
69+
->andReturn($this->stub('model.casts.stub'));
70+
71+
$this->filesystem->expects('stub')
72+
->with('model.method.stub')
73+
->andReturn($this->stub('model.method.stub'));
74+
75+
$this->filesystem->expects('exists')
76+
->with('atum/Models')
77+
->andReturnFalse();
78+
$this->filesystem->expects('makeDirectory')
79+
->with('atum/Models', 0755, true);
80+
$this->filesystem->expects('put')
81+
->with('atum/Models/Comment.php', $this->fixture('models/model-configured.php'));
82+
83+
$tokens = $this->blueprint->parse($this->fixture('drafts/relationships-configured.yaml'));
84+
$tree = $this->blueprint->analyze($tokens);
85+
86+
$this->assertEquals(['created' => ['atum/Models/Comment.php']], $this->modelGenerator->output($tree));
87+
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function it_uses_controller_namespace_config_from_yaml_override()
93+
{
94+
$this->filesystem->expects('stub')
95+
->with('controller.class.stub')
96+
->andReturn($this->stub('controller.class.stub'));
97+
$this->filesystem->expects('stub')
98+
->with('controller.method.stub')
99+
->andReturn($this->stub('controller.method.stub'));
100+
101+
$this->filesystem->expects('exists')
102+
->with('shift/Other/Http')
103+
->andReturnFalse();
104+
$this->filesystem->expects('makeDirectory')
105+
->with('shift/Other/Http', 0755, true);
106+
$this->filesystem->expects('put')
107+
->with('shift/Other/Http/UserController.php', $this->fixture('controllers/controller-configured.php'));
108+
109+
$tokens = $this->blueprint->parse($this->fixture('drafts/controller-configured.yaml'));
110+
$tree = $this->blueprint->analyze($tokens);
111+
112+
$this->assertEquals(['created' => ['shift/Other/Http/UserController.php']], $this->controllerGenerator->output($tree));
113+
}
114+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
controllers:
2+
User:
3+
index:
4+
query: all:users
5+
render: user.index with:users
6+
7+
store:
8+
validate: name
9+
save: user
10+
flash: user.name
11+
redirect: post.index
12+
13+
config:
14+
app_path: shift
15+
namespace: Some\App
16+
controllers_namespace: Other\Http
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
models:
2+
Comment:
3+
post_id: id
4+
author_id: id:user
5+
6+
config:
7+
use_constraints: true
8+
app_path: atum
9+
namespace: Some\App

0 commit comments

Comments
 (0)