doumor942473 2019-03-22 09:09
浏览 1006

如何在composer和/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.

  • 写回答

2条回答 默认 最新

  • dream198731 2019-03-22 10:43
    关注

    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 (e.g. 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.

    E.g.:

    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.


    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。