Skip to content

Commit

Permalink
Merge pull request #44 from laravel-shift/extensible
Browse files Browse the repository at this point in the history
Register Blueprint as singleton for extensibility
  • Loading branch information
jasonmccreary authored Dec 20, 2019
2 parents 7831480 + e4f06d3 commit 899bbaa
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/BlueprintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,8 @@ public function handle()
}

$contents = $this->files->get($file);

$blueprint = new Blueprint();

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

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

$blueprint->registerGenerator(new \Blueprint\Generators\ControllerGenerator($this->files));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\EventGenerator($this->files));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\FormRequestGenerator($this->files));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\JobGenerator($this->files));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\MailGenerator($this->files));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\ViewGenerator($this->files));
$blueprint->registerGenerator(new \Blueprint\Generators\RouteGenerator($this->files));

$tokens = $blueprint->parse($contents);
$registry = $blueprint->analyze($tokens);
$generated = $blueprint->generate($registry);
$blueprint = resolve(Blueprint::class);
$generated = Builder::execute($blueprint, $contents);

collect($generated)->each(function ($files, $action) {
$this->line(Str::studly($action) . ':', $this->outputStyle($action));
Expand Down
22 changes: 22 additions & 0 deletions src/BlueprintServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,33 @@ function ($app) {
return new BlueprintCommand($app['files']);
}
);

$this->app->bind('command.blueprint.erase',
function ($app) {
return new EraseCommand($app['files']);
}
);

$this->app->singleton(Blueprint::class, function ($app) {
$blueprint = new Blueprint();
$blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
$blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new \Blueprint\Lexers\StatementLexer()));

$blueprint->registerGenerator(new \Blueprint\Generators\MigrationGenerator($app['files']));
$blueprint->registerGenerator(new \Blueprint\Generators\ModelGenerator($app['files']));
$blueprint->registerGenerator(new \Blueprint\Generators\FactoryGenerator($app['files']));

$blueprint->registerGenerator(new \Blueprint\Generators\ControllerGenerator($app['files']));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\EventGenerator($app['files']));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\FormRequestGenerator($app['files']));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\JobGenerator($app['files']));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\MailGenerator($app['files']));
$blueprint->registerGenerator(new \Blueprint\Generators\Statements\ViewGenerator($app['files']));
$blueprint->registerGenerator(new \Blueprint\Generators\RouteGenerator($app['files']));

return $blueprint;
});

$this->commands([
'command.blueprint.build',
'command.blueprint.erase',
Expand All @@ -53,6 +74,7 @@ public function provides()
return [
'command.blueprint.build',
'command.blueprint.erase',
Blueprint::class,
];
}
}
19 changes: 19 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Blueprint;

class Builder
{
public static function execute(Blueprint $blueprint, string $draft)
{
// TODO: read in previous models...

$tokens = $blueprint->parse($draft);
$registry = $blueprint->analyze($tokens);
$generated = $blueprint->generate($registry);

// TODO: save to .blueprint

return $generated;
}
}
39 changes: 39 additions & 0 deletions tests/Unit/BuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\Unit;

use Blueprint\Blueprint;
use Blueprint\Builder;
use Tests\TestCase;

class BuilderTest extends TestCase
{
/**
* @test
*/
public function execute_uses_blueprint_to_build_draft()
{
$digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
shuffle($digits);

$draft = implode($digits);
$tokens = $digits;
$registry = array_rand($digits, 9);
$generated = array_rand($digits, 9);

$blueprint = \Mockery::mock(Blueprint::class);
$blueprint->expects('parse')
->with($draft)
->andReturn($tokens);
$blueprint->expects('analyze')
->with($tokens)
->andReturn($registry);
$blueprint->expects('generate')
->with($registry)
->andReturn($generated);

$actual = Builder::execute($blueprint, $draft);

$this->assertSame($generated, $actual);
}
}

0 comments on commit 899bbaa

Please sign in to comment.