1 <?php
2 /**
3 * @file
4 * Remote Site.
5 */
6
7 namespace Fluxsauce\Switchboard;
8
9 /**
10 * Remote Site.
11 * @package Fluxsauce\Switchboard
12 */
13 class Site extends Persistent {
14 /**
15 * @var string The machine name of the Provider.
16 */
17 protected $provider;
18
19 /**
20 * @var string The UUID of the Site.
21 */
22 protected $uuid;
23
24 /**
25 * @var string The realm of the site, like devcloud for Acquia.
26 */
27 protected $realm;
28
29 /**
30 * @var string The machine name of the site.
31 */
32 protected $title;
33
34 /**
35 * @var string The UNIX-style username used when SSHing to the site.
36 */
37 protected $unixUsername;
38
39 /**
40 * @var string The Version Control System URL for the site.
41 */
42 protected $vcsUrl;
43
44 /**
45 * @var string The Version Control System type, such as git or svn.
46 */
47 protected $vcsType;
48
49 /**
50 * @var string The Version Control System protocol, such as git or ssh.
51 */
52 protected $vcsProtocol;
53
54 /**
55 * @var int The target port for SSH.
56 */
57 protected $sshPort;
58
59 /**
60 * @var array Contains instances of Fluxsauce\Switchboard\Environment
61 */
62 protected $environments;
63
64 /**
65 * @var string Metadata for ORM defining database structure.
66 */
67 protected $externalKeyName = 'provider';
68
69 /**
70 * Magic __get, overriding Persistent.
71 *
72 * @param string $name
73 * Name of the property.
74 *
75 * @return mixed
76 * Value of set property.
77 * @throws \Exception
78 */
79 public function __get($name) {
80 $name = switchboard_to_camel_case($name);
81 $value = parent::__get($name);
82 if (is_null($value) || drush_get_option('refresh')) {
83 $callers = debug_backtrace();
84 drush_log(dt('Site @site_name is missing value for @name from @calling_function.', array(
85 '@site_name' => $this->name,
86 '@name' => $name,
87 '@calling_function' => $callers[1]['function'],
88 )));
89 if ($this->provider) {
90 $provider =& Provider::getInstance($this->provider);
91 $this->$name = $value = $provider->siteGetField($this->name, $name);
92 }
93 }
94 return $value;
95 }
96
97 /**
98 * Helper to add an environment to a Site.
99 *
100 * @param Environment $environment
101 * Environment to add.
102 */
103 public function environmentAdd(Environment $environment) {
104 if (!is_array($this->environments)) {
105 $this->environments = array();
106 }
107 $this->environments[$environment->name] = $environment;
108 }
109
110 /**
111 * Helper to remove an environment from a Site.
112 *
113 * @param Environment $environment
114 * Environment to remove.
115 */
116 public function environmentRemove(Environment $environment) {
117 unset($this->environments[$environment->name]);
118 }
119
120 /**
121 * Read a site.
122 */
123 public function read() {
124 parent::read();
125 $pdo = Sqlite::get();
126 // Environments.
127 try {
128 $sql_query = 'SELECT name ';
129 $sql_query .= 'FROM environments ';
130 $sql_query .= 'WHERE siteId = :id ';
131 $stmt = $pdo->prepare($sql_query);
132 $stmt->bindParam(':id', $this->id);
133 $result = $stmt->execute();
134 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
135 $this->environmentAdd(new Environment($this->id, $row['name']));
136 }
137 }
138 catch (\PDOException $e) {
139 switchboard_pdo_exception_debug($e);
140 }
141 }
142
143 /**
144 * Render a Site's environments as a Drush table.
145 */
146 public function renderEnvironmentsDrushTable() {
147 $rows = array();
148 $environment = new Environment();
149 $fields = $environment->toArray();
150 $rows = array();
151 $rows[] = array_keys($fields);
152 foreach ($this->__get('environments') as $environment) {
153 $fields = $environment->toArray();
154 $rows[] = array_values($fields);
155 }
156 drush_print_table($rows, TRUE);
157 }
158
159 /**
160 * Render a Site's environments as JSON.
161 */
162 public function renderEnvironmentsJson() {
163 $rows = array();
164 foreach ($this->__get('environments') as $environment) {
165 $rows[] = $environment->toArray();
166 }
167 drush_print(json_encode($rows));
168 }
169
170 /**
171 * Dump to an array.
172 *
173 * @return array
174 * Property names and values.
175 */
176 public function toArray() {
177 $fields = parent::toArray();
178 unset($fields['environments']);
179 return $fields;
180 }
181
182 /**
183 * Build VCS URL.
184 *
185 * @return string
186 * A full VCS connection URL.
187 */
188 public function getVcsUrl() {
189 $url = '';
190 if ($this->__get('vcsProtocol') == 'ssh') {
191 $url .= 'ssh://';
192 }
193 $url .= $this->__get('vcsUrl');
194 return $url;
195 }
196 }
197