|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Support; |
|
4
|
|
|
|
|
5
|
|
|
class Stub |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The stub path. |
|
9
|
|
|
* |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $path; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The base path of stub file. |
|
16
|
|
|
* |
|
17
|
|
|
* @var null|string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static $basePath = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The replacements array. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $replaces = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Defines the style of variables in a file |
|
30
|
|
|
* that will be replaced. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
136 |
|
protected $style; |
|
35
|
|
|
|
|
36
|
136 |
|
/** |
|
37
|
136 |
|
* The contructor. |
|
38
|
136 |
|
* |
|
39
|
|
|
* @param string $path |
|
40
|
|
|
* @param array $replaces |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($path, array $replaces = []) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->path = $path; |
|
45
|
|
|
$this->replaces = $replaces; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
11 |
|
/** |
|
49
|
|
|
* Set variable style. |
|
50
|
11 |
|
* |
|
51
|
|
|
* @param string $style |
|
|
|
|
|
|
52
|
|
|
* @return self |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setStyle($left, $right = null) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!$left) { |
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
if (!$right) { |
|
61
|
|
|
$right = $left; |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$this->style = trim($left . 'VARIABLE_NAME' . $right); |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get variable style. |
|
71
|
|
|
* |
|
72
|
135 |
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
135 |
|
public function getStyle() |
|
75
|
|
|
{ |
|
76
|
135 |
|
return $this->style; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Create new self instance. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $path |
|
83
|
|
|
* @param array $replaces |
|
84
|
230 |
|
* |
|
85
|
|
|
* @return self |
|
86
|
230 |
|
*/ |
|
87
|
230 |
|
public static function create($path, array $replaces = []) |
|
88
|
|
|
{ |
|
89
|
|
|
return new static($path, $replaces); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set stub path. |
|
94
|
135 |
|
* |
|
95
|
|
|
* @param string $path |
|
96
|
135 |
|
* |
|
97
|
|
|
* @return self |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setPath($path) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->path = $path; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
133 |
|
} |
|
105
|
|
|
|
|
106
|
133 |
|
/** |
|
107
|
|
|
* Get stub path. |
|
108
|
133 |
|
* |
|
109
|
133 |
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getPath() |
|
112
|
133 |
|
{ |
|
113
|
|
|
$path = static::getBasePath() . $this->path; |
|
114
|
|
|
|
|
115
|
|
|
return file_exists($path) ? $path : __DIR__ . '/../Commands/stubs' . $this->path; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Set base path. |
|
120
|
130 |
|
* |
|
121
|
|
|
* @param string $path |
|
122
|
130 |
|
*/ |
|
123
|
|
|
public static function setBasePath($path) |
|
124
|
|
|
{ |
|
125
|
|
|
static::$basePath = $path; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get base path. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string|null |
|
132
|
|
|
*/ |
|
133
|
3 |
|
public static function getBasePath() |
|
134
|
|
|
{ |
|
135
|
3 |
|
return static::$basePath; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get stub contents. |
|
140
|
|
|
* |
|
141
|
|
|
* @return mixed|string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getContents() |
|
144
|
|
|
{ |
|
145
|
1 |
|
$contents = file_get_contents($this->getPath()); |
|
146
|
|
|
|
|
147
|
1 |
|
foreach ($this->replaces as $search => $to) { |
|
148
|
|
|
$replace = str_replace('VARIABLE_NAME', strtoupper($search), $this->style ?: config('modules.stubs.style')); |
|
149
|
1 |
|
$contents = str_replace($replace, $to, $contents); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $contents; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get stub contents. |
|
157
|
2 |
|
* |
|
158
|
|
|
* @return string |
|
159
|
2 |
|
*/ |
|
160
|
|
|
public function render() |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->getContents(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Save stub to specific path. |
|
167
|
11 |
|
* |
|
168
|
|
|
* @param string $path |
|
169
|
11 |
|
* @param string $filename |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
|
|
public function saveTo($path, $filename) |
|
174
|
|
|
{ |
|
175
|
|
|
return file_put_contents($path . '/' . $filename, $this->getContents()); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Set replacements array. |
|
180
|
|
|
* |
|
181
|
|
|
* @param array $replaces |
|
182
|
|
|
* |
|
183
|
|
|
* @return $this |
|
184
|
|
|
*/ |
|
185
|
|
|
public function replace(array $replaces = []) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->replaces = $replaces; |
|
188
|
|
|
|
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Get replacements. |
|
194
|
|
|
* |
|
195
|
|
|
* @return array |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getReplaces() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->replaces; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Handle magic method __toString. |
|
204
|
|
|
* |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
|
|
public function __toString() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->render(); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.