Create packer with recursive inclusion and deduplication
This commit is contained in:
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;
|
||||
}
|
Reference in New Issue
Block a user