1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
7
|
|
|
use Nwidart\Modules\Support\Stub; |
8
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
|
11
|
|
|
class ComponentClassMakeCommand extends GeneratorCommand |
12
|
|
|
{ |
13
|
|
|
use ModuleCommandTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name of argument name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $argumentName = 'name'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command name. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $name = 'module:make-component'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Create a new component-class for the specified module.'; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function handle() : int |
39
|
|
|
{ |
40
|
|
|
if (parent::handle() === E_ERROR) { |
41
|
|
|
return E_ERROR; |
42
|
|
|
} |
43
|
|
|
$this->writeComponentViewTemplate(); |
44
|
|
|
return 0; |
45
|
|
|
} |
46
|
|
|
/** |
47
|
|
|
* Write the view template for the component. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected function writeComponentViewTemplate() |
52
|
|
|
{ |
53
|
|
|
$this->call('module:make-component-view', ['name' => $this->argument('name') , 'module' => $this->argument('module')]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
public function getDefaultNamespace() : string |
59
|
|
|
{ |
60
|
|
|
$module = $this->laravel['modules']; |
61
|
|
|
return $module->config('paths.generator.component-class.namespace') ?: $module->config('paths.generator.component-class.path', 'View/Component'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the console command arguments. |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected function getArguments() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
['name', InputArgument::REQUIRED, 'The name of the component.'], |
73
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
/** |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
protected function getTemplateContents() |
80
|
|
|
{ |
81
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
82
|
|
|
|
83
|
|
|
return (new Stub('/component-class.stub', [ |
84
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
85
|
|
|
'CLASS' => $this->getClass(), |
86
|
|
|
'LOWER_NAME' => $module->getLowerName(), |
87
|
|
|
'COMPONENT_NAME' => 'components.'.Str::lower($this->argument('name')) |
|
|
|
|
88
|
|
|
]))->render(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
protected function getDestinationFilePath() |
95
|
|
|
{ |
96
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
97
|
|
|
$factoryPath = GenerateConfigReader::read('component-class'); |
98
|
|
|
return $path . $factoryPath->getPath() . '/' . $this->getFileName(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function getFileName() |
105
|
|
|
{ |
106
|
|
|
return Str::studly($this->argument('name')) . '.php'; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
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.