Composer 不负责加载autoload.php,但您使用的任何框架都是。 PHPUnit,在你的情况下。
PHPUnit 仅加载 vendor/autoload.php,因为该文件是在 phpunit.xml 配置中引导的。
比在 composer 运行期间进行任何奇怪的注入要容易得多,只需创建您自己的测试引导文件。
如果您检查phpunit.xml,您会发现默认加载vendor/autoload.php 的引导声明:
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
创建一个新的引导文件(例如testing_bootstrap.php),除了vendor/autoload.php之外,还包括您需要的任何文件:
<?php
// testing_bootstrap.php
require 'path/to/wordpress/wp-load.php';
require 'vendor/autoload.php`;
并修改您的 phpunit.xml 文件,使其使用 此 文件来引导您的测试。
bootstrap="testing_bootstrap.php"
这样更简洁、更易于维护,并且实现了正确的结果。在执行之前加载/引导哪些文件不是作曲家的工作。
要在常规 Laravel 运行期间完成相同的操作,您需要修改 Laravel 的 entry point file,您会发现那里需要自动加载:
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../vendor/autoload.php';
如果您想在自动加载之前加载不同的文件,只需在该点之前添加适当的require 或include 语句。
例如:
require 'path/to/wordpress/wp-load.php';
require __DIR__.'/../vendor/autoload.php';
使用作曲家的files 键将根本不起作用。这些文件在vendor/composer/autoload_files.php 文件上加载,而这又在vendor/composer/autoload_real.php::getLoader 上加载,在其余的自动加载过程设置已执行之后。