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
|
|
|
|
10
|
|
View Code Duplication |
class MakePolicyCommand extends GeneratorCommand |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
use ModuleCommandTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The name of argument name. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $argumentName = 'name'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name = 'module:make-policy'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Create a new policy for the specified module.'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the console command arguments. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
58 |
|
protected function getArguments() |
41
|
|
|
{ |
42
|
|
|
return array( |
43
|
58 |
|
array('name', InputArgument::REQUIRED, 'The name of the policy class.'), |
44
|
|
|
array('module', InputArgument::OPTIONAL, 'The name of module will be used.'), |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
1 |
|
protected function getTemplateContents() |
52
|
|
|
{ |
53
|
1 |
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
54
|
|
|
|
55
|
1 |
|
return (new Stub('/policy.plain.stub', [ |
56
|
1 |
|
'NAMESPACE' => $this->getClassNamespace($module), |
57
|
1 |
|
'CLASS' => $this->getClass(), |
58
|
1 |
|
]))->render(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
1 |
|
protected function getDestinationFilePath() |
65
|
|
|
{ |
66
|
1 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
67
|
|
|
|
68
|
1 |
|
$policyPath = $this->laravel['modules']->config('paths.generator.policies'); |
69
|
|
|
|
70
|
1 |
|
return $path . $policyPath . '/' . $this->getFileName() . '.php'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
1 |
|
private function getFileName() |
77
|
|
|
{ |
78
|
1 |
|
return Str::studly($this->argument('name')); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
1 |
|
public function getDefaultNamespace() |
85
|
|
|
{ |
86
|
1 |
|
return $this->laravel['modules']->config('paths.generator.policies', 'Policies'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.