Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions src/Generators/Statements/ResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Blueprint\Contracts\Generator;
use Blueprint\Models\Controller;
use Blueprint\Models\Model;
use Blueprint\Models\Statements\ResourceStatement;
use Illuminate\Support\Str;

class ResourceGenerator implements Generator
{
const INDENT = ' ';
/**
* @var \Illuminate\Contracts\Filesystem\Filesystem
*/
Expand All @@ -26,11 +29,13 @@ public function output(array $tree): array

$stub = $this->files->stub('resource.stub');

$this->registerModels($tree);

/** @var \Blueprint\Models\Controller $controller */
foreach ($tree['controllers'] as $controller) {
foreach ($controller->methods() as $method => $statements) {
foreach ($statements as $statement) {
if (!$statement instanceof ResourceStatement) {
if (! $statement instanceof ResourceStatement) {
continue;
}

Expand All @@ -40,7 +45,7 @@ public function output(array $tree): array
continue;
}

if (!$this->files->exists(dirname($path))) {
if (! $this->files->exists(dirname($path))) {
$this->files->makeDirectory(dirname($path), 0755, true);
}

Expand All @@ -56,12 +61,12 @@ public function output(array $tree): array

protected function getPath(string $name)
{
return config('blueprint.app_path') . '/Http/Resources/' . $name . '.php';
return config('blueprint.app_path').'/Http/Resources/'.$name.'.php';
}

protected function populateStub(string $stub, ResourceStatement $resource)
{
$stub = str_replace('DummyNamespace', config('blueprint.namespace') . '\\Http\\Resources', $stub);
$stub = str_replace('DummyNamespace', config('blueprint.namespace').'\\Http\\Resources', $stub);
$stub = str_replace('DummyImport', $resource->collection() ? 'Illuminate\\Http\\Resources\\Json\\ResourceCollection' : 'Illuminate\\Http\\Resources\\Json\\JsonResource', $stub);
$stub = str_replace('DummyParent', $resource->collection() ? 'ResourceCollection' : 'JsonResource', $stub);
$stub = str_replace('DummyClass', $resource->name(), $stub);
Expand All @@ -74,14 +79,54 @@ protected function populateStub(string $stub, ResourceStatement $resource)

private function buildData(ResourceStatement $resource)
{
$context = Str::singular($resource->reference());

/** @var \Blueprint\Models\Model $model */
$model = $this->modelForContext($context);

$data = [];
if ($resource->collection()) {
return 'return [
\'data\' => $this->collection,
];';
$data[] = 'return [';
$data[] = self::INDENT.'\'data\' => $this->collection,';
$data[] = ' ];';

return implode(PHP_EOL, $data);
}

$data[] = 'return [';
foreach ($this->visibleColumns($model) as $column) {
$data[] = self::INDENT.'\''.$column.'\' => $this->'.$column.',';
}
$data[] = ' ];';

return implode(PHP_EOL, $data);
}

return 'return [
\'id\' => $this->id,
];';
private function visibleColumns(Model $model)
{
return array_diff(array_keys($model->columns()), [
'password',
'remember_token',
]);
}

private function modelForContext(string $context)
{
if (isset($this->models[Str::studly($context)])) {
return $this->models[Str::studly($context)];
}

$matches = array_filter(array_keys($this->models), function ($key) use ($context) {
return Str::endsWith($key, '/'.Str::studly($context));
});

if (count($matches) === 1) {
return $this->models[$matches[0]];
}
}

private function registerModels(array $tree)
{
$this->models = array_merge($tree['cache'] ?? [], $tree['models'] ?? []);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function setUp(): void
$this->subject = new ResourceGenerator($this->files);

$this->blueprint = new Blueprint();
$this->blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
$this->blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new StatementLexer()));
$this->blueprint->registerGenerator($this->subject);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/definitions/resource-statements.bp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
models:
User:
name: string
email: string
password: string
remember_token: remembertoken

controllers:
User:
index:
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/resources/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
];
}
}