This is a really obnoxious problem. I filed a feature request for composer: https://github.com/composer/composer/issues/6768
There should be a way to specify the order of operations of the autoloading so that your custom "files" can be loaded before any of the classes from the "require" or "require-dev" sections; any solution that requires you to edit a 3rd party package inside of vendor/ is hacky at best, but at present, I don't think there are any other good alternatives.
The best I could come up with is to use a script to modify the vendor/autoload.php so that it forcefully includes your files BEFORE it includes any of the autoload classes. Here's my modify_autoload.php:
<?php
/**
* Updates the vendor/autoload.php so it manually includes any files specified in composer.json's files array.
* See https://github.com/composer/composer/issues/6768
*/
$composer = json_decode(file_get_contents('composer.json'));
$files = (property_exists($composer, 'files')) ? $composer->files : [];
if (!$files) {
print "No files specified -- nothing to do.\n";
exit;
}
$patch_string = '';
foreach ($files as $f) {
$patch_string .= "require_once __DIR__ . '/../{$f}';\n";
}
$patch_string .= "require_once __DIR__ . '/composer/autoload_real.php';";
// Read and re-write the vendor/autoload.php
$autoload = file_get_contents(__DIR__ . '/vendor/autoload.php');
$autoload = str_replace("require_once __DIR__ . '/composer/autoload_real.php';", $patch_string, $autoload);
file_put_contents(__DIR__ . '/vendor/autoload.php', $autoload);
You can run this manually, or you can have composer run it by adding it to your composer.json scripts:
{
// ...
"scripts": {
"post-autoload-dump": [
"php modify_autoload.php"
]
}
// ...
}