-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.php
174 lines (157 loc) · 4 KB
/
install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Step 1: Clone this repository
* Step 2: Modify project's parameters in line 157 of this script
* Step 3: Run this script
*/
/**
* Class Installtion
*/
class Installation
{
/**
* @var array
*/
protected $parameters = [];
/**
* @var array
*/
protected $textFiles = [
'.gitattributes',
'.gitignore',
'CHANGELOG.md',
'LICENSE.md',
'README.md',
'composer.json',
'phpcs.xml.dist',
'phpunit.xml.dist',
'tests/bootstrap.php'
];
/**
* @var array
*/
protected $unusedFiles = [
'bin/empty',
'src/empty',
'.git',
'install.php',
];
/**
* A string compiler
*
* @param array $matches
* @return string
*/
protected function compiler(array $matches)
{
if (isset($matches[1]) && isset($this->parameters[$matches[1]])) {
if (is_string($this->parameters[$matches[1]])) {
return quotemeta($this->parameters[$matches[1]]);
}
// if it is integer or boolean or array or other
return json_encode($this->parameters[$matches[1]]);
}
return $matches[0];
}
/**
* Compile a file
*
* @param string $file
* @return boolean
*/
protected function compileFile($file)
{
if (false === ($content = file_get_contents($file))) {
return false;
}
$count = 0;
$newContent = preg_replace_callback('/\{\{([A-Z]+)\}\}/', [$this, 'compiler'], $content, -1, $count);
if ($count > 0 && false === file_put_contents($file, $newContent)) {
return false;
}
return true;
}
/**
* Remove directory recursively
*
* @param string $dir
* @return boolean
*/
protected function rmdir($dir)
{
try {
$iterator = new \DirectoryIterator($dir);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isDot()) {
continue;
}
if ($fileinfo->isDir() && !$this->rmdir($fileinfo->getPathname())) {
return false;
}
if ($fileinfo->isFile() && !unlink($fileinfo->getPathname())) {
return false;
}
}
} catch (\Exception $e) {
return false;
}
return rmdir($dir);
}
/**
* Constructor
* @param array $parameters
*/
public function __construct(array $parameters)
{
$this->parameters = $parameters;
}
/**
* Run installation
*/
public function run()
{
foreach ($this->textFiles as $file) {
$sourceFile = __DIR__ . DIRECTORY_SEPARATOR . $file;
if (!is_file($sourceFile) || !is_readable($sourceFile)) {
continue;
}
if (!$this->compileFile($sourceFile)) {
echo "Write to file '{$sourceFile}' failure.\n";
exit(1);
}
}
foreach ($this->unusedFiles as $file) {
$sourceFile = __DIR__ . DIRECTORY_SEPARATOR . $file;
if (is_dir($sourceFile)) {
$deleted = $this->rmdir($sourceFile);
} elseif (is_file($sourceFile)) {
$deleted = unlink($sourceFile);
} else {
$deleted = true;
}
if (!$deleted) {
echo "Delete file '{$sourceFile}' failure.\n";
exit(1);
}
}
echo 'Done';
exit(0);
}
}
/**
* Parameters of project
* Please change this variable
*/
$parameters = [
'PROJECT' => 'lemonphp/bee',
'TYPE' => 'library',
'PACKAGE' => 'lemonphp/bee',
'NAMESPACE' => 'Lemon\Bee',
'DESCRIPTION' => 'A project of LemonPHP Team, it named Bee',
'KEYWORDS' => ['demo', 'bee'],
];
/**
* Installing
*/
$cmd = new Installation($parameters);
$cmd->run();