-
Notifications
You must be signed in to change notification settings - Fork 0
/
Builder.php
285 lines (267 loc) · 9.27 KB
/
Builder.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
namespace Webdevvie\Pakket;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class Builder
* @package Webdevvie\Pakket
*/
class Builder
{
/**
* @var OutputInterface
*/
private $output;
/**
* Builder constructor.
* @param OutputInterface $output
*/
public function __construct(OutputInterface $output)
{
$this->output = $output;
}
/**
* @param string $type
* @param string $path
* @param string $targetPath
* @param array $config
* @return void
*/
private function handleRunCommands($type, $path, $targetPath, array $config = [])
{
$var = $type . 'Run';
if (isset($config[$var]) && is_array($config[$var])) {
$runs = $config[$var];
foreach ($runs as $cmd) {
if (!is_string($cmd)) {
continue;
}
$cmd = str_replace("{{PHPBIN}}", PHP_BINARY, $cmd);
$cmd = str_replace("{{TARGETPATH}}", $targetPath, $cmd);
$cmd = "cd $path; " . $cmd;
$this->output->writeln("executing $cmd");
$this->output->writeln(shell_exec($cmd));
}
}
}
/**
* @param string $path
* @param string $targetPath
* @param null|array $config
* @return boolean
*/
public function build($path, $targetPath = '', $config = null)
{
$d = DIRECTORY_SEPARATOR;
if (!is_dir($path)) {
$this->output->writeln("<error>No valid path defined defined!</error>");
}
$path = realpath($path);
$defaultConfig = [
"gzip" => false,
"buffer" => false,
"stubFile" => __DIR__ . '/defaultStub',
"stub" => "",
"sources" => ["" => ""],
"sourcePath" => $path,
"exclude" => [],
"parse" => [],
"vars" => [
"PHARFILE" => ""
]
];
if (is_null($config) || !is_array($config)) {
$config = [
'targetPath' => $targetPath
];
} elseif ($targetPath != '') {
$config['targetPath'] = $targetPath;
}
$config = array_merge($defaultConfig, $config);
$targetPath = $config['targetPath'];
$phpversion = PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;
$targetPath = str_replace("{{PHPVERSION}}", $phpversion, $targetPath);
$parts = explode($d, $targetPath);
if ($config['targetPath'] == '') {
$this->output->writeln("<error>No target file defined!</error>");
return false;
}
if (file_exists($config['targetPath'])) {
unlink($config['targetPath']);
}
$this->handleRunCommands('pre', $path, $targetPath, $config);
$filename = array_pop($parts);
$config['vars']['PHARFILE'] = $filename;
$files = [];
foreach ($config['sources'] as $source => $targetinfo) {
$this->output->writeln("Looking at source '$source'");
if (is_dir($path . $d . $source)) { //source below path
$this->getFiles($path . $d . $source, $targetinfo, $files);
} elseif (is_dir($source)) { //hard coded path
$this->getFiles($source, $targetinfo, $files);
}
}
$pharFile = new \Phar($targetPath, 0, $filename);
$pharFile->setSignatureAlgorithm(\Phar::SHA512);
if (isset($config['index'])) {
if (isset($config['indexWeb'])) {
$config['stub'] = $pharFile->createDefaultStub($config['index'], $config['indexWeb']);
} else {
$config['stub'] = $pharFile->createDefaultStub($config['index']);
}
$stub = "#!/usr/bin/env php\n" . $config['stub'];
} else {
if ($config['stub'] == '' && $config['stubFile'] != '') {
if (file_exists($path . $d . $config['stubFile'])) {
$this->output->writeln("Using stub file: " . $path . $d . $config['stubFile']);
$config['stub'] = file_get_contents($path . $d . $config['stubFile']);
} elseif (file_exists($config['stubFile'])) {
$this->output->writeln("Using stub file: " . $config['stubFile']);
$config['stub'] = file_get_contents($config['stubFile']);
} else {
$this->output->writeln("<error>No stub or stubFile defined</error>");
return false;
}
} elseif ($config['stub'] == '') {
$this->output->writeln("<error>No stub or stubFile defined</error>");
return false;
} else {
$this->output->writeln("<error>No stub or stubFile defined</error>");
}
$stub = $config['stub'];
$stub = str_replace("{{PHARFILE}}", $filename, $stub);
}
if ($config['gzip']) {
$this->output->writeln("<info>Compressing files with Gzip</info>");
$pharFile->compressFiles(\Phar::GZ);
}
if ($config['buffer']) {
$this->output->writeln("<info>Buffering</info>");
$pharFile->startBuffering();
}
ksort($files);
$queue = [];
foreach ($files as $file => $fileData) {
if (!$this->shouldKeep($file, $config)) {
continue;
}
if ($fileData['type'] == 'dir') {
$this->output->writeln("<info>Adding dir $file</info>");
$pharFile->addEmptyDir($file);
} elseif ($fileData['type'] == 'file') {
if ($this->shouldParse($file, $config)) {
$this->output->writeln("<info>Adding parsed file $file</info>");
$pharFile->addFromString(
$file,
$this->parse(file_get_contents($fileData['source']), $config['vars'])
);
} else {
$this->output->writeln("<info>Queueing file $file</info>");
$queue[$file] = $fileData['source'];
}
}
if (count($queue) >= 500) {
$this->output->writeln("<info>Adding Queued files(" . count($queue) . ")</info>");
$pharFile->buildFromIterator(
new \ArrayIterator($queue)
);
$queue = [];
}
}
if (count($queue) >= 0) {
$this->output->writeln("<info>Adding Queued files(" . count($queue) . ")</info>");
$pharFile->buildFromIterator(
new \ArrayIterator($queue)
);
$queue = [];
}
$pharFile->addFile($fileData['source'], $file);
$pharFile->addFromString('packageInfo', "Packaged with Pakket!");
$this->output->writeln("<info>Writing stub</info>");
$pharFile->setStub($stub);
if ($config['buffer']) {
$this->output->writeln("<info>Writing file</info>");
$pharFile->stopBuffering();
}
$this->handleRunCommands('post',$path, $targetPath, $config);
return true;
}
/**
* @param string $dir
* @return boolean|null
*/
private function isDirEmpty($dir)
{
return (count(scandir($dir)) == 2);
}
/**
* @param string $content
* @param array $vars
* @return mixed
*/
public function parse($content, array &$vars)
{
foreach ($vars as $var => $val) {
$content = str_replace("{{" . $var . "}}", $val, $content);
}
return $content;
}
/**
* @param string $file
* @param array $config
* @return boolean
*/
public function shouldKeep($file, array &$config)
{
foreach ($config['exclude'] as $exclude) {
if (preg_match($exclude, $file)) {
return false;
}
}
return true;
}
/**
* @param string $file
* @param array $config
* @return boolean
*/
public function shouldParse($file, array &$config)
{
foreach ($config['parse'] as $parse) {
if (preg_match($parse, $file)) {
return true;
}
}
return false;
}
/**
* @param string $source
* @param string|array $targetInfo
* @param array $files
* @return void
*/
public function getFiles($source, $targetInfo, array &$files)
{
$d = DIRECTORY_SEPARATOR;
$directory = new \RecursiveDirectoryIterator($source);
$ittr = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($ittr as $name => $obj) {
$parts = explode($d, $name);
$lp = array_pop($parts);
if ($lp == '.' || $lp == '..') {
continue;
}
if (is_dir($name)) {
if ($this->isDirEmpty($name)) {
$info = ['type' => 'dir'];
} else {
continue;
}
} else {
$info = ["type" => 'file'];
}
$target = str_replace($source, $targetInfo, $name);
$info['source'] = $name;
$files[$target] = $info;
}
}
}