1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SamIT\Yii2\PhpFpm\controllers; |
5
|
|
|
|
6
|
|
|
use SamIT\Docker\Context; |
7
|
|
|
use SamIT\Docker\Docker; |
8
|
|
|
use SamIT\Yii2\PhpFpm\Module; |
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
use yii\console\Controller; |
12
|
|
|
use yii\helpers\Console; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BuildController |
16
|
|
|
* @package SamIT\Yii2\PhpFpm\controllers |
17
|
|
|
* @property Module $module |
18
|
|
|
*/ |
19
|
|
|
class BuildController extends Controller |
20
|
|
|
{ |
21
|
|
|
public $defaultAction = 'build'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string The name of the created image |
25
|
|
|
* If not explicitly set will take its default from module config. |
26
|
|
|
*/ |
27
|
|
|
public $image; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string The tag of the created image |
31
|
|
|
* If not explicitly set will take its default from module config. |
32
|
|
|
*/ |
33
|
|
|
public $tag; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool whether to push the image after a successful build. |
37
|
|
|
* If not explicitly set will take its default from module config. |
38
|
|
|
*/ |
39
|
|
|
public $push; |
40
|
|
|
|
41
|
|
|
public function init(): void |
42
|
|
|
{ |
43
|
|
|
parent::init(); |
44
|
|
|
$this->push = $this->module->push; |
45
|
|
|
$this->image = $this->module->image; |
46
|
|
|
$this->tag = $this->module->tag; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $targetPath The path where the docker build context should be stored |
51
|
|
|
*/ |
52
|
|
|
public function actionCreateContext(string $targetPath): void |
53
|
|
|
{ |
54
|
|
|
$filesystem = new Filesystem(); |
55
|
|
|
if (!is_dir($targetPath)) { |
56
|
|
|
$filesystem->mkdir($targetPath); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$context = new Context(); |
60
|
|
|
$this->module->createBuildContext($context, $this->tag, \Yii::getAlias('@app')); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$filesystem->mirror($context->getDirectory(), $targetPath); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function actionBuild(): void |
66
|
|
|
{ |
67
|
|
|
if ($this->push && !isset($this->image)) { |
68
|
|
|
throw new InvalidConfigException("When using the push option, you must configure or provide image"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$params = []; |
72
|
|
|
|
73
|
|
|
if (isset($this->image)) { |
74
|
|
|
$params['t'] = "{$this->image}:{$this->tag}"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$context = new Context(); |
78
|
|
|
$this->module->createBuildContext($context, $this->tag, \Yii::getAlias('@app')); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$docker = new Docker(); |
81
|
|
|
$docker->build($context, "{$this->image}:{$this->tag}"); |
82
|
|
|
|
83
|
|
|
if ($this->push) { |
84
|
|
|
$docker->push("{$this->image}:{$this->tag}"); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function actionTestClient(): void |
89
|
|
|
{ |
90
|
|
|
$this->stdout("It seems the console client works!\n", Console::FG_GREEN); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
public function options($actionID) |
95
|
|
|
{ |
96
|
|
|
|
97
|
|
|
$result = parent::options($actionID); |
98
|
|
|
switch ($actionID) { |
99
|
|
|
case 'build': |
100
|
|
|
$result[] = 'push'; |
101
|
|
|
$result[] = 'image'; |
102
|
|
|
$result[] = 'tag'; |
103
|
|
|
break; |
104
|
|
|
} |
105
|
|
|
return $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function optionAliases() |
109
|
|
|
{ |
110
|
|
|
$result = parent::optionAliases(); |
111
|
|
|
$result['p'] = 'push'; |
112
|
|
|
$result['t'] = 'tag'; |
113
|
|
|
$result['i'] = 'image'; |
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function stdout($string) |
118
|
|
|
{ |
119
|
|
|
if ($this->isColorEnabled()) { |
120
|
|
|
$args = \func_get_args(); |
121
|
|
|
\array_shift($args); |
122
|
|
|
$string = Console::ansiFormat($string, $args); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
echo $string; |
126
|
|
|
return \strlen($string); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.