DumpAutoloadCommand.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\Plugin\CommandEvent;
  13. use Composer\Plugin\PluginEvents;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class DumpAutoloadCommand extends BaseCommand
  21. {
  22. protected function configure()
  23. {
  24. $this
  25. ->setName('dump-autoload')
  26. ->setAliases(array('dumpautoload'))
  27. ->setDescription('Dumps the autoloader.')
  28. ->setDefinition(array(
  29. new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'),
  30. new InputOption('optimize', 'o', InputOption::VALUE_NONE, 'Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.'),
  31. new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize`.'),
  32. new InputOption('apcu', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'),
  33. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables autoload-dev rules.'),
  34. ))
  35. ->setHelp(
  36. <<<EOT
  37. <info>php composer.phar dump-autoload</info>
  38. Read more at https://getcomposer.org/doc/03-cli.md#dump-autoload-dumpautoload-
  39. EOT
  40. )
  41. ;
  42. }
  43. protected function execute(InputInterface $input, OutputInterface $output)
  44. {
  45. $composer = $this->getComposer();
  46. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'dump-autoload', $input, $output);
  47. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  48. $installationManager = $composer->getInstallationManager();
  49. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  50. $package = $composer->getPackage();
  51. $config = $composer->getConfig();
  52. $optimize = $input->getOption('optimize') || $config->get('optimize-autoloader');
  53. $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative');
  54. $apcu = $input->getOption('apcu') || $config->get('apcu-autoloader');
  55. if ($authoritative) {
  56. $this->getIO()->writeError('<info>Generating optimized autoload files (authoritative)</info>', false);
  57. } elseif ($optimize) {
  58. $this->getIO()->writeError('<info>Generating optimized autoload files</info>', false);
  59. } else {
  60. $this->getIO()->writeError('<info>Generating autoload files</info>', false);
  61. }
  62. $generator = $composer->getAutoloadGenerator();
  63. $generator->setDevMode(!$input->getOption('no-dev'));
  64. $generator->setClassMapAuthoritative($authoritative);
  65. $generator->setApcu($apcu);
  66. $generator->setRunScripts(!$input->getOption('no-scripts'));
  67. $numberOfClasses = $generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize);
  68. if ($authoritative) {
  69. $this->getIO()->overwriteError('<info>Generated optimized autoload files (authoritative) containing '. $numberOfClasses .' classes</info>');
  70. } elseif ($optimize) {
  71. $this->getIO()->overwriteError('<info>Generated optimized autoload files containing '. $numberOfClasses .' classes</info>');
  72. } else {
  73. $this->getIO()->overwriteError('<info>Generated autoload files containing '. $numberOfClasses .' classes</info>');
  74. }
  75. }
  76. }