AutoloadGenerator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Autoload;
  12. use Composer\Installer\InstallationManager;
  13. use Composer\Json\JsonFile;
  14. use Composer\Package\Loader\JsonLoader;
  15. use Composer\Package\PackageInterface;
  16. use Composer\Repository\RepositoryInterface;
  17. /**
  18. * @author Igor Wiedler <igor@wiedler.ch>
  19. */
  20. class AutoloadGenerator
  21. {
  22. private $localRepo;
  23. private $package;
  24. private $installationManager;
  25. public function __construct(RepositoryInterface $localRepo, PackageInterface $package, InstallationManager $installationManager)
  26. {
  27. $this->localRepo = $localRepo;
  28. $this->package = $package;
  29. $this->installationManager = $installationManager;
  30. }
  31. public function dump($targetFilename)
  32. {
  33. $autoloads = $this->parseAutoloads();
  34. $file = <<<'EOF'
  35. <?php
  36. // autoload.php generated by composer
  37. if (false === class_exists('Symfony\Component\ClassLoader\UniversalClassLoader', false)) {
  38. require __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
  39. }
  40. use Symfony\Component\ClassLoader\UniversalClassLoader;
  41. $loader = new UniversalClassLoader();
  42. EOF;
  43. if (isset($autoloads['psr0'])) {
  44. foreach ($autoloads['psr0'] as $def) {
  45. foreach ($def['mapping'] as $namespace => $path) {
  46. $exportedNamespace = var_export($namespace, true);
  47. $exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true);
  48. $file .= <<<EOF
  49. \$loader->registerNamespace($exportedNamespace, dirname(__DIR__).$exportedPath);
  50. EOF;
  51. }
  52. }
  53. }
  54. if (isset($autoloads['pear'])) {
  55. foreach ($autoloads['pear'] as $def) {
  56. foreach ($def['mapping'] as $prefix => $path) {
  57. $exportedPrefix = var_export($prefix, true);
  58. $exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true);
  59. $file .= <<<EOF
  60. \$loader->registerPrefix($exportedPrefix, dirname(__DIR__).$exportedPath);
  61. EOF;
  62. }
  63. }
  64. }
  65. $file .= <<<'EOF'
  66. $loader->register();
  67. EOF;
  68. file_put_contents($targetFilename, $file);
  69. }
  70. private function parseAutoloads()
  71. {
  72. $installPaths = array();
  73. foreach ($this->localRepo->getPackages() as $package) {
  74. $installPaths[] = array(
  75. $package,
  76. $this->installationManager->getInstallPath($package)
  77. );
  78. }
  79. $installPaths[] = array($this->package, '');
  80. $autoloads = array();
  81. foreach ($installPaths as $item) {
  82. list($package, $installPath) = $item;
  83. if (null !== $package->getTargetDir()) {
  84. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  85. }
  86. foreach ($package->getAutoload() as $type => $mapping) {
  87. $autoloads[$type][] = array(
  88. 'mapping' => $mapping,
  89. 'path' => $installPath,
  90. );
  91. }
  92. }
  93. return $autoloads;
  94. }
  95. }