|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acquia\Cloud\Environment; |
|
4
|
|
|
|
|
5
|
|
|
use Acquia\Environment\Environment; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @see https://docs.acquia.com/cloud/configure/env-variable |
|
9
|
|
|
*/ |
|
10
|
|
|
class LocalEnvironment extends Environment implements CloudEnvironmentInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $creds = array(); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $sitegroup; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $sitegroup |
|
24
|
|
|
*/ |
|
25
|
18 |
|
public function __construct($sitegroup) |
|
26
|
|
|
{ |
|
27
|
18 |
|
$this->sitegroup = $sitegroup; |
|
28
|
18 |
|
parent::__construct(); |
|
29
|
18 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Calculates and returns the environment. |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
18 |
|
protected function init() |
|
37
|
|
|
{ |
|
38
|
18 |
|
return self::LOCAL; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritDoc} |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function isAcquia() |
|
45
|
|
|
{ |
|
46
|
3 |
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritDoc} |
|
51
|
|
|
*/ |
|
52
|
3 |
|
public function setSiteGroup($sitegroup) |
|
53
|
|
|
{ |
|
54
|
3 |
|
$this->sitegroup = $sitegroup; |
|
55
|
3 |
|
return $this; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function getSiteGroup() |
|
62
|
|
|
{ |
|
63
|
6 |
|
return $this->sitegroup; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
6 |
|
public function serviceCredentials() |
|
70
|
|
|
{ |
|
71
|
6 |
|
return $this->creds; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Adds credentials to a database server. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $acquiaDbName |
|
78
|
|
|
* @param string $localDbName |
|
79
|
|
|
* @param string $username |
|
80
|
|
|
* @param string|null $password |
|
81
|
|
|
* @param string $host |
|
82
|
|
|
* @param integer $port |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Acquia\Cloud\Environment\LocalEnvironment |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function addDatabaseCredentials($acquiaDbName, $localDbName, $username, $password = null, $host = 'localhost', $port = 3306) |
|
87
|
|
|
{ |
|
88
|
3 |
|
$connString = $username; |
|
89
|
3 |
|
if ($password !== null) { |
|
90
|
3 |
|
$connString . ':' . $password; |
|
91
|
3 |
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
$this->creds['databases'][$acquiaDbName] = array( |
|
94
|
3 |
|
'id' => '1', |
|
95
|
3 |
|
'role' => $this->sitegroup, // Is this right? For local doesn't really matter. |
|
96
|
3 |
|
'name' => $localDbName, |
|
97
|
3 |
|
'user' => $username, |
|
98
|
3 |
|
'pass' => $password, |
|
99
|
|
|
'db_url_ha' => array( |
|
100
|
3 |
|
$host => "mysqli://$connString@$host:$port/mysiteprod" |
|
101
|
3 |
|
), |
|
102
|
3 |
|
'db_cluster_id' => '1', |
|
103
|
3 |
|
'port' => $port, |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
3 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Adds credentials to a Memcached server. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $host |
|
113
|
|
|
* @param string $port |
|
114
|
|
|
* |
|
115
|
|
|
* @return \Acquia\Cloud\Environment\LocalEnvironment |
|
116
|
|
|
*/ |
|
117
|
3 |
|
public function addMemcacheCredentials($host, $port) |
|
118
|
|
|
{ |
|
119
|
3 |
|
$this->creds['memcached_servers'][] = $host . ':' . $port; |
|
120
|
3 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.