Skip to content

Commit e4f06d3

Browse files
committed
Register Blueprint as singleton for extensibility
1 parent 7831480 commit e4f06d3

File tree

4 files changed

+82
-21
lines changed

4 files changed

+82
-21
lines changed

src/BlueprintCommand.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,8 @@ public function handle()
5050
}
5151

5252
$contents = $this->files->get($file);
53-
54-
$blueprint = new Blueprint();
55-
56-
$blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
57-
$blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new \Blueprint\Lexers\StatementLexer()));
58-
59-
$blueprint->registerGenerator(new \Blueprint\Generators\MigrationGenerator($this->files));
60-
$blueprint->registerGenerator(new \Blueprint\Generators\ModelGenerator($this->files));
61-
$blueprint->registerGenerator(new \Blueprint\Generators\FactoryGenerator($this->files));
62-
63-
$blueprint->registerGenerator(new \Blueprint\Generators\ControllerGenerator($this->files));
64-
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\EventGenerator($this->files));
65-
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\FormRequestGenerator($this->files));
66-
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\JobGenerator($this->files));
67-
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\MailGenerator($this->files));
68-
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\ViewGenerator($this->files));
69-
$blueprint->registerGenerator(new \Blueprint\Generators\RouteGenerator($this->files));
70-
71-
$tokens = $blueprint->parse($contents);
72-
$registry = $blueprint->analyze($tokens);
73-
$generated = $blueprint->generate($registry);
53+
$blueprint = resolve(Blueprint::class);
54+
$generated = Builder::execute($blueprint, $contents);
7455

7556
collect($generated)->each(function ($files, $action) {
7657
$this->line(Str::studly($action) . ':', $this->outputStyle($action));

src/BlueprintServiceProvider.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,33 @@ function ($app) {
3131
return new BlueprintCommand($app['files']);
3232
}
3333
);
34+
3435
$this->app->bind('command.blueprint.erase',
3536
function ($app) {
3637
return new EraseCommand($app['files']);
3738
}
3839
);
3940

41+
$this->app->singleton(Blueprint::class, function ($app) {
42+
$blueprint = new Blueprint();
43+
$blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
44+
$blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new \Blueprint\Lexers\StatementLexer()));
45+
46+
$blueprint->registerGenerator(new \Blueprint\Generators\MigrationGenerator($app['files']));
47+
$blueprint->registerGenerator(new \Blueprint\Generators\ModelGenerator($app['files']));
48+
$blueprint->registerGenerator(new \Blueprint\Generators\FactoryGenerator($app['files']));
49+
50+
$blueprint->registerGenerator(new \Blueprint\Generators\ControllerGenerator($app['files']));
51+
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\EventGenerator($app['files']));
52+
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\FormRequestGenerator($app['files']));
53+
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\JobGenerator($app['files']));
54+
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\MailGenerator($app['files']));
55+
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\ViewGenerator($app['files']));
56+
$blueprint->registerGenerator(new \Blueprint\Generators\RouteGenerator($app['files']));
57+
58+
return $blueprint;
59+
});
60+
4061
$this->commands([
4162
'command.blueprint.build',
4263
'command.blueprint.erase',
@@ -53,6 +74,7 @@ public function provides()
5374
return [
5475
'command.blueprint.build',
5576
'command.blueprint.erase',
77+
Blueprint::class,
5678
];
5779
}
5880
}

src/Builder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Blueprint;
4+
5+
class Builder
6+
{
7+
public static function execute(Blueprint $blueprint, string $draft)
8+
{
9+
// TODO: read in previous models...
10+
11+
$tokens = $blueprint->parse($draft);
12+
$registry = $blueprint->analyze($tokens);
13+
$generated = $blueprint->generate($registry);
14+
15+
// TODO: save to .blueprint
16+
17+
return $generated;
18+
}
19+
}

tests/Unit/BuilderTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Blueprint\Blueprint;
6+
use Blueprint\Builder;
7+
use Tests\TestCase;
8+
9+
class BuilderTest extends TestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
public function execute_uses_blueprint_to_build_draft()
15+
{
16+
$digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
17+
shuffle($digits);
18+
19+
$draft = implode($digits);
20+
$tokens = $digits;
21+
$registry = array_rand($digits, 9);
22+
$generated = array_rand($digits, 9);
23+
24+
$blueprint = \Mockery::mock(Blueprint::class);
25+
$blueprint->expects('parse')
26+
->with($draft)
27+
->andReturn($tokens);
28+
$blueprint->expects('analyze')
29+
->with($tokens)
30+
->andReturn($registry);
31+
$blueprint->expects('generate')
32+
->with($registry)
33+
->andReturn($generated);
34+
35+
$actual = Builder::execute($blueprint, $draft);
36+
37+
$this->assertSame($generated, $actual);
38+
}
39+
}

0 commit comments

Comments
 (0)