diff --git a/src/Api/Api.php b/src/Api/Api.php index c728b77a..5d33d71b 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -140,6 +140,11 @@ class Api */ private $transactions; + /** + * @var string|null + */ + private $modelNamespace; + /** * Api constructor. * @@ -156,6 +161,7 @@ class Api * @param array $providers * @param string|null $connection * @param bool $transactions + * @param string|null $modelNamespace */ public function __construct( Factory $factory, @@ -170,7 +176,8 @@ public function __construct( array $errors = [], array $providers = [], string $connection = null, - bool $transactions = true + bool $transactions = true, + string $modelNamespace = null ) { $this->factory = $factory; $this->resolver = $resolver; @@ -185,6 +192,7 @@ public function __construct( $this->providers = $providers; $this->connection = $connection; $this->transactions = $transactions; + $this->modelNamespace = $modelNamespace; } /** @@ -384,6 +392,16 @@ public function hasTransactions(): bool return $this->transactions; } + /** + * @return string|null + */ + public function getModelNamespace(): ?string + { + return $this->modelNamespace; + } + + + /** * Create an encoder for the API. * diff --git a/src/Api/Repository.php b/src/Api/Repository.php index 30f6e930..70738cac 100644 --- a/src/Api/Repository.php +++ b/src/Api/Repository.php @@ -90,7 +90,8 @@ public function createApi($apiName, $host = null) $config['errors'], $config['providers'] ?? [], $config['controllers']['connection'] ?? null, - $config['controllers']['transactions'] ?? true + $config['controllers']['transactions'] ?? true, + $config['model-namespace'] ?? null ); /** Attach resource providers to the API. */ diff --git a/src/Console/Commands/AbstractGeneratorCommand.php b/src/Console/Commands/AbstractGeneratorCommand.php index 9e54a1d9..5d600304 100644 --- a/src/Console/Commands/AbstractGeneratorCommand.php +++ b/src/Console/Commands/AbstractGeneratorCommand.php @@ -141,7 +141,8 @@ protected function buildClass($name) ->replaceClassName($stub, $name) ->replaceResourceType($stub) ->replaceApplicationNamespace($stub) - ->replaceRecord($stub); + ->replaceRecord($stub) + ->replaceModelNamespace($stub); return $stub; } @@ -273,6 +274,20 @@ protected function replaceClassName(&$stub, $name) return $this; } + /** + * Replace the model namespace name. + * + * @param $stub + * @return $this + */ + private function replaceModelNamespace(&$stub) { + + $modelNamespace = $this->getApi()->getModelNamespace() ?? rtrim($this->laravel->getNamespace(), "\\"); + $stub = str_replace('DummyModelNamespace', $modelNamespace, $stub); + + return $this; + } + /** * Get the stub for specific generator type * diff --git a/stubs/api.php b/stubs/api.php index 06632126..51ae6e0a 100644 --- a/stubs/api.php +++ b/stubs/api.php @@ -39,6 +39,19 @@ 'namespace' => null, 'by-resource' => true, + /* + |-------------------------------------------------------------------------- + | Model Namespace + |-------------------------------------------------------------------------- + | + | Here you can decide where your api models live. + | By default (i.e. set to null), the package assumes they will live on + | your root namespace + | - e.g. namespace: 'App', Models: 'App\Foo', 'App\Bar' + | + */ + 'model-namespace' => null, + /* |-------------------------------------------------------------------------- | Resources diff --git a/stubs/eloquent/adapter.stub b/stubs/eloquent/adapter.stub index 90f62843..fafd9fbe 100644 --- a/stubs/eloquent/adapter.stub +++ b/stubs/eloquent/adapter.stub @@ -31,7 +31,7 @@ class DummyClass extends AbstractAdapter */ public function __construct(StandardStrategy $paging) { - parent::__construct(new \DummyApplicationNamespace\DummyRecord(), $paging); + parent::__construct(new \DummyModelNamespace\DummyRecord(), $paging); } /** diff --git a/stubs/eloquent/schema.stub b/stubs/eloquent/schema.stub index 3ecd8cff..0af95747 100644 --- a/stubs/eloquent/schema.stub +++ b/stubs/eloquent/schema.stub @@ -13,7 +13,7 @@ class DummyClass extends SchemaProvider protected $resourceType = 'dummyResourceType'; /** - * @param $resource + * @param \DummyModelNamespace\DummyRecord $resource * the domain record being serialized. * @return string */ @@ -23,7 +23,7 @@ class DummyClass extends SchemaProvider } /** - * @param $resource + * @param \DummyModelNamespace\DummyRecord $resource * the domain record being serialized. * @return array */ diff --git a/tests/dummy/config/json-api-v1.php b/tests/dummy/config/json-api-v1.php index 4c2ec46e..7848f4b1 100644 --- a/tests/dummy/config/json-api-v1.php +++ b/tests/dummy/config/json-api-v1.php @@ -39,6 +39,19 @@ 'namespace' => 'DummyApp\JsonApi', 'by-resource' => true, + /* + |-------------------------------------------------------------------------- + | Model Namespace + |-------------------------------------------------------------------------- + | + | Here you can decide where your api models live. + | + | By default Models live on the root of the application like App\Post hence the model-namespace: 'App' + | but you can set it any other place like App\Models\Post hence the model-namespace: 'App\Models' + | + */ + 'model-namespace' => null, + /* |-------------------------------------------------------------------------- | Resources diff --git a/tests/lib/Integration/GeneratorsTest.php b/tests/lib/Integration/GeneratorsTest.php index 2cf5907f..1c4be2ed 100644 --- a/tests/lib/Integration/GeneratorsTest.php +++ b/tests/lib/Integration/GeneratorsTest.php @@ -475,6 +475,7 @@ private function assertEloquentSchema() { $content = $this->assertSchema(); $this->assertContentContains('return (string) $resource->getRouteKey();', $content); + $this->assertContentContains(' @param \DummyApp\Company $resource', $content); } /**