|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VoidBuilder; |
|
4
|
|
|
|
|
5
|
|
|
if (!defined ('VoidBuilder\ENGINE_DIR') && defined ('VoidEngine\ENGINE_DIR')) |
|
6
|
|
|
define ('VoidBuilder\ENGINE_DIR', \VoidEngine\ENGINE_DIR); |
|
7
|
|
|
|
|
8
|
|
|
class Builder |
|
9
|
|
|
{ |
|
10
|
|
|
public $appDir; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct (string $appDir) |
|
13
|
|
|
{ |
|
14
|
|
|
if (!is_dir ($appDir)) |
|
15
|
|
|
throw new \Exception ('Wrong $appDir param'); |
|
16
|
|
|
|
|
17
|
|
|
$this->appDir = $appDir; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function build (string $outputDir, string $iconPath = null, bool $union = true): array |
|
21
|
|
|
{ |
|
22
|
|
|
\VoidEngine\dir_clean ($outputDir .'/build'); |
|
|
|
|
|
|
23
|
|
|
\VoidEngine\dir_copy (CORE_DIR, $outputDir .'/build'); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
unlink ($outputDir .'/build/script.php'); |
|
26
|
|
|
unlink ($outputDir .'/build/VoidCore.exe'); |
|
27
|
|
|
|
|
28
|
|
|
\VoidEngine\dir_clean ($outputDir .'/build/qero-packages'); |
|
29
|
|
|
\VoidEngine\dir_copy (dirname ($this->appDir) .'/qero-packages', $outputDir .'/build/qero-packages'); |
|
30
|
|
|
\VoidEngine\dir_delete ($outputDir .'/build/qero-packages/winforms-php/VoidFramework'); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
return \VoidEngine\EngineAdditions::compile ($outputDir .'/build/app.exe', $iconPath ?? dirname (__DIR__) .'/system/Icon.ico', self::optimizeCode (\VoidEngine\str_replace_assoc (file_get_contents (dirname (__DIR__) .'/system/preset.php'), [ |
|
|
|
|
|
|
33
|
|
|
'%VoidEngine%' => self::generateCode (self::getReferences (ENGINE_DIR .'/VoidEngine.php')), |
|
34
|
|
|
'%APP%' => base64_encode (gzdeflate (serialize ($union ? self::getFiles ($this->appDir) : []), 9)) |
|
35
|
|
|
]))/*, null, null, null, null, null, '', '', null, null*/); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function generateCode (array $references, bool $removeNamespaces = true): string |
|
39
|
|
|
{ |
|
40
|
|
|
$code = "/*\n\n\t". join ("\n\t", explode ("\n", file_get_contents (dirname (ENGINE_DIR) .'/license.txt'))) ."\n\n*/\n\n"; |
|
41
|
|
|
|
|
42
|
|
|
foreach ($references as $path) |
|
43
|
|
|
$code .= join (array_slice (array_map (function ($line) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
return substr ($line, 0, 7) != 'require' ? $line : ''; |
|
46
|
|
|
}, file ($path)), 1)); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
return $removeNamespaces ? |
|
49
|
|
|
preg_replace ('/'. "\n" .'namespace [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*;'. "\n" .'/', "\n\n", $code) : $code; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function getReferences (string $file, bool $parseExtensions = true): array |
|
53
|
|
|
{ |
|
54
|
|
|
$references = []; |
|
55
|
|
|
|
|
56
|
|
|
foreach (file ($file) as $line) |
|
57
|
|
|
if (substr ($line, 0, 7) == 'require') |
|
58
|
|
|
try |
|
59
|
|
|
{ |
|
60
|
|
|
$begin = strpos ($line, "'"); |
|
61
|
|
|
$end = strrpos ($line, "'") - $begin + 1; |
|
62
|
|
|
|
|
63
|
|
|
$references = array_merge ($references, self::getReferences (dirname ($file) .'/'. eval ('namespace VoidEngine; return '. substr ($line, $begin, $end) .';'), false)); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
catch (\Throwable $e) |
|
67
|
|
|
{ |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($parseExtensions) |
|
72
|
|
|
if (is_dir (ENGINE_DIR .'/extensions') && is_array ($exts = scandir (ENGINE_DIR .'/extensions'))) |
|
73
|
|
|
foreach ($exts as $ext) |
|
74
|
|
|
if (is_dir (ENGINE_DIR .'/extensions/'. $ext) && file_exists ($ext = ENGINE_DIR .'/extensions/'. $ext .'/main.php')) |
|
75
|
|
|
$references = array_merge ($references, self::getReferences ($ext, false)); |
|
76
|
|
|
|
|
77
|
|
|
$references[] = $file; |
|
78
|
|
|
|
|
79
|
|
|
return $references; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public static function getFiles (string $path, string $prefixBlacklist = null, array $files = [], int $originalPathLength = -1): array |
|
83
|
|
|
{ |
|
84
|
|
|
if ($originalPathLength == -1) |
|
85
|
|
|
$originalPathLength = strlen (dirname ($path)) + 1; |
|
86
|
|
|
|
|
87
|
|
|
$len = strlen ($prefixBlacklist); |
|
88
|
|
|
|
|
89
|
|
|
foreach (array_slice (scandir ($path), 2) as $name) |
|
|
|
|
|
|
90
|
|
|
if ($prefixBlacklist === null || substr ($path .'/'. $name, $originalPathLength, $len) != $prefixBlacklist) |
|
91
|
|
|
{ |
|
92
|
|
|
if (is_dir ($file = $path .'/'. $name)) |
|
93
|
|
|
$files = self::getFiles ($file, $prefixBlacklist, $files, $originalPathLength); |
|
94
|
|
|
|
|
95
|
|
|
else $files[substr ($file, $originalPathLength)] = file_get_contents ($file); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $files; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function optimizeCode (string $code): string |
|
102
|
|
|
{ |
|
103
|
|
|
$tokens = token_get_all ('<?php '. $code); |
|
104
|
|
|
$return = ''; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($tokens as $id => $token) |
|
107
|
|
|
if (is_string ($token)) |
|
108
|
|
|
$return .= $token; |
|
109
|
|
|
|
|
110
|
|
|
else |
|
111
|
|
|
{ |
|
112
|
|
|
list ($token_id, $text) = $token; |
|
113
|
|
|
|
|
114
|
|
|
switch ($token_id) |
|
115
|
|
|
{ |
|
116
|
|
|
case T_COMMENT: |
|
117
|
|
|
case T_DOC_COMMENT: |
|
118
|
|
|
break; |
|
119
|
|
|
|
|
120
|
|
|
case T_WHITESPACE: |
|
121
|
|
|
if (!isset ($tokens[$id + 1]) || !is_array ($tokens[$id + 1]) || $tokens[$id + 1][0] != T_WHITESPACE) |
|
122
|
|
|
$return .= ' '; |
|
123
|
|
|
|
|
124
|
|
|
break; |
|
125
|
|
|
|
|
126
|
|
|
default: |
|
127
|
|
|
$return .= $text; |
|
128
|
|
|
|
|
129
|
|
|
break; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return substr ($return, 6); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|