-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreatephar.php
57 lines (54 loc) · 2.78 KB
/
createphar.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
<?php
require "vendor/autoload.php";
$composer = \grinfeld\phpjsonable\parsers\json\Json::decode(new \grinfeld\phpjsonable\utils\streams\StringInputStream(file_get_contents("composer.json")));
$name = preg_replace('/[\\/]/', '_', $composer["name"]);
$sources = array();
if (isset($composer["autoload"]) && isset($composer["autoload"]["psr-4"])) {
$sources = $composer["autoload"]["psr-4"];
}
$pharName = $name . '.phar';
$phar = new Phar($pharName);
$dirName = dirname(__FILE__);
foreach ($sources as $suffix => $src) {
$suffix = preg_replace('/[\\\\]/', '/', $suffix);
$src = preg_replace('/[\\\\]/', '/', $src);
$dir = $dirName . "/" . $src;
$rp = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($rp as $file) {
if ($file->isFile() && $file->getFilename() != ".." && $file->getFilename() != ".") {
$pathBefore = substr($file->getPath(), 0, strlen($dir));
$pathAfter = substr($file->getPath(), strlen($dir) + 1);
$pathTo = "/" . $suffix . ($pathAfter !== false ? ($pathAfter . "/") : "") . $file->getFilename();
$phar->addFromString($pathTo, file_get_contents($file->getPath() . "/" . $file->getFilename()));
}
}
}
if (isset($composer["require"])) {
foreach ($composer["require"] as $library => $ver) {
if (file_exists($dirName . "/vendor/" . $library)) {
$dir = $dirName . "/vendor/" . $library . "/src";
$rp = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
}
foreach ($rp as $file) {
if ($file->isFile() && $file->getFilename() != ".." && $file->getFilename() != ".") {
$pathBefore = substr($file->getPath(), 0, strlen($dir));
$pathAfter = substr($file->getPath(), strlen($dir) + 1);
$pathTo = "/" . $suffix . ($pathAfter !== false ? ($pathAfter . "/") : "") . $file->getFilename();
$phar->addFromString($pathTo, file_get_contents($file->getPath() . "/" . $file->getFilename()));
}
}
}
}
$stubSource = "<?php\r\n" .
"class TMLoader {\r\n" .
" public static function get() {\r\n" .
" spl_autoload_register(function (\$class) {\r\n" .
" \$fileName = \"phar://$pharName/\" . \$class . \".php\";\r\n" .
" if (file_exists(\$fileName)) {\r\n" .
" include_once (\$fileName);\r\n" .
" }\r\n" .
" })\r\n;" .
" }\r\n" .
"}";
$phar->addFromString("TMLoader.php", $stubSource);
$phar->setStub($phar->createDefaultStub('TMLoader.php'));