1 <?php
2
3 namespace Setup;
4
5 use Service;
6 use Settings as SET;
7
8 9 10 11 12
13 class Redis extends AbstractBinSetup {
14
15 16 17 18 19
20 protected $binchecker;
21
22 23 24 25 26
27 function __construct() {
28 $this->dest = DIR_TMP . 'redis.zip';
29 $this->dest_unzip = DIR_TMP . 'redis' . DIRECTORY_SEPARATOR;
30
31 $this->binchecker = new \BinChecker\Redis();
32 $this->links = $this->binchecker->getLinks();
33
34 $this
35 ->promptDownload()
36 ->unzip()
37 ->copy()
38 ->editConf()
39 ->installService();
40 }
41
42 43 44 45 46 47
48 protected function installService() {
49 if(Service::exists('aloredis')) {
50 _echo('Removing previous AloWAMP Redis service');
51 _echo(Service::delete('aloredis'));
52 }
53
54 _echo('Installing Redis service');
55 _echo(Service::installExe('aloredis',
56 DIR_REDIS .
57 $this->version .
58 DIRECTORY_SEPARATOR .
59 'redis-server.exe --service-run --service-name aloredis',
60 'AloWAMP Redis ' . $this->version));
61
62 return $this;
63 }
64
65 66 67 68 69 70
71 protected function editConf() {
72 $file = DIR_REDIS . $this->version . DIRECTORY_SEPARATOR . 'redis.windows.conf';
73
74 if(!file_exists($file)) {
75 die('Redis configuration file not found. Aborting.');
76 } else {
77 _echo('Editing Redis configuration file');
78 $contents = file_get_contents($file);
79
80 $contents = str_ireplace([
81 '# bind 127.0.0.1',
82 'tcp-keepalive 0',
83 'logfile ""'
84 ],
85 [
86 'bind 127.0.0.1',
87 'tcp-keepalive 60',
88 'logfile "' . DIR_LOGS . 'redis' . DIRECTORY_SEPARATOR . 'redis.log"'
89 ],
90 $contents);
91
92 if(file_put_contents($file, $contents) !== false) {
93 _echo('Redis config file edited');
94 } else {
95 die('Failed to edit Redis config file. Abording.');
96 }
97 }
98
99 return $this;
100 }
101
102 103 104 105 106 107
108 protected function copy() {
109 _echo('Copying unzipped contents..');
110
111 $source = rtrim($this->dest_unzip, DIRECTORY_SEPARATOR);
112 $this->unzipped_destination = DIR_REDIS . $this->version;
113
114 if(!file_exists($this->unzipped_destination)) {
115 mkdir($this->unzipped_destination . DIRECTORY_SEPARATOR, 777, true);
116 }
117
118 shell_exec('xcopy /s /e "' . $source . '" "' . $this->unzipped_destination . '"');
119
120 $this->unzipped_destination .= DIRECTORY_SEPARATOR;
121
122 if(file_exists($this->unzipped_destination . 'redis-server.exe')) {
123 _echo('Copy successful!');
124 $this->updateSettings();
125 $this->cleanup();
126 } else {
127 die('Failed to copy. Terminating setup.');
128 }
129
130 return $this;
131 }
132
133 134 135 136 137 138
139 protected function updateSettings() {
140 SET::$s->redis_version = $this->version;
141 SET::$s->save();
142
143 return $this;
144 }
145 }