Skip to content

Commit 69731ce

Browse files
authored
Update ApiMakeCommand.php
1 parent 32b51e8 commit 69731ce

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/Generator/ApiMakeCommand.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ class ApiMakeCommand extends Command
2727
protected $signature = 'make:api
2828
{name : The name of the model}
2929
{--M|model : create the model}
30-
{--R|resource : create a resource}
30+
{--C|resource : create a resource}
3131
{--P|policy : create a policy}
32+
{--R|request : create a request}
3233
{--A|all : create all requirements}';
3334

3435
/**
@@ -94,15 +95,20 @@ protected function createOptionals()
9495
}
9596

9697
if ($this->option('all') || $this->option('policy')) {
97-
$this->call('make:policy', ['name' => $this->stubVariables['model']['name'].'Policy', '--model' => $this->stubVariables['model']['fullName']]);
98+
$this->call('make:policy', ['name' => $this->stubVariables['model']['name'].'Policy', '--model' => $this->stubVariables['model']['name']]);
9899
}
99100

100101
if ($this->option('all') || $this->option('resource')) {
101-
$this->call('make:resource', ['name' => $this->stubVariables['model']['name']]);
102-
$this->call('make:resource', ['name' => $this->stubVariables['model']['name'].'Collection']);
102+
$this->call('make:apiresource', ['name' => $this->stubVariables['model']['name']. 'Resource']);
103+
$this->call('make:apiresource', ['name' => $this->stubVariables['model']['name'].'Collection']);
104+
}
105+
106+
if ($this->option('all') || $this->option('request')) {
107+
$this->call('make:request', ['name' => $this->stubVariables['model']['name']. 'Request']);
103108
}
104109
}
105110

111+
106112
/**
107113
* Prepare names, paths and namespaces for stubs.
108114
*
@@ -190,11 +196,16 @@ protected function setDataForEntity($entity)
190196
$this->stubVariables[$entity]['fullNameWithoutRoot'] = $this->stubVariables[$entity]['namespaceWithoutRoot'].'\\'.$this->stubVariables[$entity]['name'];
191197
$this->stubVariables[$entity]['fullName'] = $this->stubVariables[$entity]['namespace'].'\\'.$this->stubVariables[$entity]['name'];
192198

193-
$this->stubVariables[$entity]['resource'] = ($this->option('all') || $this->option('resource')) ? '\\App\\Http\Resources\\'.$this->stubVariables[$entity]['name'] : '\Phpsa\LaravelApiController\Http\Resources\ApiResource';
194-
$this->stubVariables[$entity]['collection'] = ($this->option('all') || $this->option('resource')) ? '\\App\\Http\Resources\\'.$this->stubVariables[$entity]['name'].'Collection' : '\Phpsa\LaravelApiController\Http\Resources\ApiCollection';
199+
$resourceName = $this->stubVariables['model']['name'].'Resource';
200+
$resourceCollection = $this->stubVariables['model']['name'].'Collection';
201+
202+
$requestName = $this->stubVariables['model']['name'].'Request';
203+
204+
$this->stubVariables[$entity]['request'] = ($this->option('all') || $this->option('request')) ? $requestName : 'Request';
205+
$this->stubVariables[$entity]['useRequest'] = ($this->option('all') || $this->option('request')) ? 'use \\App\\Http\\Requests\\' . $requestName . ';' : null;
195206

196-
$resourceName = $this->stubVariables['model']['fullName'];
197-
$resourceCollection = $resourceName.'Collection';
207+
$this->stubVariables[$entity]['resource'] = ($this->option('all') || $this->option('resource')) ? '\\App\\Http\Resources\\'.$resourceName : '\Phpsa\LaravelApiController\Http\Resources\ApiResource';
208+
$this->stubVariables[$entity]['collection'] = ($this->option('all') || $this->option('resource')) ? '\\App\\Http\Resources\\'.$resourceCollection : '\Phpsa\LaravelApiController\Http\Resources\ApiCollection';
198209

199210
return $this;
200211
}

0 commit comments

Comments
 (0)