8000 adding the models location namespace to the config file by Mina-R-Meshriky · Pull Request #479 · cloudcreativity/laravel-json-api · GitHub
[go: up one dir, main page]

Skip to content

adding the models location namespace to the config file #479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2020
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
20 changes: 19 additions & 1 deletion src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ class Api
*/
private $transactions;

/**
* @var string|null
*/
private $modelNamespace;

/**
* Api constructor.
*
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -185,6 +192,7 @@ public function __construct(
$this->providers = $providers;
$this->connection = $connection;
$this->transactions = $transactions;
$this->modelNamespace = $modelNamespace;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Api/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
17 changes: 16 additions & 1 deletion src/Console/Commands/AbstractGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ protected function buildClass($name)
->replaceClassName($stub, $name)
->replaceResourceType($stub)
->replaceApplicationNamespace($stub)
->replaceRecord($stub);
->replaceRecord($stub)
->replaceModelNamespace($stub);

return $stub;
}
Expand Down Expand Up @@ -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
*
Expand Down
13 changes: 13 additions & 0 deletions stubs/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stubs/eloquent/adapter.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions stubs/eloquent/schema.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DummyClass extends SchemaProvider
protected $resourceType = 'dummyResourceType';

/**
* @param $resource
* @param \DummyModelNamespace\DummyRecord $resource
* the domain record being serialized.
* @return string
*/
Expand All @@ -23,7 +23,7 @@ class DummyClass extends SchemaProvider
}

/**
* @param $resource
* @param \DummyModelNamespace\DummyRecord $resource
* the domain record being serialized.
* @return array
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/dummy/config/json-api-v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/lib/Integration/GeneratorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ private function assertEloquentSchema()
{
$content = $this->assertSchema();
$this->assertContentContains('return (string) $resource->getRouteKey();', $content);
$this->assertContentContains(' @param \DummyApp\Company $resource', $content);
}

/**
Expand Down
0