forked from Shaked/php.tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.php
85 lines (79 loc) · 2.56 KB
/
build.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
<?php
if (ini_get('phar.readonly')) {
fwrite(STDERR, 'Please run build with -dphar.readonly=0' . PHP_EOL);
exit(255);
}
include 'vendor/dericofilho/csp/csp.php';
include "Core/FormatterPass.php";
class Build extends FormatterPass {
public function candidate($source, $found_tokens) {
return true;
}
public function format($source) {
$this->tkns = SplFixedArray::fromArray(token_get_all($source));
$this->code = '';
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->get_token($token);
$this->ptr = $index;
switch ($id) {
case T_REQUIRE:
list($id, $text) = $this->walk_until(T_CONSTANT_ENCAPSED_STRING);
$included = token_get_all(file_get_contents(str_replace(['"', "'"], '', $text)));
if (T_OPEN_TAG == $included[0][0]) {
unset($included[0]);
}
while (list(, $token) = each($included)) {
list($id, $text) = $this->get_token($token);
$this->append_code($text);
}
break;
default:
$this->append_code($text);
}
}
return $this->code;
}
}
$pass = new Build();
$chn = make_channel();
$chn_done = make_channel();
$workers = 2;
echo "Starting ", $workers, " workers...", PHP_EOL;
for ($i = 0; $i < $workers; ++$i) {
cofunc(function ($pass, $chn, $chn_done) {
while (true) {
$target = $chn->out();
if (empty($target)) {
break;
}
echo $target, PHP_EOL;
file_put_contents($target . '.php', $pass->format(file_get_contents($target . '.src.php')));
chmod($target . '.php', 0755);
file_put_contents($target . '.stub.php', $pass->format(file_get_contents($target . '.stub.src.php')));
chmod($target . '.stub.php', 0755);
}
$chn_done->in('done');
}, $pass, $chn, $chn_done);
}
$targets = ['fmt', 'refactor'];
foreach ($targets as $target) {
$chn->in($target);
}
for ($i = 0; $i < $workers; ++$i) {
$chn->in(null);
}
for ($i = 0; $i < $workers; ++$i) {
$chn_done->out();
}
$chn->close();
$chn_done->close();
echo 'Building PHARs...';
$phars = ['fmt', 'refactor'];
foreach ($phars as $target) {
file_put_contents($target . '.stub.php', '<?php $in_phar = true;' . "\n" . str_replace('#!/usr/bin/env php' . "\n" . '<?php', '', file_get_contents($target . '.stub.php')));
$phar = new Phar($target . '.phar', FilesystemIterator::CURRENT_AS_FILEINFO|FilesystemIterator::KEY_AS_FILENAME, $target . '.phar');
$phar[$target . ".stub.php"] = file_get_contents($target . '.stub.php');
$phar->setStub('#!/usr/bin/env php' . "\n" . $phar->createDefaultStub($target . '.stub.php'));
file_put_contents($target . ".phar.sha1", sha1_file($target . '.phar'));
}
echo 'done', PHP_EOL;