<?php

$out = 'leproxy.out.php';

system('composer install --no-dev --classmap-authoritative');
$classes = require __DIR__ . '/vendor/composer/autoload_classmap.php';
$includes = require __DIR__ . '/vendor/composer/autoload_files.php';
system('composer install');

echo 'Loading ' . count($classes) . ' classes to determine load order...';

// register autoloader which remembers load order
$ordered = array();
spl_autoload_register(function ($name) use ($classes, &$ordered) {
    require $classes[$name];
    $ordered[$name] = $classes[$name];
});

// use actual include file instead of include wrapper
foreach ($includes as $i => $path) {
    $includes[$i] = str_replace('_include.php', '.php', $path);

    require $path;
}

// load each class (and its superclasses once) into memory
foreach ($classes as $class => $path) {
    class_exists($class, true);
}

echo ' DONE' . PHP_EOL;

// resulting list of all includes and ordered class list
$files = array_merge($includes, $ordered);
echo 'Concatenating ' . count($files) . ' files into ' . $out . '...';
system('head -n2 leproxy.php > ' . escapeshellarg($out));

foreach ($files as $file) {
    $file = substr($file, strlen(__DIR__) + 1);
    system('(echo "# ' . $file . '"; grep -v "<?php" ' . escapeshellarg($file) . ') >> ' . escapeshellarg($out));
}

$file = 'leproxy.php';
system('(echo "# ' . $file . '"; egrep -v "^<\?php|^require " ' . escapeshellarg($file) . ') >> ' . escapeshellarg($out));
echo ' DONE' . PHP_EOL;

<?php

system('composer install --no-dev --classmap-authoritative');
$files = require __DIR__ . '/vendor/composer/autoload_classmap.php';
system('composer install');

//system('cat leproxy.php > leproxy.out.php');
system('egrep -v "^require " leproxy.php > leproxy.out.php');

foreach ($files as $file) {
    $file = substr($file, strlen(__DIR__) + 1);
    //system('cat ' . escapeshellarg($file) . ' >> leproxy.out.php');
    //system('grep -v "<?php" ' . escapeshellarg($file) . ' >> leproxy.out.php');
    system('(echo "# ' . $file . '"; grep -v "<?php" ' . escapeshellarg($file) . ') >> leproxy.out.php');
}

--- 
+++ 
@@ -1,15 +1,45 @@
 <?php
 
+$out = 'leproxy.out.php';
+
 system('composer install --no-dev --classmap-authoritative');
-$files = require __DIR__ . '/vendor/composer/autoload_classmap.php';
+$classes = require __DIR__ . '/vendor/composer/autoload_classmap.php';
+$includes = require __DIR__ . '/vendor/composer/autoload_files.php';
 system('composer install');
 
-//system('cat leproxy.php > leproxy.out.php');
-system('egrep -v "^require " leproxy.php > leproxy.out.php');
+echo 'Loading ' . count($classes) . ' classes to determine load order...';
+
+// register autoloader which remembers load order
+$ordered = array();
+spl_autoload_register(function ($name) use ($classes, &$ordered) {
+    require $classes[$name];
+    $ordered[$name] = $classes[$name];
+});
+
+// use actual include file instead of include wrapper
+foreach ($includes as $i => $path) {
+    $includes[$i] = str_replace('_include.php', '.php', $path);
+
+    require $path;
+}
+
+// load each class (and its superclasses once) into memory
+foreach ($classes as $class => $path) {
+    class_exists($class, true);
+}
+
+echo ' DONE' . PHP_EOL;
+
+// resulting list of all includes and ordered class list
+$files = array_merge($includes, $ordered);
+echo 'Concatenating ' . count($files) . ' files into ' . $out . '...';
+system('head -n2 leproxy.php > ' . escapeshellarg($out));
 
 foreach ($files as $file) {
     $file = substr($file, strlen(__DIR__) + 1);
-    //system('cat ' . escapeshellarg($file) . ' >> leproxy.out.php');
-    //system('grep -v "<?php" ' . escapeshellarg($file) . ' >> leproxy.out.php');
-    system('(echo "# ' . $file . '"; grep -v "<?php" ' . escapeshellarg($file) . ') >> leproxy.out.php');
+    system('(echo "# ' . $file . '"; grep -v "<?php" ' . escapeshellarg($file) . ') >> ' . escapeshellarg($out));
 }
+
+$file = 'leproxy.php';
+system('(echo "# ' . $file . '"; egrep -v "^<\?php|^require " ' . escapeshellarg($file) . ') >> ' . escapeshellarg($out));
+echo ' DONE' . PHP_EOL;
Adjust class load order according to autoloader