8

So - I have a simple PCR0 auto-loader in my bootstrap.php, that should load any PCR0 compatible library class from vendors directory...

spl_autoload_register( function( $classname ) {
    $path = preg_match( '/\\\\/', $classname )
    ? str_replace( '\\', DIRECTORY_SEPARATOR, $classname )
    : str_replace( '_', DIRECTORY_SEPARATOR, $classname );
    $file = VENDORS_PATH . DIRECTORY_SEPARATOR  . $path . '.php';
    if ( file_exists( $file ) ) {
        require_once( $file );
    }
});

I'm not sure if I understand why composer generates auto-loading files in vendors directory (namely composer directory and autoload.php file) ?

Can I stop Composer from generating those auto-loader files? or am I missing something? I don't think I need them?

5 Answers 5

4

There are three autoload related files, each having a different purpose.

  • vendor/autoload.php initializes the autoloaders of composer. Composer offers a autoloaders to enable composer compatible libraries to be load.
  • vendor/composer/autoload_classmap.php this file is used by the classmap autoloader, this is for either libraries that are not even PSR-0 compatible, or production environments (classmap is faster than a lookup through the file system).
  • vendor/composer/autoload_namespaces.php this is the configuration for the PSR-0 autoloading that composer comes with

Now you mentioned that you have your own PSR-0 classloader, which you are not supposed to use for composer dependencies - you are simply supposed to require/include the vendor/autoload.php and have composer take care of the rest.

This is why there is no option to disable the generation of the autoloading files. In the end composer is supposed to enable you to use the library installed, and enables you by providing all loading you need.

Sign up to request clarification or add additional context in comments.

3 Comments

"..require/include the vendor/autoload.php and have composer.." Hmmm. IMHO dependency managament and autoloading are different problems. Some people do prefer for example using Zend\Loader or other libraries for autoloading. I usually prefer to just use PSR0 compliant libraries only and this short snippet in bootstrap - and not to ever worry about different loading strategies. Zend/Loader also supports classmaps...
from composer docs: "For libraries that specify autoload information, Composer generates a vendor/autoload.php file." - this to me almost sounds like Composer presents autoloading as rather optonal, yet you're saying it is not?
for libraries that specify means the libraries that you install through composer. If the composer.json of that library has autload information, then those autoload information will be put into autoload.php.
1

Unfortunately, It doesn't sound like Composer is going to support this feature: https://github.com/composer/composer/issues/1663

Comments

1

There is an option --no-autoloader on install and update commands.

It's implemented in https://github.com/composer/composer/pull/3453 since the December of 2014.

Comments

1

In my CMS EFFCORE I used the following solution...

For UNIX shell:

File composer.json

"scripts": {
    "post-install-cmd": [
        "rm vendors/autoload.php",
        "rm -rf vendors/composer"
    ],
    "post-update-cmd": [
        "rm vendors/autoload.php",
        "rm -rf vendors/composer"
    ]
}

For Win/Nix:

File composer.json

"scripts": {
    "post-install-cmd": [
        "php vendors/post-install-cmd.php"
    ],
    "post-update-cmd": [
        "php vendors/post-install-cmd.php"
    ]
}

File vendors/post-install-cmd.php

# rm -rf vendors/packages/composer
if (file_exists('vendors/packages/composer/')) {
    $composer_items = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator('vendors/packages/composer/', \FilesystemIterator::UNIX_PATHS|\FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($composer_items as $c_path => $c_spl_file_info) {
        if ($c_spl_file_info->isFile()) {if (@unlink($c_path)) print "File '".      $c_path.  "' was removed.\n";}
        if ($c_spl_file_info->isDir ()) {if (@rmdir ($c_path)) print "Directory '". $c_path. "/' was removed.\n";} }
    if (@rmdir('vendors/packages/composer/')) {
        print "Directory 'vendors/packages/composer/' was removed.\n";
    }
}

# rm vendors/packages/autoload.php
if (file_exists('vendors/packages/autoload.php') &&
        @unlink('vendors/packages/autoload.php')) {
    print "File 'vendors/packages/autoload.php' was removed.\n";
}

Comments

-1

Personally I added those files to .gitignore since the project I am working on has an autoloader that works fine

1 Comment

Please add some explanation to your answer such that others can learn from it. How does any git related file help to skip generating a autoloader file?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.