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