Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer: Specify autoload_files require order

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

Can I specify order of the autoload_files, and let Kint.class.php require before helpers.php?

return array( 
    $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php',
    $vendorDir . '/raveren/kint/Kint.class.php',
);
like image 897
Chen-Tsu Lin Avatar asked Dec 24 '14 12:12

Chen-Tsu Lin


People also ask

Where are composer dependencies defined?

composer.json : Project setup# To start using Composer in your project, all you need is a composer.json file. This file describes the dependencies of your project and may contain other metadata as well. It typically should go in the top-most directory of your project/VCS repository.

What does composer dump autoload do?

This will clear the current compiled files, update the classes it needs and then write them back out so you don't have to do it again. Ideally, you execute composer dump-autoload -o , for a faster load of your webpages.

What is Classmap in composer json?

This map is built by scanning for classes in all . php and . inc files in the given directories/files. You can use the classmap generation support to define autoloading for all libraries that do not follow PSR-0/4. To configure this you specify all directories or files to search for classes.

How do I use composer JSON file?

To configure Composer for your PHP app json file specifies required packages. Verify that a composer. json file is present in the root of your git repository. Run composer install (on your local machine) to install the required packages and generate a composer.


2 Answers

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

There should be a way to specify the order of operations of the autoloading so that your custom "files" can be loaded before any of the classes from the "require" or "require-dev" sections; any solution that requires you to edit a 3rd party package inside of vendor/ is hacky at best, but at present, I don't think there are any other good alternatives.

The best I could come up with is to use a script to modify the vendor/autoload.php so that it forcefully includes your files BEFORE it includes any of the autoload classes. Here's my 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);

You can run this manually, or you can have composer run it by adding it to your composer.json scripts:

{
 // ... 
  "scripts": {
    "post-autoload-dump": [
      "php modify_autoload.php"
    ]
  }
 // ...
}
like image 197
Everett Avatar answered Oct 17 '22 23:10

Everett


I tested this in several ways, by adding my helpers in autoload as well and still Laravel helpers we loaded first.

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

I did it in index.php file on 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';
 }
like image 35
Tzook Bar Noy Avatar answered Oct 17 '22 23:10

Tzook Bar Noy