This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
/
Rocketeer.php
158 lines (134 loc) · 4.01 KB
/
Rocketeer.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
149
150
151
152
153
154
155
156
157
158
<?php
/*
* This file is part of Rocketeer
*
* (c) Maxime Fabre <ehtnam6@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Rocketeer;
use Rocketeer\Traits\HasLocator;
/**
* Handles interaction between the User provided informations
* and the various Rocketeer components.
*
* @author Maxime Fabre <ehtnam6@gmail.com>
*/
class Rocketeer
{
use HasLocator;
/**
* The Rocketeer version.
*
* @type string
*/
const VERSION = '2.2.4';
/**
* The specific commit built.
*/
const COMMIT = '@commit@';
/**
* Global local mode.
*
* @type bool
*/
protected $local = false;
/**
* Returns what stage Rocketeer thinks he's in.
*
* @param string $application
* @param string|null $path
*
* @return string|false
*/
public static function getDetectedStage($application = 'application', $path = null)
{
$current = $path ?: realpath(__DIR__);
preg_match('/'.$application.'\/([a-zA-Z0-9_-]+)\/releases\/([0-9]{14})/', $current, $matches);
return isset($matches[1]) ? $matches[1] : false;
}
//////////////////////////////////////////////////////////////////////
/////////////////////////////// LOCAL ////////////////////////////////
//////////////////////////////////////////////////////////////////////
/**
* @return bool
*/
public function isLocal()
{
return $this->local;
}
/**
* @param bool $local
*/
public function setLocal($local)
{
$this->local = $local;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////// CONFIGURATION ///////////////////////////
//////////////////////////////////////////////////////////////////////
/**
* Get the name of the application to deploy.
*
* @return string
*/
public function getApplicationName()
{
return $this->config->get('rocketeer::application_name');
}
/**
* Get an option from Rocketeer's config file.
*
* @param string $option
*
* @return string|array|Closure
*/
public function getOption($option)
{
$original = $this->config->get('rocketeer::'.$option);
if ($contextual = $this->getContextualOption($option, 'stages', $original)) {
return $contextual;
}
if ($contextual = $this->getContextualOption($option, 'connections', $original)) {
return $contextual;
}
if ($contextual = $this->getContextualOption($option, 'servers', $original)) {
return $contextual;
}
return $original;
}
/**
* Get a contextual option.
*
* @param string $option
* @param string $type [stage,connection]
* @param string|array|null $original
*
* @return string|array|\Closure
*/
protected function getContextualOption($option, $type, $original = null)
{
// Switch context
switch ($type) {
case 'servers':
$contextual = sprintf('rocketeer::connections.%s.servers.%d.config.%s', $this->connections->getConnection(), $this->connections->getServer(), $option);
break;
case 'stages':
$contextual = sprintf('rocketeer::on.stages.%s.%s', $this->connections->getStage(), $option);
break;
case 'connections':
$contextual = sprintf('rocketeer::on.connections.%s.%s', $this->connections->getConnection(), $option);
break;
default:
$contextual = sprintf('rocketeer::%s', $option);
break;
}
// Merge with defaults
$value = $this->config->get($contextual);
if (is_array($value) && $original) {
$value = array_replace($original, $value);
}
return $value;
}
}