|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JunaidQadirB\Cray\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
|
|
9
|
|
|
class PolicyMakeCommand extends GeneratorCommand |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The console command name. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $name = 'mbt:policy'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The console command description. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $description = 'Create a new policy class'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The type of class being generated. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $type = 'Policy'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Build the class with the given name. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $name |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function buildClass($name) |
|
39
|
|
|
{ |
|
40
|
|
|
$stub = $this->replaceUserNamespace( |
|
41
|
|
|
parent::buildClass($name) |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$model = $this->option('model'); |
|
45
|
|
|
|
|
46
|
|
|
return $model ? $this->replaceModel($stub, $model) : $stub; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Replace the User model namespace. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $stub |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function replaceUserNamespace($stub) |
|
56
|
|
|
{ |
|
57
|
|
|
$model = $this->userProviderModel(); |
|
58
|
|
|
|
|
59
|
|
|
if (!$model) { |
|
|
|
|
|
|
60
|
|
|
return $stub; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return str_replace( |
|
64
|
|
|
$this->rootNamespace() . 'User', |
|
65
|
|
|
$model, |
|
66
|
|
|
$stub |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Replace the model for the given stub. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $stub |
|
74
|
|
|
* @param string $model |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function replaceModel($stub, $model) |
|
78
|
|
|
{ |
|
79
|
|
|
$model = str_replace('/', '\\', $model); |
|
80
|
|
|
|
|
81
|
|
|
$namespaceModel = $this->laravel->getNamespace() . $model; |
|
82
|
|
|
|
|
83
|
|
|
if (Str::startsWith($model, '\\')) { |
|
84
|
|
|
$stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub); |
|
85
|
|
|
} else { |
|
86
|
|
|
$stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$stub = str_replace( |
|
90
|
|
|
"use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$model = class_basename(trim($model, '\\')); |
|
94
|
|
|
|
|
95
|
|
|
$dummyUser = class_basename($this->userProviderModel()); |
|
96
|
|
|
|
|
97
|
|
|
$dummyModel = Str::camel($model) === 'user' ? 'model' : $model; |
|
98
|
|
|
|
|
99
|
|
|
$stub = str_replace('DocDummyModel', Str::snake($dummyModel, ' '), $stub); |
|
100
|
|
|
|
|
101
|
|
|
$stub = str_replace('DummyModelPlural', strtolower(Str::plural(Str::slug(studly_to_words($model), '_'))), $stub); |
|
102
|
|
|
|
|
103
|
|
|
$stub = str_replace('DummyModel', $model, $stub); |
|
104
|
|
|
|
|
105
|
|
|
$stub = str_replace('dummyModel', Str::camel($dummyModel), $stub); |
|
106
|
|
|
|
|
107
|
|
|
$stub = str_replace('DummyUser', $dummyUser, $stub); |
|
108
|
|
|
|
|
109
|
|
|
return str_replace('DocDummyPluralModel', Str::snake(Str::pluralStudly($dummyModel), ' '), $stub); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get the stub file for the generator. |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function getStub() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->option('model') |
|
120
|
|
|
? resource_path('/stubs/policy.stub') |
|
121
|
|
|
: resource_path('/stubs/policy.plain.stub'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get the default namespace for the class. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $rootNamespace |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getDefaultNamespace($rootNamespace) |
|
131
|
|
|
{ |
|
132
|
|
|
return $rootNamespace . '\Policies'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get the console command arguments. |
|
137
|
|
|
* |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function getOptions() |
|
141
|
|
|
{ |
|
142
|
|
|
return [ |
|
143
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the policy applies to'], |
|
144
|
|
|
]; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: