diff --git a/src/NovaGenerator.php b/src/NovaGenerator.php index 8efd19a..baa3c49 100644 --- a/src/NovaGenerator.php +++ b/src/NovaGenerator.php @@ -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) { @@ -60,7 +60,6 @@ protected function getPath(Model $model): string protected function populateStub(string $stub, Model $model): string { $data = [ - 'model' => $model, 'fields' => '', 'imports' => [], ]; @@ -68,11 +67,11 @@ protected function populateStub(string $stub, Model $model): string $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(); diff --git a/src/Tasks/AddIdentifierField.php b/src/Tasks/AddIdentifierField.php index 24ef765..bc22051 100644 --- a/src/Tasks/AddIdentifierField.php +++ b/src/Tasks/AddIdentifierField.php @@ -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() . "'"; diff --git a/src/Tasks/AddRegularFields.php b/src/Tasks/AddRegularFields.php index 56ec7db..7894e19 100644 --- a/src/Tasks/AddRegularFields.php +++ b/src/Tasks/AddRegularFields.php @@ -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()'; @@ -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'); diff --git a/src/Tasks/AddRelationshipFields.php b/src/Tasks/AddRelationshipFields.php index 3067de7..8d66ffe 100644 --- a/src/Tasks/AddRelationshipFields.php +++ b/src/Tasks/AddRelationshipFields.php @@ -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); diff --git a/src/Tasks/AddTimestampFields.php b/src/Tasks/AddTimestampFields.php index 59ff450..c0762ad 100644 --- a/src/Tasks/AddTimestampFields.php +++ b/src/Tasks/AddTimestampFields.php @@ -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'),"; } diff --git a/tests/NovaGeneratorTest.php b/tests/NovaGeneratorTest.php index a5c8af4..aec1d27 100644 --- a/tests/NovaGeneratorTest.php +++ b/tests/NovaGeneratorTest.php @@ -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'); @@ -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') @@ -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') @@ -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')