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
|
|
View Code Duplication |
class PolicyMakeCommand 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-policy'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Create a new policy class for the specified module.'; |
35
|
|
|
|
36
|
|
|
public function getDefaultNamespace() : string |
37
|
|
|
{ |
38
|
|
|
return $this->laravel['modules']->config('paths.generator.policies.path', 'Policies'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the console command arguments. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
protected function getArguments() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
['name', InputArgument::REQUIRED, 'The name of the policy class.'], |
50
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
protected function getTemplateContents() |
58
|
|
|
{ |
59
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
60
|
|
|
|
61
|
|
|
return (new Stub('/policy.plain.stub', [ |
62
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
63
|
|
|
'CLASS' => $this->getClass(), |
64
|
|
|
]))->render(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
protected function getDestinationFilePath() |
71
|
|
|
{ |
72
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
73
|
|
|
|
74
|
|
|
$policyPath = GenerateConfigReader::read('policies'); |
75
|
|
|
|
76
|
|
|
return $path . $policyPath->getPath() . '/' . $this->getFileName() . '.php'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
private function getFileName() |
83
|
|
|
{ |
84
|
|
|
return Str::studly($this->argument('name')); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.