Skip to content

Instantly share code, notes, and snippets.

@kapitanluffy
Created November 12, 2019 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kapitanluffy/cf10f3bb84e363e742376ecc90029a14 to your computer and use it in GitHub Desktop.
Save kapitanluffy/cf10f3bb84e363e742376ecc90029a14 to your computer and use it in GitHub Desktop.
Compiles classes into a single giant package file
<?php
/**
* Compiles all files into one giant package file
*
* Usage php compiler.php path/directory/PackageFoo
*
* We assume that your package is under a package folder. This will be used as the package file name
* e.g. path/directory/PackageFoo/
*
* Other things to note:
* - All namespaces are commented out
* - All use statements are commented out
*/
function rglob($directory) {
$files = glob($directory . '/*');
$result = [];
if (!$files) return $result;
foreach ($files as $file) {
if (is_dir($file)) {
$subfiles = rglob($file);
$result = array_merge($result, $subfiles);
continue;
}
$result[] = $file;
}
return $result;
}
function getContents($directory) {
$files = rglob($directory);
$contents = [];
$spaces = [];
$uses = [];
foreach ($files as $file) {
if (preg_match("#\.package.php$#", $file) != false) continue;
if (preg_match("#\.php$#", $file) == false) continue;
if ($content = file_get_contents($file)) {
$content = preg_replace("#^\<\?php#", "", $content);
if (preg_match_all("#use (.+);#i", (string) $content, $match)) {
$content = preg_replace("#use (.+);\n#i", "", (string) $content);
$uses = array_merge($uses, $match[0]);
}
if (preg_match_all("#namespace (.+);#i", (string) $content, $match)) {
$content = preg_replace("#namespace (.+);\n\n#i", "", (string) $content);
$spaces = array_merge($spaces, $match[0]);
}
$content = trim((string) $content);
$content = "// $file\n$content";
$contents[$file] = $content;
}
}
$uses = array_unique($uses);
sort($uses);
$uses = array_map(function($v) {
return "// $v";
}, $uses);
$spaces = array_unique($spaces);
sort($spaces);
$spaces = array_map(function($v) {
return "// $v";
}, $spaces);
return ['contents' => $contents, 'namespaces' => $spaces, 'uses' => $uses];
}
function getPackageMeta($directory) {
$directory = trim($directory, '/\\');
$directory = (string) preg_replace("#[/\\\\]#", DIRECTORY_SEPARATOR, $directory);
$dirs = explode(DIRECTORY_SEPARATOR, $directory);
$packageName = array_pop($dirs);
return ['directory' => $directory, 'package_name' => $packageName, 'namespace' => $packageName];
}
function createPackageFile($package, $contents) {
$headers = [];
$headers[] = "namespace {$package['namespace']};";
$headers[] = implode("\n", $contents['namespaces']);
$headers[] = implode("\n", $contents['uses']);
$header = implode("\n\n", $headers);
$contents = implode("\n", $contents['contents']);
$contents = "<?php\n\n$header\n\n$contents";
$file = $package['directory'] . DIRECTORY_SEPARATOR . $package['package_name'] . ".package.php";
file_put_contents($file, $contents);
}
$directory = isset($argv[1]) ? $argv[1] : __DIR__;
$package = getPackageMeta($directory);
$contents = getContents($package['directory']);
createPackageFile($package, $contents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment