|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of Laravel Zero. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Nuno Maduro <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace LaravelZero\Framework\Providers\Composer; |
|
15
|
|
|
|
|
16
|
|
|
use function collect; |
|
17
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
18
|
|
|
use LaravelZero\Framework\Contracts\Providers\ComposerContract; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
use Symfony\Component\Process\Process; |
|
21
|
|
|
use Throwable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @codeCoverageIgnore |
|
25
|
|
|
* @internal |
|
26
|
|
|
*/ |
|
27
|
|
|
final class Composer implements ComposerContract |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Holds an instance of the app. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
|
33
|
|
|
*/ |
|
34
|
|
|
private $app; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Composer constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(Application $app) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->app = $app; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function install(array $options = []): bool |
|
52
|
|
|
{ |
|
53
|
|
|
$cmd = 'composer install'; |
|
54
|
|
|
|
|
55
|
|
|
collect($options)->each( |
|
56
|
|
|
function ($option) use (&$cmd) { |
|
57
|
|
|
$cmd .= " $option"; |
|
58
|
|
|
} |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
return $this->run($cmd, $this->app->basePath()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function require(string $package, bool $dev = false): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->run("composer require $package".($dev ? ' --dev' : ''), $this->app->basePath()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function createProject(string $skeleton, string $projectName, array $options): bool |
|
76
|
|
|
{ |
|
77
|
|
|
$cmd = "composer create-project $skeleton $projectName"; |
|
78
|
|
|
|
|
79
|
|
|
collect($options)->each( |
|
80
|
|
|
function ($option) use (&$cmd) { |
|
81
|
|
|
$cmd .= " $option"; |
|
82
|
|
|
} |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
return $this->run($cmd); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Runs the provided command on the provided folder. |
|
90
|
|
|
*/ |
|
91
|
|
|
private function run(string $cmd, string $cwd = null): bool |
|
92
|
|
|
{ |
|
93
|
|
|
$process = new Process($cmd, $cwd); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
$process->setTimeout(300); |
|
96
|
|
|
|
|
97
|
|
|
if ($process->isTty()) { |
|
98
|
|
|
$process->setTty(true); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
$output = $this->app->make(OutputInterface::class); |
|
103
|
|
|
} catch (Throwable $e) { |
|
104
|
|
|
$output = null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($output) { |
|
108
|
|
|
$output->write("\n"); |
|
109
|
|
|
$process->run( |
|
110
|
|
|
function ($type, $line) use ($output) { |
|
111
|
|
|
$output->write($line); |
|
112
|
|
|
} |
|
113
|
|
|
); |
|
114
|
|
|
} else { |
|
115
|
|
|
$process->run(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $process->isSuccessful(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|