Create packer with recursive inclusion and deduplication
This commit is contained in:
parent
c4845ed7c6
commit
61a4dcd370
@ -1,6 +1,10 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Very simple script, recursively parses a PHP file replacing all includes with the file, no deduplication
|
||||
*/
|
||||
|
||||
function line(string $text): void
|
||||
{
|
||||
echo $text . PHP_EOL;
|
||||
@ -57,7 +61,7 @@ $lines = replaceIncludes($fromFile);
|
||||
$toFileHandle = fopen($toFile, 'w');
|
||||
foreach ($lines as $line) {
|
||||
fwrite($toFileHandle, $line);
|
||||
chmod($toFile, 0755);
|
||||
chmod($toFile, fileperms($fromFile));
|
||||
}
|
||||
fwrite($toFileHandle, PHP_EOL);
|
||||
fclose($toFileHandle);
|
10
includes/fileReader.php
Normal file
10
includes/fileReader.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
function getAllLines(string $file): iterable
|
||||
{
|
||||
$handle = fopen($file, 'r');
|
||||
while (!feof($handle)) {
|
||||
yield fgets($handle);
|
||||
}
|
||||
fclose($handle);
|
||||
}
|
18
includes/includeBuilder.php
Normal file
18
includes/includeBuilder.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
function yieldIncludes(string $file, &$includes, int $depth = 0): iterable
|
||||
{
|
||||
if (!isset($includes[$file])) return;
|
||||
|
||||
foreach (getAllLines($file) as $line) {
|
||||
$includeFile = getLineIncludeFile($line);
|
||||
if ($includeFile) {
|
||||
yield from yieldIncludes($includeFile, $includes, $depth + 1);
|
||||
unset($includes[$includeFile]);
|
||||
} else {
|
||||
if ($depth > 0 && str_starts_with($line, '<?php')) continue;
|
||||
if (empty(trim($line))) continue;
|
||||
yield $line;
|
||||
}
|
||||
}
|
||||
}
|
21
includes/includeParser.php
Normal file
21
includes/includeParser.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
function getLineIncludeFile(string $line): false|string
|
||||
{
|
||||
preg_match("/include\s+'([^']+\.php)'/", $line, $matches);
|
||||
if (count($matches) === 2) {
|
||||
return $matches[1];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function buildIncludes(string $file, array &$includes): void
|
||||
{
|
||||
foreach (getAllLines($file) as $line) {
|
||||
$includeFile = getLineIncludeFile($line);
|
||||
if ($includeFile) {
|
||||
buildIncludes($includeFile, $includes);
|
||||
}
|
||||
}
|
||||
$includes[$file] = true;
|
||||
}
|
6
includes/io.php
Normal file
6
includes/io.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
function line(string $text): void
|
||||
{
|
||||
echo $text . PHP_EOL;
|
||||
}
|
33
packer.php
Executable file
33
packer.php
Executable file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
include 'includes/fileReader.php';
|
||||
include 'includes/io.php';
|
||||
include 'includes/includeBuilder.php';
|
||||
include 'includes/includeParser.php';
|
||||
|
||||
if (count($argv) < 3) {
|
||||
line('Usage: ' . __FILE__ . ' <inFile> <outFile>');
|
||||
exit(1);
|
||||
}
|
||||
$fromFile = $argv[1];
|
||||
if (!file_exists($fromFile)) {
|
||||
line('File not found: ' . $fromFile);
|
||||
exit(1);
|
||||
}
|
||||
$toFile = $argv[2];
|
||||
if (file_exists($toFile)) {
|
||||
line('File already exists: ' . $toFile);
|
||||
unlink($toFile);
|
||||
}
|
||||
|
||||
$includes = [];
|
||||
buildIncludes($fromFile, $includes);
|
||||
$lines = yieldIncludes($fromFile, $includes);
|
||||
|
||||
$toFileHandle = fopen($toFile, 'w');
|
||||
foreach ($lines as $line) {
|
||||
fwrite($toFileHandle, $line);
|
||||
chmod($toFile, fileperms($fromFile));
|
||||
}
|
||||
fclose($toFileHandle);
|
6
test.php
6
test.php
@ -1,6 +0,0 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
include 'test.include.php';
|
||||
|
||||
echo 'test' . PHP_EOL;
|
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
|
||||
include 'test2.include.php';
|
||||
include 'test.include2.php';
|
||||
|
||||
echo 'test.include' . PHP_EOL;
|
3
test/test.include2.php
Normal file
3
test/test.include2.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
echo 'test.include2' . PHP_EOL;
|
11
test/test.php
Normal file
11
test/test.php
Normal file
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
include 'test.include.php';
|
||||
include 'test2.include.php';
|
||||
include 'test2.include.php';
|
||||
include 'test.include.php';
|
||||
include 'test2.include.php';
|
||||
include 'test2.include.php';
|
||||
|
||||
echo 'test' . PHP_EOL;
|
@ -1,3 +0,0 @@
|
||||
<?php
|
||||
|
||||
echo 'test2.include' . PHP_EOL;
|
Loading…
x
Reference in New Issue
Block a user