Composer:指定autoload_files需要订单

Che*_*Lin 9 php laravel composer-php kint

Laravel的辅助功能有if ( ! function_exists('xx'))保护作用.

我可以指定的顺序autoload_files,并让Kint.class.php需要之前helpers.php

return array( 
    $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php',
    $vendorDir . '/raveren/kint/Kint.class.php',
);
Run Code Online (Sandbox Code Playgroud)

Eve*_*ett 6

这真是一个令人讨厌的问题。我提交了作曲家的功能请求:https ://github.com/composer/composer/issues/6768

应该有一种方法来指定自动加载的操作顺序,以便您的自定义“文件”可以在“require”或“require-dev”部分中的任何类之前加载;任何需要您在供应商/内部编辑第三方包的解决方案充其量都是hacky,但目前,我认为没有任何其他好的替代方案。

我能想到的最好办法是使用脚本修改vendor/autoload.php,以便它在包含任何自动加载类之前强制包含您的文件。这是我的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);
Run Code Online (Sandbox Code Playgroud)

您可以手动运行它,也可以通过将其添加到您的composer.json脚本中来让composer运行它:

{
 // ... 
  "scripts": {
    "post-autoload-dump": [
      "php modify_autoload.php"
    ]
  }
 // ...
}
Run Code Online (Sandbox Code Playgroud)


Tzo*_*Noy 2

我通过多种方式对此进行了测试,通过在自动加载中添加我的帮助程序以及我们首先加载的 Laravel 帮助程序。

所以我的解决方案是在供应商自动加载之前包含您自己的辅助函数。

我是在文件夹index.php中的文件中完成的public

//my extra line
require_once __DIR__.'/../app/helpers.php';

//this is laravel original code
//I make sure to include before this line

require __DIR__.'/../vendor/autoload.php';
Run Code Online (Sandbox Code Playgroud)

在你的助手文件中,你可以定义你的助手函数:

 function camel_case($value)
 {
     return 'MY_OWN_CAMEL_CASE';
 }
Run Code Online (Sandbox Code Playgroud)