|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IanOlson\Support\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use IanOlson\Support\Helpers\ValidateVariableTypeHelper; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
trait CommandTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Model name. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $model; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Table name. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $table; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Base path. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $path; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get model. |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
8 |
|
*/ |
|
37
|
|
|
protected function getModel() |
|
38
|
8 |
|
{ |
|
39
|
|
|
return $this->model; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Set model. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $model |
|
46
|
|
|
* |
|
47
|
|
|
* @return $this |
|
48
|
8 |
|
*/ |
|
49
|
|
|
protected function setModel($model) |
|
50
|
8 |
|
{ |
|
51
|
|
|
ValidateVariableTypeHelper::isString($model, 'model'); |
|
52
|
6 |
|
|
|
53
|
|
|
$this->model = $model; |
|
54
|
6 |
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get table. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
6 |
|
*/ |
|
63
|
|
|
protected function getTable() |
|
64
|
6 |
|
{ |
|
65
|
|
|
return $this->table; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set table. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $table |
|
72
|
|
|
* |
|
73
|
|
|
* @return $this |
|
74
|
6 |
|
*/ |
|
75
|
|
|
protected function setTable($table) |
|
76
|
6 |
|
{ |
|
77
|
|
|
ValidateVariableTypeHelper::isString($table, 'table'); |
|
78
|
4 |
|
|
|
79
|
|
|
$this->table = $table; |
|
80
|
4 |
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get path. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
12 |
|
*/ |
|
89
|
|
|
protected function getPath() |
|
90
|
12 |
|
{ |
|
91
|
|
|
return $this->path; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set path. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $path |
|
98
|
|
|
* |
|
99
|
|
|
* @return $this |
|
100
|
8 |
|
*/ |
|
101
|
|
|
protected function setPath($path) |
|
102
|
8 |
|
{ |
|
103
|
|
|
ValidateVariableTypeHelper::isString($path, 'path'); |
|
104
|
6 |
|
|
|
105
|
|
|
$this->path = $path; |
|
106
|
6 |
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Create directory. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $directory |
|
114
|
4 |
|
*/ |
|
115
|
|
|
protected function createDirectory($directory) |
|
116
|
4 |
|
{ |
|
117
|
|
|
File::makeDirectory("{$this->getPath()}/{$directory}"); |
|
118
|
2 |
|
|
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get content from stub and update. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $type |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
6 |
|
*/ |
|
129
|
|
|
protected function getContent($type) |
|
130
|
6 |
|
{ |
|
131
|
6 |
|
$content = null; |
|
132
|
|
|
$stub = self::COMMAND_DIRECTORY . self::STUB_DIRECTORY . "{$type}.stub"; |
|
133
|
6 |
|
|
|
134
|
4 |
|
if (isset($this->model) && !is_null($this->getModel())) { |
|
135
|
|
|
$stubContent = str_replace('{{model}}', $this->getModel(), File::get($stub)); |
|
136
|
4 |
|
$stubContent = str_replace('{{model_lower}}', Str::lower($this->getModel()), $stubContent); |
|
137
|
2 |
|
|
|
138
|
1 |
|
if (isset($this->table) && !is_null($this->getTable())) { |
|
139
|
|
|
$stubContent = str_replace('{{table}}', $this->getTable(), $stubContent); |
|
140
|
4 |
|
} |
|
141
|
2 |
|
|
|
142
|
1 |
|
$content = $stubContent; |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
6 |
|
if (is_null($content)) { |
|
146
|
2 |
|
$content = File::get($stub); |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
6 |
|
return $content; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Write the file to the filesystem. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $content |
|
156
|
|
|
* @param string $directory |
|
157
|
|
|
* @param string $fileName |
|
158
|
|
|
* @param null $end |
|
159
|
|
|
* @param null $ext |
|
160
|
|
|
*/ |
|
161
|
4 |
|
protected function writeFile($content, $directory, $fileName, $end = null, $ext = null) |
|
162
|
|
|
{ |
|
163
|
4 |
|
if (is_null($ext)) { |
|
164
|
4 |
|
$ext = '.php'; |
|
165
|
2 |
|
} |
|
166
|
|
|
|
|
167
|
4 |
|
if (!is_null($directory)) { |
|
168
|
2 |
|
$fullPath = "{$this->getPath()}/{$directory}/{$fileName}{$end}{$ext}"; |
|
169
|
1 |
|
} else { |
|
170
|
2 |
|
$fullPath = "{$this->getPath()}/{$fileName}{$end}{$ext}"; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
4 |
|
File::put($fullPath, $content); |
|
174
|
|
|
|
|
175
|
4 |
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|