|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use App\Action\ActionInterface; |
|
6
|
|
|
use App\Action\Context; |
|
7
|
|
|
use App\Facades\Log; |
|
8
|
|
|
use App\Model\Deployment; |
|
9
|
|
|
use App\Model\Event; |
|
10
|
|
|
use App\Model\Finder\EventFinder; |
|
11
|
|
|
use App\Model\Project; |
|
12
|
|
|
use Closure; |
|
13
|
|
|
use Ronanchilvers\Foundation\Config; |
|
14
|
|
|
use Ronanchilvers\Orm\Orm; |
|
15
|
|
|
use SplQueue; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The builder is responsible for building a new deployment from a given repository |
|
19
|
|
|
* |
|
20
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Builder |
|
23
|
|
|
{ |
|
24
|
|
|
const STAGE_INITIALISE = 'initialise'; |
|
25
|
|
|
const STAGE_PREPARE = 'prepare'; |
|
26
|
|
|
const STAGE_FINALISE = 'finalise'; |
|
27
|
|
|
|
|
28
|
|
|
const MODE_DEFAULT = 0770; |
|
29
|
|
|
const MODE_DEFAULT_FILE = 0640; |
|
30
|
|
|
const MODE_WRITABLE_DIR = 0770; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \App\Model\Project |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $project; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var \App\Model\Deployment |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $deployment; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var \SplQueue |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $actions = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Class constructor |
|
49
|
|
|
* |
|
50
|
|
|
* @param \App\Model\Project $project |
|
51
|
|
|
* @param \App\Model\Deployment $deployment |
|
52
|
|
|
* @param \Ronanchilvers\Foundation\Config $configuration |
|
53
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
54
|
|
|
*/ |
|
55
|
5 |
|
public function __construct( |
|
56
|
|
|
Project $project, |
|
57
|
|
|
Deployment $deployment |
|
58
|
|
|
) { |
|
59
|
5 |
|
$this->project = $project; |
|
60
|
5 |
|
$this->deployment = $deployment; |
|
61
|
5 |
|
$this->actions = new SplQueue; |
|
62
|
5 |
|
$this->actions->setIteratorMode(SplQueue::IT_MODE_DELETE); |
|
63
|
5 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Add an action to the builder |
|
67
|
|
|
* |
|
68
|
|
|
* @param \App\Action\ActionInterface |
|
69
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function addAction(ActionInterface $action) |
|
72
|
|
|
{ |
|
73
|
4 |
|
$this->actions->enqueue($action); |
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Run the builder |
|
78
|
|
|
* |
|
79
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function run(Config $configuration, Context $context = null, Closure $closure = null) |
|
82
|
|
|
{ |
|
83
|
3 |
|
if (is_null($closure)) { |
|
84
|
|
|
$closure = function($string) { |
|
85
|
|
|
Log::debug($string); |
|
86
|
|
|
}; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
$eventFinder = Orm::finder(Event::class); |
|
90
|
3 |
|
if (!$context instanceof Context) { |
|
91
|
|
|
$context = new Context(); |
|
92
|
|
|
} |
|
93
|
3 |
|
$context->set('project', $this->project); |
|
94
|
3 |
|
$context->set('deployment', $this->deployment); |
|
95
|
|
|
|
|
96
|
3 |
|
foreach ($this->actions as $action) { |
|
97
|
3 |
|
$header = 'Running action: ' . $action->getKey(); |
|
98
|
3 |
|
$closure($header); |
|
99
|
3 |
|
$action->setEventFinder($eventFinder); |
|
100
|
3 |
|
if ($action->isHookable()) { |
|
101
|
1 |
|
$action->runHooks( |
|
102
|
1 |
|
'before', |
|
103
|
|
|
$configuration, |
|
104
|
|
|
$context |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
3 |
|
$action->run( |
|
108
|
3 |
|
$configuration, |
|
109
|
|
|
$context |
|
110
|
|
|
); |
|
111
|
3 |
|
if ($action->isHookable()) { |
|
112
|
1 |
|
$action->runHooks( |
|
113
|
1 |
|
'after', |
|
114
|
|
|
$configuration, |
|
115
|
|
|
$context |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
3 |
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|