Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Apr 17, 2020
1 parent 9df4823 commit 3c35de0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 32 deletions.
13 changes: 6 additions & 7 deletions src/NovaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function output(array $tree): array
{
$output = [];

$stub = $this->files->stub('class.stub', $this->stubPath());
$stub = $this->files->get($this->stubPath() . DIRECTORY_SEPARATOR . 'class.stub');

/** @var \Blueprint\Models\Model $model */
foreach ($tree['models'] as $model) {
Expand Down Expand Up @@ -60,19 +60,18 @@ protected function getPath(Model $model): string
protected function populateStub(string $stub, Model $model): string
{
$data = [
'model' => $model,
'fields' => '',
'imports' => [],
];

$data = resolve(Pipeline::class)
->send($data)
->through([
AddIdentifierField::class,
AddRegularFields::class,
AddRelationshipFields::class,
AddTimestampFields::class,
RemapImports::class,
new AddIdentifierField($model),
new AddRegularFields($model),
new AddRelationshipFields($model),
new AddTimestampFields($model),
new RemapImports(),
])
->thenReturn();

Expand Down
15 changes: 10 additions & 5 deletions src/Tasks/AddIdentifierField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ class AddIdentifierField
{
const INDENT = ' ';

public function handle($data, Closure $next): array
/** @var Model */
private $model;

public function __construct(Model $model)
{
/** @var Model */
$model = $data['model'];
$this->model = $model;
}

public function handle($data, Closure $next): array
{
$column = $this->getIdentifierColumn(
$model->columns(),
$model->relationships()
$this->model->columns(),
$this->model->relationships()
);

$identifierName = $column->name() === 'id' ? '' : "'" . $column->name() . "'";
Expand Down
18 changes: 12 additions & 6 deletions src/Tasks/AddRegularFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@ class AddRegularFields
const INDENT = ' ';
const INDENT_PLUS = ' ';

/** @var Model */
private $model;

public function __construct(Model $model)
{
$this->model = $model;
}

public function handle($data, Closure $next): array
{
/** @var Model */
$model = $data['model'];
$fields = $data['fields'];
$imports = $data['imports'];

$columns = $this->regularColumns($model->columns());
$columns = $this->regularColumns($this->model->columns());
foreach ($columns as $column) {
$fieldType = $this->fieldType($column->dataType());
$imports[] = $fieldType;

$field = $fieldType . "::make('" . $this->fieldLabel($column->name()) . "')";
$field .= $this->addRules($column, $model);
$field .= $this->addRules($column);

if ($column->dataType() === 'json') {
$field .= PHP_EOL . self::INDENT_PLUS . '->json()';
Expand Down Expand Up @@ -55,12 +61,12 @@ private function fieldLabel($name): string
return str_replace('_', ' ', ucfirst($name));
}

private function addRules(Column $column, Model $model): string
private function addRules(Column $column): string
{
$fieldRules = '';

if (!in_array($column->dataType(), ['id'])) {
$rules = Rules::fromColumn($model->tableName(), $column);
$rules = Rules::fromColumn($this->model->tableName(), $column);

if ($column->dataType() === 'json') {
array_push($rules, 'json');
Expand Down
11 changes: 9 additions & 2 deletions src/Tasks/AddRelationshipFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ class AddRelationshipFields
{
const INDENT = ' ';

/** @var Model */
private $model;

public function __construct(Model $model)
{
$this->model = $model;
}

public function handle(array $data, Closure $next)
{
/** @var Model */
$relationships = $data['model']->relationships();
$fields = $data['fields'];
$imports = $data['imports'];
$relationships = $this->model->relationships();

ksort($relationships);

Expand Down
14 changes: 10 additions & 4 deletions src/Tasks/AddTimestampFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ class AddTimestampFields
{
const INDENT = ' ';

/** @var Model */
private $model;

public function __construct(Model $model)
{
$this->model = $model;
}

public function handle($data, Closure $next): array
{
/** @var Model */
$model = $data['model'];
$fields = $data['fields'];
$imports = $data['imports'];

if ($model->usesTimestamps()) {
if ($this->model->usesTimestamps()) {
$imports[] = 'DateTime';

$fields .= self::INDENT . "DateTime::make('Created at')," . PHP_EOL . self::INDENT . "DateTime::make('Updated at'),";
}

if ($model->usesSoftDeletes()) {
if ($this->model->usesSoftDeletes()) {
$imports[] = 'DateTime';
$fields .= PHP_EOL . self::INDENT . "DateTime::make('Deleted at'),";
}
Expand Down
16 changes: 8 additions & 8 deletions tests/NovaGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected function setUp(): void
*/
public function output_generates_nothing_for_empty_tree()
{
$this->files->expects('stub')
->withArgs(['class.stub', $this->stubPath()])
$this->files->expects('get')
->with($this->stubPath() . DIRECTORY_SEPARATOR . 'class.stub')
->andReturn(file_get_contents('stubs/class.stub'));

$this->files->shouldNotHaveReceived('put');
Expand All @@ -49,8 +49,8 @@ public function output_generates_nothing_for_empty_tree()
*/
public function output_generates_nova_resources($definition, $path, $novaResource)
{
$this->files->expects('stub')
->withArgs(['class.stub', $this->stubPath()])
$this->files->expects('get')
->with($this->stubPath() . DIRECTORY_SEPARATOR . 'class.stub')
->andReturn(file_get_contents('stubs/class.stub'));

$this->files->expects('exists')
Expand All @@ -70,8 +70,8 @@ public function output_generates_nova_resources($definition, $path, $novaResourc
*/
public function output_generates_relationships()
{
$this->files->expects('stub')
->withArgs(['class.stub', $this->stubPath()])
$this->files->expects('get')
->with($this->stubPath() . DIRECTORY_SEPARATOR . 'class.stub')
->andReturn(file_get_contents('stubs/class.stub'));

$this->files->expects('exists')
Expand All @@ -95,8 +95,8 @@ public function output_respects_configuration()
$this->app['config']->set('blueprint.namespace', 'Some\\App');
$this->app['config']->set('blueprint.models_namespace', 'Models');

$this->files->expects('stub')
->withArgs(['class.stub', $this->stubPath()])
$this->files->expects('get')
->with($this->stubPath() . DIRECTORY_SEPARATOR . 'class.stub')
->andReturn(file_get_contents('stubs/class.stub'));

$this->files->expects('exists')
Expand Down

0 comments on commit 3c35de0

Please sign in to comment.