|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Nwidart\Modules\Support\Stub; |
|
7
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
|
|
11
|
|
|
class ResourceMakeCommand extends GeneratorCommand |
|
12
|
|
|
{ |
|
13
|
|
|
use ModuleCommandTrait; |
|
14
|
|
|
|
|
15
|
|
|
protected $argumentName = 'name'; |
|
16
|
|
|
protected $name = 'module:make-resource'; |
|
17
|
|
|
protected $description = 'Create a new resource class for the specified module.'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get default namespace. |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function getDefaultNamespace() |
|
25
|
|
|
{ |
|
26
|
3 |
|
return 'Transformers'; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the console command arguments. |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
65 |
|
protected function getArguments() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
65 |
|
['name', InputArgument::REQUIRED, 'The name of the resource class.'], |
|
38
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
65 |
|
protected function getOptions() |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
65 |
|
['collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection.'], |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
3 |
|
protected function getTemplateContents() |
|
53
|
|
|
{ |
|
54
|
3 |
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
|
55
|
|
|
|
|
56
|
3 |
|
return (new Stub($this->getStubName(), [ |
|
57
|
3 |
|
'NAMESPACE' => $this->getClassNamespace($module), |
|
58
|
3 |
|
'CLASS' => $this->getClass(), |
|
59
|
3 |
|
]))->render(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
3 |
|
protected function getDestinationFilePath() |
|
66
|
|
|
{ |
|
67
|
3 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
|
68
|
|
|
|
|
69
|
3 |
|
$seederPath = $this->laravel['modules']->config('paths.generator.resource'); |
|
70
|
|
|
|
|
71
|
3 |
|
return $path . $seederPath . '/' . $this->getFileName() . '.php'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
3 |
|
private function getFileName() |
|
78
|
|
|
{ |
|
79
|
3 |
|
return Str::studly($this->argument('name')); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Determine if the command is generating a resource collection. |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
3 |
|
protected function collection() : bool |
|
88
|
|
|
{ |
|
89
|
3 |
|
return $this->option('collection') || |
|
90
|
3 |
|
Str::endsWith($this->argument('name'), 'Collection'); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
3 |
|
protected function getStubName(): string |
|
97
|
|
|
{ |
|
98
|
3 |
|
if ($this->collection()) { |
|
99
|
1 |
|
return '/resource-collection.stub'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
return '/resource.stub'; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.