Skip to content

Commit 68004f9

Browse files
committed
[Feature] Add non-Eloquent option to schema generator command
1 parent c4d174f commit 68004f9

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ All notable changes to this project will be documented in this file. This projec
2020
still be used, the new `RelatedResponse` class merges relationship meta into the top-level `meta` member of the
2121
response document. For *to-many* relationships that are countable, this will mean the top-level `meta` member will
2222
contain the count of the relationship.
23+
- The schema generator Artisan command now has a `--non-eloquent` option to generate a schema for a non-Eloquent
24+
resource.
2325

2426
### Changed
2527

src/Console/MakeSchema.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class MakeSchema extends GeneratorCommand
5353
*/
5454
protected function getStub()
5555
{
56+
if ($this->option('non-eloquent')) {
57+
return $this->resolveStubPath('non-eloquent-schema.stub');
58+
}
59+
5660
return $this->resolveStubPath('schema.stub');
5761
}
5862

@@ -108,6 +112,7 @@ protected function getOptions()
108112
return [
109113
['force', null, InputOption::VALUE_NONE, 'Create the class even if the schema already exists'],
110114
['model', 'm', InputOption::VALUE_REQUIRED, 'The model that the schema applies to.'],
115+
['non-eloquent', null, InputOption::VALUE_NONE, 'Create a schema for a non-Eloquent resource.'],
111116
['proxy', 'p', InputOption::VALUE_NONE, 'Create a schema for an Eloquent model proxy.'],
112117
['server', 's', InputOption::VALUE_REQUIRED, 'The JSON:API server the schema exists in.'],
113118
];

stubs/non-eloquent-schema.stub

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use {{ namespacedModel }};
6+
use LaravelJsonApi\Core\Schema\Schema;
7+
use LaravelJsonApi\NonEloquent\Fields\Attribute;
8+
use LaravelJsonApi\NonEloquent\Fields\ID;
9+
use LaravelJsonApi\NonEloquent\Filters\Filter;
10+
11+
class {{ class }} extends Schema
12+
{
13+
14+
/**
15+
* The model the schema corresponds to.
16+
*
17+
* @var string
18+
*/
19+
public static string $model = {{ model }}::class;
20+
21+
/**
22+
* Get the resource fields.
23+
*
24+
* @return array
25+
*/
26+
public function fields(): array
27+
{
28+
return [
29+
ID::make(),
30+
// Attribute::make('name'),
31+
];
32+
}
33+
34+
/**
35+
* Get the resource filters.
36+
*
37+
* @return array
38+
*/
39+
public function filters(): array
40+
{
41+
return [
42+
// Filter::make('id'),
43+
];
44+
}
45+
46+
}

tests/lib/Integration/Console/MakeSchemaTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ public function testProxy(): void
116116
$this->assertProxySchemaCreated('App\JsonApi\Proxies\UserAccount', 'UserAccount');
117117
}
118118

119+
public function testNonEloquent(): void
120+
{
121+
config()->set('jsonapi.servers', [
122+
'v1' => Server::class,
123+
]);
124+
125+
$result = $this->artisan('jsonapi:schema', [
126+
'name' => 'sites',
127+
'--model' => '\App\Entities\Site',
128+
'--non-eloquent' => true,
129+
]);
130+
131+
$this->assertSame(0, $result);
132+
$this->assertNonEloquentSchemaCreated('App\Entities\Site', 'Site');
133+
}
134+
119135
public function testNoServer(): void
120136
{
121137
config()->set('jsonapi.servers', [
@@ -167,6 +183,15 @@ private function assertSchemaCreated(string $namespacedModel, string $model): vo
167183
foreach ($tests as $expected) {
168184
$this->assertStringContainsString($expected, $content);
169185
}
186+
187+
$missing = [
188+
'LaravelJsonApi\Eloquent\ProxySchema',
189+
'LaravelJsonApi\Core\Schema\Schema',
190+
];
191+
192+
foreach ($missing as $notExpected) {
193+
$this->assertStringNotContainsString($notExpected, $content);
194+
}
170195
}
171196

172197
/**
@@ -190,6 +215,46 @@ private function assertProxySchemaCreated(string $namespacedModel, string $model
190215
foreach ($tests as $expected) {
191216
$this->assertStringContainsString($expected, $content);
192217
}
218+
219+
$missing = [
220+
'LaravelJsonApi\Eloquent\Schema',
221+
'LaravelJsonApi\Core\Schema\Schema',
222+
];
223+
224+
foreach ($missing as $notExpected) {
225+
$this->assertStringNotContainsString($notExpected, $content);
226+
}
227+
}
228+
229+
/**
230+
* @param string $namespacedModel
231+
* @param string $model
232+
* @return void
233+
*/
234+
private function assertNonEloquentSchemaCreated(string $namespacedModel, string $model): void
235+
{
236+
$this->assertFileExists($path = app_path('JsonApi/V1/Sites/SiteSchema.php'));
237+
$content = file_get_contents($path);
238+
239+
$tests = [
240+
'namespace App\JsonApi\V1\Sites;',
241+
'use LaravelJsonApi\Core\Schema\Schema;',
242+
'class SiteSchema extends Schema',
243+
"use {$namespacedModel};",
244+
"public static string \$model = {$model}::class;",
245+
];
246+
247+
foreach ($tests as $expected) {
248+
$this->assertStringContainsString($expected, $content);
249+
}
250+
251+
$missing = [
252+
'LaravelJsonApi\Eloquent',
253+
];
254+
255+
foreach ($missing as $notExpected) {
256+
$this->assertStringNotContainsString($notExpected, $content);
257+
}
193258
}
194259

195260
/**

0 commit comments

Comments
 (0)