1 <?php
2
3 namespace Setup;
4
5 /**
6 * Abstract setup class
7 *
8 * @author Art <a.molcanovas@gmail.com>
9 */
10 abstract class AbstractSetup {
11
12 /**
13 * Where to download
14 *
15 * @var string
16 */
17 protected $dest;
18
19 /**
20 * Where to unzip
21 *
22 * @var string
23 */
24 protected $dest_unzip;
25
26 /**
27 * Downloader instance
28 *
29 * @var \Downloader
30 */
31 protected $downloader;
32
33 /**
34 * Whether a cleanup should be performed
35 *
36 * @var bool
37 */
38 protected $cleanup = true;
39
40 /**
41 * Performs cleanup if needed
42 *
43 * @author Art <a.molcanovas@gmail.com>
44 */
45 function __destruct() {
46 $this->cleanup();
47 }
48
49 /**
50 * Cleans up in tmp
51 *
52 * @author Art <a.molcanovas@gmail.com>
53 *
54 * @param array $files Files to clear
55 *
56 * @return AbstractSetup
57 */
58 protected function cleanup(array $files = []) {
59 if($this->cleanup) {
60 $files = array_merge([$this->dest, $this->dest_unzip], $files);
61
62 foreach($files as $f) {
63 if(file_exists($f)) {
64 _echo('Cleaning up ' . $f);
65
66 if(is_dir($f)) {
67 shell_exec('rd /s /q "' . $f . '"');
68 } else {
69 unlink($f);
70 }
71 }
72 }
73 }
74
75 return $this;
76 }
77
78 /**
79 * Unzips downloaded contents
80 *
81 * @author Art <a.molcanovas@gmail.com>
82 * @return AbstractSetup
83 */
84 protected function unzip() {
85 _echo('Unzipping...');
86 $zip = new \ZipArchive();
87 $res = $zip->open($this->dest);
88
89 if($res === true) {
90 if(file_exists($this->dest_unzip)) {
91 shell_exec('rd /s /q "' . rtrim($this->dest_unzip, '\\/') . '"');
92 }
93
94 $zip->extractTo($this->dest_unzip);
95 $zip->close();
96 } else {
97 die('Failed to unzip ' . $this->dest . '. Terminating.');
98 }
99
100 return $this;
101 }
102 }