-
Notifications
You must be signed in to change notification settings - Fork 119
/
LocalCommand.php
148 lines (134 loc) · 3.96 KB
/
LocalCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Laravel\VaporCli\Commands;
use Exception;
use Laravel\VaporCli\Helpers;
use Laravel\VaporCli\Path;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Yaml;
class LocalCommand extends Command
{
/**
* The Docker for various PHP versions.
*
* @var array
*/
public static $images = [
'7.3' => 'laravelphp/vapor:php73',
'7.4' => 'laravelphp/vapor:php74',
'8.0' => 'laravelphp/vapor:php80',
'8.1' => 'laravelphp/vapor:php81',
'8.2' => 'laravelphp/vapor:php82',
'8.3' => 'laravelphp/vapor:php83',
];
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->ignoreValidationErrors();
}
/**
* Configure the command options.
*
* @return void
*/
protected function configure()
{
$this
->setName('local')
->addOption('php', null, InputOption::VALUE_OPTIONAL, 'The PHP version that should be used to execute the command')
->setDescription('Run a command inside a simulated Vapor environment');
}
/**
* Execute the command.
*
* @return int
*
* @throws Exception
*/
public function handle()
{
$options = array_slice($_SERVER['argv'], $this->option('php') && array_search('--php='.$this->option('php'), $_SERVER['argv']) !== false ? 3 : 2);
$status = 0;
file_put_contents(
$dockerComposePath = Path::current().'/vapor-docker-compose.yml',
Yaml::dump($this->dockerConfiguration(), $inline = 20, $spaces = 4)
);
passthru(
implode(' ', array_merge([
'docker-compose',
'-f',
$dockerComposePath,
'run',
'--rm',
'-e DB_HOST=mysql',
'-e DB_DATABASE=vapor',
'-e DB_PORT=3306',
'-e DB_USERNAME=vapor',
'-e DB_PASSWORD=secret',
'-e REDIS_HOST=redis',
'-w /app',
'-v',
Path::current().':/app',
'app',
], $options)),
$status
);
unlink($dockerComposePath);
return $status;
}
/**
* Get the Docker configuration.
*
* @return array
*
* @throws Exception
*/
protected function dockerConfiguration()
{
if ($this->option('php') && ! isset(static::$images[$this->option('php')])) {
Helpers::abort('Invalid PHP version.');
}
return [
'version' => '3.7',
'services' => [
'redis' => [
'image' => 'redis:alpine',
'volumes' => [
'vapor_redis:/data',
],
'restart' => 'always',
],
'mysql' => [
'image' => 'mysql:8.0',
'volumes' => [
0 => 'vapor_mysql:/var/lib/mysql',
],
'restart' => 'always',
'environment' => [
'MYSQL_ROOT_PASSWORD' => 'secret',
'MYSQL_DATABASE' => 'vapor',
'MYSQL_USER' => 'vapor',
'MYSQL_PASSWORD' => 'secret',
],
],
'app' => [
'image' => static::$images[$this->option('php') ? $this->option('php') : '8.3'],
'depends_on' => [
0 => 'mysql',
1 => 'redis',
],
'restart' => 'always',
'init' => true,
],
],
'volumes' => [
'vapor_mysql' => [],
'vapor_redis' => [],
],
];
}
}