Composer: specify autoload_files to order

The Laravel helper function has if ( ! function_exists('xx')) protection.

Is it possible to specify the order of autoload_files , and Kint.class.php before helpers.php ?

 return array( $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php', $vendorDir . '/raveren/kint/Kint.class.php', ); 
+7
php laravel composer-php kint
source share
2 answers

I tested this in several ways, adding my helpers to startup, as well as the Laravel helpers, which we downloaded first.

So my solution is to enable my own helper functions before the vendor autoload.

I did this in the index.php file in the public folder

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

inside your helpers file, you can define your helper functions:

  function camel_case($value) { return 'MY_OWN_CAMEL_CASE'; } 
0
source share

This is a really nasty problem. I filed a feature request for the composer: https://github.com/composer/composer/issues/6768

There should be a way to specify the order of startup operations so that your custom “files” can be loaded before any of the classes in the “require” or “require-dev” sections; any solution that requires you to edit a third-party package inside the provider / is hacked at best, but at the moment I don’t think there are other good alternatives.

The best I can think of is to use a script to modify the /autoload.php provider so that it forcibly includes your files before it includes any of the startup classes. Here is my modify_autoload.php :

 <?php /** * Updates the vendor/autoload.php so it manually includes any files specified in composer.json 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 it manually, or you can run its composer by adding it to your composer.json scripts:

 { // ... "scripts": { "post-autoload-dump": [ "php modify_autoload.php" ] } // ... } 
0
source share

All Articles