8000 [Feature] Allow the model namespace to be customised in config (#479) · CodingSeo/laravel-json-api@67330eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 67330eb

Browse files
[Feature] Allow the model namespace to be customised in config (cloudcreativity#479)
Adds the model namespace to the config file so that generator uses the correct namespace.
1 parent 04a0029 commit 67330eb

File tree

8 files changed

+67
-6
lines changed

8 files changed

+67
-6
lines changed

src/Api/Api.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ class Api
140140
*/
141141
private $transactions;
142142

143+
/**
144+
* @var string|null
145+
*/
146+
private $modelNamespace;
147+
143148
/**
144149
* Api constructor.
145150
*
@@ -156,6 +161,7 @@ class Api
156161
* @param array $providers
157162
* @param string|null $connection
158163
* @param bool $transactions
164+
* @param string|null $modelNamespace
159165
*/
160166
public function __construct(
161167
Factory $factory,
@@ -170,7 +176,8 @@ public function __construct(
170176
array $errors = [],
171177
array $providers = [],
172178
string $connection = null,
173-
bool $transactions = true
179+
bool $transactions = true,
180+
string $modelNamespace = null
174181
) {
175182
$this->factory = $factory;
176183
$this->resolver = $resolver;
@@ -185,6 +192,7 @@ public function __construct(
185192
$this->providers = $providers;
186193
$this->connection = $connection;
187194
$this->transactions = $transactions;
195+
$this->modelNamespace = $modelNamespace;
188196
}
189197

190198
/**
@@ -384,6 +392,16 @@ public function hasTransactions(): bool
384392
return $this->transactions;
385393
}
386394

395+
/**
396+
* @return string|null
397+
*/
398+
public function getModelNamespace(): ?string
399+
{
400+
return $this->modelNamespace;
401+
}
402+
403+
404+
387405
/**
388406
* Create an encoder for the API.
389407
*

src/Api/Repository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public function createApi($apiName, $host = null)
9090
$config['errors'],
9191
$config['providers'] ?? [],
9292
$config['controllers']['connection'] ?? null,
93-
$config['controllers']['transactions'] ?? true
93+
$config['controllers']['transactions'] ?? true,
94+
$config['model-namespace'] ?? null
9495
);
9596

9697
/** Attach resource providers to the API. */

src/Console/Commands/AbstractGeneratorCommand.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ protected function buildClass($name)
141141
->replaceClassName($stub, $name)
142142
->replaceResourceType($stub)
143143
->replaceApplicationNamespace($stub)
144-
->replaceRecord($stub);
144+
->replaceRecord($stub)
145+
->replaceModelNamespace($stub);
145146

146147
return $stub;
147148
}
@@ -273,6 +274,20 @@ protected function replaceClassName(&$stub, $name)
273274
return $this;
274275
}
275276

277+
/**
278+
* Replace the model namespace name.
279+
*
280+
* @param $stub
281+
* @return $this
282+
*/
283+
private function replaceModelNamespace(&$stub) {
284+
285+
$modelNamespace = $this->getApi()->getModelNamespace() ?? rtrim($this->laravel->getNamespace(), "\\");
286+
$stub = str_replace('DummyModelNamespace', $modelNamespace, $stub);
287+
288+
return $this;
289+
}
290+
276291
/**
277292
* Get the stub for specific generator type
278293
*

stubs/api.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@
3939
'namespace' => null,
4040
'by-resource' => true,
4141

42+
/*
43+
|--------------------------------------------------------------------------
44+
| Model Namespace
45+
|--------------------------------------------------------------------------
46+
|
47+
| Here you can decide where your api models live.
48+
| By default (i.e. set to null), the package assumes they will live on
49+
| your root namespace
50+
| - e.g. namespace: 'App', Models: 'App\Foo', 'App\Bar'
51+
|
52+
*/
53+
'model-namespace' => null,
54+
4255
/*
4356
|--------------------------------------------------------------------------
4457
| Resources

stubs/eloquent/adapter.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class DummyClass extends AbstractAdapter
3131
*/
3232
public function __construct(StandardStrategy $paging)
3333
{
34-
parent::__construct(new \DummyApplicationNamespace\DummyRecord(), $paging);
34+
parent::__construct(new \DummyModelNamespace\DummyRecord(), $paging);
3535
}
3636

3737
/**

stubs/eloquent/schema.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class DummyClass extends SchemaProvider
1313
protected $resourceType = 'dummyResourceType';
1414

1515
/**
16-
* @param $resource
16+
* @param \DummyModelNamespace\DummyRecord $resource
1717
* the domain record being serialized.
1818
* @return string
1919
*/
@@ -23,7 +23,7 @@ class DummyClass extends SchemaProvider
2323
}
2424

2525
/**
26-
* @param $resource
26+
* @param \DummyModelNamespace\DummyRecord $resource
2727
* the domain record being serialized.
2828
* @return array
2929
*/

tests/dummy/config/json-api-v1.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@
3939
'namespace' => 'DummyApp\JsonApi',
4040
'by-resource' => true,
4141

42+
/*
43+
|--------------------------------------------------------------------------
44+
| Model Namespace
45+
|--------------------------------------------------------------------------
46+
|
47+
| Here you can decide where your api models live.
48+
|
49+
| By default Models live on the root of the application like App\Post hence the model-namespace: 'App'
50+
| but you can set it any other place like App\Models\Post hence the model-namespace: 'App\Models'
51+
|
52+
*/
53+
'model-namespace' => null,
54+
4255
/*
4356
|--------------------------------------------------------------------------
4457
| Resources

tests/lib/Integration/GeneratorsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ private function assertEloquentSchema()
475475
{
476476
$content = $this->assertSchema();
477477
$this->assertContentContains('return (string) $resource->getRouteKey();', $content);
478+
$this->assertContentContains(' @param \DummyApp\Company $resource', $content);
478479
}
479480

480481
/**

0 commit comments

Comments
 (0)
0