3

I use PHPUnit for testing and development only, I don't want my app to autoload them in production server, is it possible?

"require-dev": {
    "phpunit/phpunit": "4.2.*",
..

I see the file "autoload_classmap.php" contains lines such as..

return array(
    'File_Iterator' => $vendorDir . '/phpunit/php-file-iterator/File/Iterator.php',
    'File_Iterator_Facade' => $vendorDir . '/phpunit/php-file-iterator/File/Iterator/Facade.php',

Updated:

I want a clean production env and don't want to autoload phpunit, I only need phpunit during development. So, can composer generate two autoload.php so I can include them depending on my current env?

Howard
  • 19,215
  • 35
  • 112
  • 184

1 Answers1

6

--no-dev : Skip installing packages listed in require-dev

composer install --no-dev --optimize-autoloader

You will probably also want to do: --optimize-autoloader (-o): Convert PSR-0/4 autoloading to classmap to get a faster autoloader. This is recommended especially for production, but can take a bit of time to run so it is currently not done by default.

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
  • Regarding the classmap: Please see my answer here: http://stackoverflow.com/questions/22803419/why-use-a-psr-0-or-psr-4-autoload-in-composer-if-classmap-is-actually-faster/22823995#22823995 Short version: Classmaps are not the fastest way to autoload in EVERY case. – Sven Aug 23 '14 at 11:26
  • Thanks, I've updated the question. Actually what I want is to generate two autoload.php, one for development, and one for production. Is it possible? – Howard Aug 23 '14 at 18:14
  • `--dev` (the default), will install things from the `require-dev` section. `--no-dev` won't install the `require-dev` section. That is what you need. – Alister Bulman Aug 23 '14 at 18:30
  • Yes, you are right, but the default will also `pollute` the autoload.php, I don't mind to waste the storage space, but I don't want to waste the CPU time in autoloading the stuffs that I never use in production. (Actually it is good to avoid autoloading lib that I don't intended to be used in production) – Howard Aug 24 '14 at 05:23
  • @Howard, you don't seem to be grasping the point. In development, it runs with the default - so composer pulls in, and builds the autoloading to include PHPUnit and so on from the require-dev section. In production, when you run composer, you add the --no-dev parameter which does not pull in anything from require-dev. – Alister Bulman Aug 24 '14 at 09:18