1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ptondereau\PackMe\Crafters; |
4
|
|
|
|
5
|
|
|
use ConstantNull\Backstubber\FileGenerator; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Ptondereau\PackMe\Exception\CrafterException; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PHPCrafter. |
12
|
|
|
*/ |
13
|
|
|
class PHPCrafter implements CrafterInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Package name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Author name. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $author = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Path destination for all generated files. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $destination = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Package's description. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $description = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Stube file generator. |
45
|
|
|
* |
46
|
|
|
* @var FileGenerator |
47
|
|
|
*/ |
48
|
|
|
protected $stubber; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Filesystem |
52
|
|
|
*/ |
53
|
|
|
private $filesystem; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* PHPCrafter constructor. |
57
|
|
|
* |
58
|
|
|
* @param FileGenerator $stubber |
59
|
|
|
* @param Filesystem $filesystem |
60
|
|
|
*/ |
61
|
21 |
|
public function __construct(FileGenerator $stubber, Filesystem $filesystem) |
62
|
|
|
{ |
63
|
21 |
|
$this->stubber = $stubber; |
64
|
21 |
|
$this->filesystem = $filesystem; |
65
|
21 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
15 |
|
public function setName($name) |
73
|
|
|
{ |
74
|
15 |
|
$this->name = $name; |
75
|
|
|
|
76
|
15 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $author |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
12 |
|
public function setAuthor(array $author) |
84
|
|
|
{ |
85
|
12 |
|
$this->author = $author; |
|
|
|
|
86
|
|
|
|
87
|
12 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $destination |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
9 |
|
public function setDestination($destination) |
96
|
|
|
{ |
97
|
9 |
|
$this->destination = $destination; |
98
|
|
|
|
99
|
9 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $description |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
15 |
|
public function setDescription($description) |
108
|
|
|
{ |
109
|
15 |
|
$this->description = $description; |
110
|
|
|
|
111
|
15 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Craft the application with parameters. |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
15 |
|
public function craft() |
120
|
|
|
{ |
121
|
15 |
|
$this->verifyParameters(); |
122
|
6 |
|
$this->verifyApplicationDoesntExist( |
123
|
6 |
|
$directory = ($this->destination) ? getcwd().'/'.$this->destination : getcwd() |
124
|
6 |
|
); |
125
|
|
|
|
126
|
3 |
|
$packageInfo = explode('/', $this->name); |
127
|
3 |
|
$vendor = Str::studly($packageInfo[0]); |
128
|
3 |
|
$package = Str::studly($packageInfo[1]); |
129
|
|
|
|
130
|
3 |
|
$stubPath = realpath(__DIR__.'/../stubs'); |
131
|
3 |
|
$this->filesystem->mirror($stubPath, $directory); |
132
|
|
|
|
133
|
|
|
// set delimiters |
134
|
3 |
|
$this->stubber->withDelimiters('{{', '}}'); |
135
|
|
|
|
136
|
|
|
// set keywords |
137
|
3 |
|
$this->stubber->setRaw('author', $this->author['name'].' <'.$this->author['email'].'>') |
138
|
3 |
|
->setRaw('name', $this->name) |
139
|
3 |
|
->setRaw('description', $this->description ?: '') |
140
|
3 |
|
->setRaw('vendor', $vendor) |
141
|
3 |
|
->setRaw('package', $package) |
142
|
3 |
|
->setRaw('authorName', $this->author['name']) |
143
|
3 |
|
->setRaw('authorEmail', $this->author['email']) |
144
|
3 |
|
->setRaw('config', Str::slug($package)); |
145
|
|
|
|
146
|
|
|
// array of all stub files |
147
|
3 |
|
$stubFiles = new \RecursiveIteratorIterator( |
148
|
3 |
|
new \RecursiveDirectoryIterator( |
149
|
3 |
|
$directory, |
150
|
|
|
\RecursiveDirectoryIterator::SKIP_DOTS |
151
|
3 |
|
) |
152
|
3 |
|
); |
153
|
|
|
|
154
|
|
|
// find and replace |
155
|
3 |
|
foreach ($stubFiles as $stub) { |
156
|
3 |
|
$new = pathinfo($stub); |
157
|
3 |
|
$this->stubber->useStub($stub); |
158
|
3 |
|
if ($this->isConfigFile($new['basename'])) { |
159
|
3 |
|
$this->stubber->generate($new['dirname'].'/'.Str::slug($package).'.php'); |
160
|
3 |
|
} elseif ($this->isServiceProviderFile($new['basename'])) { |
161
|
3 |
|
$this->stubber->generate($new['dirname'].'/'.$package.'ServiceProvider.php'); |
162
|
3 |
|
} else { |
163
|
3 |
|
$this->stubber->generate($new['dirname'].'/'.$new['filename']); |
164
|
|
|
} |
165
|
3 |
|
$this->filesystem->remove($stub); |
166
|
3 |
|
} |
167
|
3 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Verify all parameters presence (name, author, destination). |
171
|
|
|
*/ |
172
|
15 |
|
protected function verifyParameters() |
173
|
|
|
{ |
174
|
15 |
|
if (null === $this->name) { |
175
|
3 |
|
throw new CrafterException('Package name is not defined!'); |
176
|
|
|
} |
177
|
|
|
|
178
|
12 |
|
if (null === $this->author) { |
179
|
3 |
|
throw new CrafterException('Author is not defined!'); |
180
|
|
|
} |
181
|
|
|
|
182
|
9 |
|
if (null === $this->destination) { |
183
|
3 |
|
throw new CrafterException('Destination folder is not defined!'); |
184
|
|
|
} |
185
|
6 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Verify that the application does not already exist. |
189
|
|
|
* |
190
|
|
|
* @param string $directory |
191
|
|
|
* |
192
|
|
|
* @throws CrafterException |
193
|
|
|
*/ |
194
|
6 |
|
protected function verifyApplicationDoesntExist($directory) |
195
|
|
|
{ |
196
|
6 |
|
if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) { |
197
|
3 |
|
throw new CrafterException('Package already exists!'); |
198
|
|
|
} |
199
|
3 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Detect if a file is the config file. |
203
|
|
|
* |
204
|
|
|
* @param $file |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
3 |
|
private function isConfigFile($file) |
209
|
|
|
{ |
210
|
3 |
|
return $file === 'package.php.stub'; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Detect if a file is the service provider file. |
215
|
|
|
* |
216
|
|
|
* @param $file |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
3 |
|
private function isServiceProviderFile($file) |
221
|
|
|
{ |
222
|
3 |
|
return $file === 'ServiceProvider.php.stub'; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..