1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SamIT\Yii2\PhpFpm\controllers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Docker\API\Model\BuildInfo; |
8
|
|
|
use Docker\Context\Context; |
9
|
|
|
use Docker\Context\ContextBuilder; |
10
|
|
|
use Docker\Context\ContextInterface; |
11
|
|
|
use Docker\Docker; |
12
|
|
|
use Docker\Stream\BuildStream; |
13
|
|
|
use SamIT\Yii2\PhpFpm\Module; |
14
|
|
|
use yii\console\Controller; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class BuildController |
18
|
|
|
* @package SamIT\Yii2\PhpFpm\controllers |
19
|
|
|
* @property Module $module |
20
|
|
|
*/ |
21
|
|
|
class BuildController extends Controller |
22
|
|
|
{ |
23
|
|
|
public $defaultAction = 'build'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string The name of the created image |
27
|
|
|
* If not explicitly set will take its default from module config. |
28
|
|
|
*/ |
29
|
|
|
public $image; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string The tag of the created image |
33
|
|
|
* If not explicitly set will take its default from module config. |
34
|
|
|
*/ |
35
|
|
|
public $tag; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool whether to push the image after a successful build. |
39
|
|
|
* If not explicitly set will take its default from module config. |
40
|
|
|
*/ |
41
|
|
|
public $push; |
42
|
|
|
|
43
|
|
|
public function init(): void |
44
|
|
|
{ |
45
|
|
|
parent::init(); |
46
|
|
|
$this->push = $this->module->push; |
47
|
|
|
$this->image = $this->module->image; |
48
|
|
|
$this->tag = $this->module->tag; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function actionBuild(): void |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
$params = []; |
56
|
|
|
if (isset($this->image)) { |
57
|
|
|
$params['t'] = "{$this->image}:{$this->tag}"; |
58
|
|
|
} |
59
|
|
|
$buildStream = $this->createBuildStream($params); |
60
|
|
|
|
61
|
|
|
$buildStream->onFrame(function(BuildInfo $buildInfo): void { |
62
|
|
|
echo $buildInfo->getStream(); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
$buildStream->wait(); |
66
|
|
|
echo "Wait finished\n"; |
67
|
|
|
$buildStream->wait(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function createBuildStream(array $params = []): BuildStream |
71
|
|
|
{ |
72
|
|
|
$docker = Docker::create(); |
73
|
|
|
$context = $this->module->createBuildContext(); |
74
|
|
|
|
75
|
|
|
/** @var BuildStream $buildStream */ |
76
|
|
|
$buildStream = $docker->imageBuild($context->toStream(), $params, Docker::FETCH_STREAM); |
|
|
|
|
77
|
|
|
return $buildStream; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function options($actionID) |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
$result = parent::options($actionID); |
84
|
|
|
switch ($actionID) { |
85
|
|
|
case 'build': |
86
|
|
|
// $result[] = 'push'; |
|
|
|
|
87
|
|
|
$result[] = 'image'; |
88
|
|
|
$result[] = 'tag'; |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function optionAliases() |
96
|
|
|
{ |
97
|
|
|
$result = parent::optionAliases(); |
98
|
|
|
// $result['p'] = 'push'; |
|
|
|
|
99
|
|
|
$result['t'] = 'tag'; |
100
|
|
|
$result['i'] = 'image'; |
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: