|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Spatie\Permission\Contracts\Role as RoleContract; |
|
9
|
|
|
|
|
10
|
|
|
trait AdaptiveCommandParams |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The built-in columns. |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $builtInFields = ['id', 'name', 'guard_name', 'created_at', 'updated_at', 'deleted_at']; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The required extra columns. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $requiredExtraFields = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The optional extra columns. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $optionalExtraFields = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Register arguments or options for extra columns in database. |
|
35
|
|
|
* |
|
36
|
|
|
* @param object $model The model instance |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function registerCustomColumns($model) |
|
41
|
|
|
{ |
|
42
|
|
|
$table = $model->getTable(); |
|
43
|
|
|
$columns = $model->getConnection()->getDoctrineSchemaManager()->listTableColumns($table); |
|
44
|
|
|
$modelType = ($model instanceof RoleContract) ? 'role' : 'permission'; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($columns as $column) { |
|
47
|
|
|
// We will look for columns that are fillable and |
|
48
|
|
|
// are not the inherent columns of the package |
|
49
|
|
|
|
|
50
|
|
|
$columnName = $column->getName(); |
|
51
|
|
|
$isAutoIncrement = $column->getAutoincrement(); |
|
52
|
|
|
|
|
53
|
|
|
if (! in_array($columnName, $this->builtInFields) && $model->isFillable($columnName) && ! $isAutoIncrement) { |
|
54
|
|
|
$isNotNullable = $column->getNotnull(); |
|
55
|
|
|
$defaultValue = $column->getDefault(); |
|
56
|
|
|
$columnLength = $column->getLength(); |
|
|
|
|
|
|
57
|
|
|
$columnType = $column->getType(); |
|
58
|
|
|
$suffixTitle = ('boolean' == Str::slug($columnType) && Str::startsWith($columnName, 'is')) ? 'status' : null; |
|
59
|
|
|
$columnTitle = is_null($suffixTitle) ? str_replace(['-', '_'], ' ', $columnName) : ($columnName.' '.$suffixTitle); |
|
60
|
|
|
|
|
61
|
|
|
if ($isNotNullable && is_null($defaultValue)) { |
|
62
|
|
|
// This is the case that the column needs to fill in the value. |
|
63
|
|
|
// We will register it in the "requiredExtraFields" property |
|
64
|
|
|
|
|
65
|
|
|
$this->requiredExtraFields[] = [ |
|
66
|
|
|
$columnName, |
|
67
|
|
|
InputArgument::REQUIRED, |
|
68
|
|
|
'The '.Str::lower($columnTitle).' of the '.$modelType, |
|
69
|
|
|
]; |
|
70
|
|
|
} else { |
|
71
|
|
|
// This is the case that the column does not require or has a default value. |
|
72
|
|
|
// We will register it in the "optionalExtraFields" property |
|
73
|
|
|
|
|
74
|
|
|
$this->optionalExtraFields[] = [ |
|
75
|
|
|
$columnName, |
|
76
|
|
|
null, |
|
77
|
|
|
InputOption::VALUE_OPTIONAL, |
|
78
|
|
|
'The '.Str::lower($columnTitle).' of the '.$modelType, |
|
79
|
|
|
$defaultValue, |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the input values of the command parameters. |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getInputParams() |
|
92
|
|
|
{ |
|
93
|
|
|
$params = []; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($this->getArguments() as $argument) { |
|
|
|
|
|
|
96
|
|
|
$key = $argument[0]; |
|
97
|
|
|
$value = $this->argument($key); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$params[$key] = $value; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
foreach ($this->getOptions() as $option) { |
|
|
|
|
|
|
103
|
|
|
$key = $option[0]; |
|
104
|
|
|
$value = $this->option($key); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
if ('guard' == $key) { |
|
107
|
|
|
$params['guard_name'] = $value; |
|
108
|
|
|
} else { |
|
109
|
|
|
$params[$key] = $value; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $params; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.