1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consigliere\Components\Process; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command as ComponentCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Consigliere\Components\Repository; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
class Installer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The component name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The version of component being installed. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $version; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The component repository instance. |
28
|
|
|
* |
29
|
|
|
* @var \Consigliere\Components\Repository |
30
|
|
|
*/ |
31
|
|
|
protected $repository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The console command instance. |
35
|
|
|
* |
36
|
|
|
* @var \Illuminate\Console\Command |
37
|
|
|
*/ |
38
|
|
|
protected $console; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The destionation path. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $path; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The process timeout. |
49
|
|
|
* |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
protected $timeout = 3360; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The constructor. |
56
|
|
|
* |
57
|
|
|
* @param string $name |
58
|
|
|
* @param string $version |
59
|
|
|
* @param string $type |
60
|
|
|
* @param bool $tree |
61
|
|
|
*/ |
62
|
|
|
public function __construct($name, $version = null, $type = null, $tree = false) |
63
|
|
|
{ |
64
|
|
|
$this->name = $name; |
65
|
|
|
$this->version = $version; |
66
|
|
|
$this->type = $type; |
|
|
|
|
67
|
|
|
$this->tree = $tree; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set destination path. |
72
|
|
|
* |
73
|
|
|
* @param string $path |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function setPath($path) |
78
|
|
|
{ |
79
|
|
|
$this->path = $path; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set the component repository instance. |
86
|
|
|
* |
87
|
|
|
* @param \Consigliere\Components\Repository $repository |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setRepository(Repository $repository) |
92
|
|
|
{ |
93
|
|
|
$this->repository = $repository; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set console command instance. |
100
|
|
|
* |
101
|
|
|
* @param \Consigliere\Components\Process\Command $console |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function setConsole(Command $console) |
106
|
|
|
{ |
107
|
|
|
$this->console = $console; |
|
|
|
|
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set process timeout. |
114
|
|
|
* |
115
|
|
|
* @param int $timeout |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setTimeout($timeout) |
120
|
|
|
{ |
121
|
|
|
$this->timeout = $timeout; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Run the installation process. |
128
|
|
|
* |
129
|
|
|
* @return \Symfony\Component\Process\Process |
130
|
|
|
*/ |
131
|
|
|
public function run() |
132
|
|
|
{ |
133
|
|
|
$process = $this->getProcess(); |
134
|
|
|
|
135
|
|
|
$process->setTimeout($this->timeout); |
136
|
|
|
|
137
|
|
|
if ($this->console instanceof Command) { |
|
|
|
|
138
|
|
|
$process->run(function($type, $line) { |
139
|
|
|
$this->console->line($line); |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $process; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get process instance. |
148
|
|
|
* |
149
|
|
|
* @return \Symfony\Component\Process\Process |
150
|
|
|
*/ |
151
|
|
|
public function getProcess() |
152
|
|
|
{ |
153
|
|
|
switch ($this->type) { |
154
|
|
|
case 'github': |
155
|
|
|
case 'github-https': |
156
|
|
|
case 'bitbucket': |
157
|
|
|
if ($this->tree) { |
158
|
|
|
$process = $this->installViaSubtree(); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$process = $this->installViaGit(); |
162
|
|
|
break; |
163
|
|
|
|
164
|
|
|
default: |
165
|
|
|
$process = $this->installViaComposer(); |
166
|
|
|
break; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $process; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get destination path. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getDestinationPath() |
178
|
|
|
{ |
179
|
|
|
if ($this->path) { |
180
|
|
|
return $this->path; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this->repository->getComponentPath($this->getComponentName()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get git repo url. |
188
|
|
|
* |
189
|
|
|
* @return string|null |
190
|
|
|
*/ |
191
|
|
|
public function getRepoUrl() |
192
|
|
|
{ |
193
|
|
|
switch ($this->type) { |
194
|
|
|
case 'github': |
195
|
|
|
return "[email protected]:{$this->name}.git"; |
196
|
|
|
break; |
|
|
|
|
197
|
|
|
|
198
|
|
|
case 'github-https': |
199
|
|
|
return "https://github.com/{$this->name}.git"; |
200
|
|
|
break; |
|
|
|
|
201
|
|
|
|
202
|
|
|
case 'bitbucket': |
203
|
|
|
return "[email protected]:{$this->name}.git"; |
204
|
|
|
break; |
|
|
|
|
205
|
|
|
|
206
|
|
|
default: |
207
|
|
|
return; |
208
|
|
|
break; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get branch name. |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function getBranch() |
218
|
|
|
{ |
219
|
|
|
return is_null($this->version) ? 'master' : $this->version; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get component name. |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function getComponentName() |
228
|
|
|
{ |
229
|
|
|
$parts = explode('/', $this->name); |
230
|
|
|
|
231
|
|
|
return Str::studly(end($parts)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get composer package name. |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function getPackageName() |
240
|
|
|
{ |
241
|
|
|
if (is_null($this->version)) { |
242
|
|
|
return $this->name . ':dev-master'; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $this->name . ':' . $this->version; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Install the component via git. |
250
|
|
|
* |
251
|
|
|
* @return \Symfony\Component\Process\Process |
252
|
|
|
*/ |
253
|
|
View Code Duplication |
public function installViaGit() |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
return new Process(sprintf( |
256
|
|
|
'cd %s && git clone %s %s && cd %s && git checkout %s', |
257
|
|
|
base_path(), |
258
|
|
|
$this->getRepoUrl(), |
259
|
|
|
$this->getDestinationPath(), |
260
|
|
|
$this->getDestinationPath(), |
261
|
|
|
$this->getBranch() |
262
|
|
|
)); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Install the component via git subtree. |
267
|
|
|
* |
268
|
|
|
* @return \Symfony\Component\Process\Process |
269
|
|
|
*/ |
270
|
|
View Code Duplication |
public function installViaSubtree() |
|
|
|
|
271
|
|
|
{ |
272
|
|
|
return new Process(sprintf( |
273
|
|
|
'cd %s && git remote add %s %s && git subtree add --prefix=%s --squash %s %s', |
274
|
|
|
base_path(), |
275
|
|
|
$this->getComponentName(), |
276
|
|
|
$this->getRepoUrl(), |
277
|
|
|
$this->getDestinationPath(), |
278
|
|
|
$this->getComponentName(), |
279
|
|
|
$this->getBranch() |
280
|
|
|
)); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Install the component via composer. |
285
|
|
|
* |
286
|
|
|
* @return \Symfony\Component\Process\Process |
287
|
|
|
*/ |
288
|
|
|
public function installViaComposer() |
289
|
|
|
{ |
290
|
|
|
return new Process(sprintf( |
291
|
|
|
'cd %s && composer require %s', |
292
|
|
|
base_path(), |
293
|
|
|
$this->getPackageName() |
294
|
|
|
)); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: