1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of ci-phpunit-test |
4
|
|
|
* |
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2015 Kenji Suzuki |
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class Installer |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
private $silent = false; |
14
|
|
|
private $app_dir = 'application'; |
15
|
|
|
private $pub_dir = 'public'; |
16
|
|
|
private $test_dir = 'tests'; |
17
|
|
|
|
18
|
|
|
public function __construct($argv) |
19
|
|
|
{ |
20
|
|
|
$this->parse_args($argv); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function parse_args($argv) |
24
|
|
|
{ |
25
|
|
|
$argc = count($argv); |
26
|
|
|
|
27
|
|
|
if ($argc === 1) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
for ($i = 1; $i <= $argc; $i++) { |
32
|
|
|
if (! isset($argv[$i])) { |
33
|
|
|
break; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
switch ($argv[$i]) { |
37
|
|
|
// php install.php -s |
38
|
|
|
case '-s': |
39
|
|
|
$this->silent = true; |
40
|
|
|
break; |
41
|
|
|
|
42
|
|
|
// php install.php -a application |
43
|
|
View Code Duplication |
case '-a': |
|
|
|
|
44
|
|
|
if (is_dir($argv[$i+1])) { |
45
|
|
|
$this->app_dir = $argv[$i+1]; |
46
|
|
|
} else { |
47
|
|
|
throw new Exception('No such directory: ' . $argv[$i+1]); |
48
|
|
|
} |
49
|
|
|
$i++; |
50
|
|
|
break; |
51
|
|
|
|
52
|
|
|
// php install.php -p public |
53
|
|
View Code Duplication |
case '-p': |
|
|
|
|
54
|
|
|
if (is_dir($argv[$i+1])) { |
55
|
|
|
$this->pub_dir = $argv[$i+1]; |
56
|
|
|
} else { |
57
|
|
|
throw new Exception('No such directory: ' . $argv[$i+1]); |
58
|
|
|
} |
59
|
|
|
$i++; |
60
|
|
|
break; |
61
|
|
|
|
62
|
|
|
default: |
63
|
|
|
throw new Exception('Unknown argument: ' . $argv[$i]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function install() |
69
|
|
|
{ |
70
|
|
|
$this->recursiveCopy( |
71
|
|
|
dirname(__FILE__) . '/application/tests', |
72
|
|
|
$this->app_dir . '/' . $this->test_dir |
73
|
|
|
); |
74
|
|
|
$this->fixPath(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Fix paths in Bootstrap.php |
79
|
|
|
*/ |
80
|
|
|
private function fixPath() |
81
|
|
|
{ |
82
|
|
|
$file = $this->app_dir . '/' . $this->test_dir . '/Bootstrap.php'; |
83
|
|
|
$contents = file_get_contents($file); |
84
|
|
|
|
85
|
|
|
if (! file_exists('system')) { |
86
|
|
|
if (file_exists('vendor/codeigniter/framework/system')) { |
87
|
|
|
$contents = str_replace( |
88
|
|
|
'$system_path = \'../../system\';', |
89
|
|
|
'$system_path = \'../../vendor/codeigniter/framework/system\';', |
90
|
|
|
$contents |
91
|
|
|
); |
92
|
|
|
} else { |
93
|
|
|
throw new Exception('Can\'t find "system" folder.'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (! file_exists('index.php')) { |
98
|
|
|
if (file_exists($this->pub_dir . '/index.php')) { |
99
|
|
|
// CodeIgniter 3.0.6 and after |
100
|
|
|
$contents = str_replace( |
101
|
|
|
"define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);", |
102
|
|
|
"define('FCPATH', realpath(dirname(__FILE__).'/../../'. $this->pub_dir).DIRECTORY_SEPARATOR);", |
103
|
|
|
$contents |
104
|
|
|
); |
105
|
|
|
// CodeIgniter 3.0.5 and before |
106
|
|
|
$contents = str_replace( |
107
|
|
|
"define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');", |
108
|
|
|
"define('FCPATH', realpath(dirname(__FILE__).'/../../' . $this->pub_dir).'/');", |
109
|
|
|
$contents |
110
|
|
|
); |
111
|
|
|
} elseif (file_exists($this->app_dir . '/public/index.php')) { |
112
|
|
|
// CodeIgniter 3.0.6 and after |
113
|
|
|
$contents = str_replace( |
114
|
|
|
"define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);", |
115
|
|
|
"define('FCPATH', realpath(dirname(__FILE__).'/../public').DIRECTORY_SEPARATOR);", |
116
|
|
|
$contents |
117
|
|
|
); |
118
|
|
|
// CodeIgniter 3.0.5 and before |
119
|
|
|
$contents = str_replace( |
120
|
|
|
"define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');", |
121
|
|
|
"define('FCPATH', realpath(dirname(__FILE__).'/../public').'/');", |
122
|
|
|
$contents |
123
|
|
|
); |
124
|
|
|
if ($this->app_dir != 'application') { |
125
|
|
|
$contents = str_replace( |
126
|
|
|
"\$application_folder = '../../application';", |
127
|
|
|
"\$application_folder = '../../{$this->app_dir}';", |
128
|
|
|
$contents |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} else { |
132
|
|
|
throw new Exception('Can\'t find "index.php".'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
file_put_contents($file, $contents); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function update() |
140
|
|
|
{ |
141
|
|
|
$target_dir = $this->app_dir . '/' . $this->test_dir . '/_ci_phpunit_test'; |
142
|
|
|
$this->recursiveUnlink($target_dir); |
143
|
|
|
$this->recursiveCopy( |
144
|
|
|
dirname(__FILE__) . '/application/tests/_ci_phpunit_test', |
145
|
|
|
$target_dir |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Recursive Copy |
151
|
|
|
* |
152
|
|
|
* @param string $src |
153
|
|
|
* @param string $dst |
154
|
|
|
*/ |
155
|
|
|
private function recursiveCopy($src, $dst) |
156
|
|
|
{ |
157
|
|
|
@mkdir($dst, 0755); |
|
|
|
|
158
|
|
|
|
159
|
|
|
$iterator = new \RecursiveIteratorIterator( |
160
|
|
|
new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), |
161
|
|
|
\RecursiveIteratorIterator::SELF_FIRST |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
foreach ($iterator as $file) { |
165
|
|
|
if ($file->isDir()) { |
166
|
|
|
@mkdir($dst . '/' . $iterator->getSubPathName()); |
|
|
|
|
167
|
|
|
} else { |
168
|
|
|
$success = copy($file, $dst . '/' . $iterator->getSubPathName()); |
169
|
|
|
if ($success) { |
170
|
|
|
if (! $this->silent) { |
171
|
|
|
echo 'copied: ' . $dst . '/' . $iterator->getSubPathName() . PHP_EOL; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Recursive Unlink |
180
|
|
|
* |
181
|
|
|
* @param string $dir |
182
|
|
|
*/ |
183
|
|
View Code Duplication |
private function recursiveUnlink($dir) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
$iterator = new \RecursiveIteratorIterator( |
186
|
|
|
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), |
187
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
foreach ($iterator as $file) { |
191
|
|
|
if ($file->isDir()) { |
192
|
|
|
rmdir($file); |
193
|
|
|
} else { |
194
|
|
|
unlink($file); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
rmdir($dir); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.