简体   繁体   中英

How to load a file before composer and /vendor/autoload.php?

I need to load Wordpress wp-load.php before the vendor/autoload.php and Laravel.

I can update in the public/index.php but at PHPUnit level the vendor/bin/phpunit loads vendor/autoload.php before the wp-load.php .

Is it possible to force composer to load a file before anything else ?

I tried

{
    "autoload" : {
         "files" : ["public/wordpress/wp-load.php"]
    }
}

But it doesn't seems to work as composer loads Laravel before wordpress...

The only ugly fix I found is to manually load wp-load in the vendor/autoload.php file but I need to do that at every composer update.

Composer is not responsible for loading autoload.php , but the whatever framework you are using is. PHPUnit, in your case.

PHPUnit only loads vendor/autoload.php because that file is bootstrapped in phpunit.xml configuration.

Much easier than doing any weird injection during composer run is simply to create your own testing bootstrap file.

If you check phpunit.xml you'll find a bootstrap declaration which by default loads vendor/autoload.php :

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
stopOnFailure="false">

Create a new bootstrap file (eg testing_bootstrap.php ) and there include whatever files you need in addition to vendor/autoload.php :

<?php
// testing_bootstrap.php

require 'path/to/wordpress/wp-load.php';
require 'vendor/autoload.php`;

And modify your phpunit.xml file so it uses this file to bootstrap your tests.

bootstrap="testing_bootstrap.php"

This is cleaner and more maintainable, and accomplishes the right result. Which files are loaded/bootstrapped before execution is not composer's job.


To accomplish the same during a regular Laravel run, you'll need to modify Laravel's entry point file , you'll find there that autoload is required there:

/*
|--------------------------------------------------------------------------
| 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';

If you want to load a different file before autoload, simply add the appropriate require or include statement before that point.

Eg:

require 'path/to/wordpress/wp-load.php';
require __DIR__.'/../vendor/autoload.php';

Using the files key of composer will simply not work . These files are loaded on vendor/composer/autoload_files.php file, and this in turn is loaded on vendor/composer/autoload_real.php::getLoader , after the rest of the autoloading process setup has been performed.


我目前发现的解决方法是在作曲家自动加载转储之后立即应用php脚本(在脚本中,post-autoload-dump)=>它可以解决问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM