1 <?php
2
3 namespace VersionSwitch;
4
5 use Format;
6 use IO;
7 use Service;
8 use Settings as SET;
9
10 11 12 13 14
15 abstract class AbstractVersionSwitch {
16
17 18 19 20 21
22 protected $dependent_dirs = [];
23
24 25 26 27 28
29 protected $ini;
30
31 32 33 34 35
36 protected $dir;
37
38 39 40 41 42
43 protected $old_version;
44
45 46 47 48 49
50 protected $new_version;
51
52 53 54 55 56
57 protected $service;
58
59 60 61 62 63
64 function __construct() {
65 $this->old_version = SET::$s->{$this->ini};
66 $this->trySwitch();
67 }
68
69 70 71 72 73 74
75 function trySwitch() {
76 if(!file_exists($this->dir)) {
77 die('The module is not installed');
78 } elseif(!$this->old_version) {
79 die('Module installation config entry missing');
80 } else {
81 $installed_versions = scandir($this->dir);
82 Format::formatScandir($installed_versions);
83
84 if(count($installed_versions) < 2) {
85 die('You only have one module version installed');
86 } else {
87 _echo('Your current version is ' .
88 $this->old_version .
89 '. Which version would you like to switch to? Available options are:');
90
91 foreach($installed_versions as $v) {
92 echo "\t$v" . PHP_EOL;
93 }
94
95 $io = IO::readline();
96
97 if(!$io) {
98 return $this->trySwitch();
99 } elseif(!in_array($io, $installed_versions)) {
100 _echo('You don\'t have that version installed');
101
102 return $this->trySwitch();
103 } else {
104 SET::$s->{$this->ini} = $this->new_version = $io;
105 SET::$s->save();
106 $this->updateDependencies()->restartService();
107 }
108 }
109 }
110
111 return $this;
112 }
113
114 115 116 117 118 119
120 protected function restartService() {
121 if($this->service) {
122 Service::stop($this->service);
123 for($i = 0; $i < 10; $i++) {
124 _echo('Waiting...');
125 }
126 Service::start($this->service);
127 }
128
129 return $this;
130 }
131
132 133 134 135 136 137
138 function updateDependencies() {
139 $allowed = [DIR_APACHE];
140
141 _echo('Checking dependencies');
142
143 foreach($this->dependent_dirs as $d) {
144 if(file_exists($d) && in_array($d, $allowed)) {
145 $scan = scandir($d);
146 Format::formatScandir($scan);
147
148 if(!empty($scan)) {
149 $method = $service = false;
150
151 switch($d) {
152 case DIR_APACHE:
153 $method = 'updateApache';
154 $service = 'aloapache';
155 break;
156 }
157
158 if($method) {
159 foreach($scan as $version) {
160 call_user_func([$this, $method], $version);
161 }
162
163 if($service && Service::exists($service)) {
164 _echo('Restarting service');
165 Service::stop($service);
166 for($i = 0; $i < 10; $i++) {
167 _echo('Waiting...');
168 }
169 Service::start($service);
170 }
171 }
172 }
173 }
174 }
175
176 _echo('Switch complete');
177
178 return $this;
179 }
180
181 182 183 184 185 186 187
188 protected function updateApache($version) {
189 _echo('Updating Apache ' . $version);
190 $file = DIR_APACHE . $version . DIRECTORY_SEPARATOR . 'conf' . DIRECTORY_SEPARATOR . 'httpd.conf';
191
192 if(!file_exists($file)) {
193 _echo('Failed to update ' . $version . ': httpd.conf not found');
194 } else {
195 $contents = file_get_contents($file);
196 $contents =
197 str_ireplace('bin' . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . $this->old_version,
198 'bin' . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . $this->new_version,
199 $contents);
200
201 if(file_put_contents($file, $contents) !== false) {
202 _echo($version . ' updated');
203 } else {
204 _echo('Failed to edit ' . $version . '\'s httpd.conf');
205 }
206 }
207 }
208 }