AutoloadGenerator.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. use Composer\Downloader\Util\Filesystem;
  18. /**
  19. * @author Igor Wiedler <igor@wiedler.ch>
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class AutoloadGenerator
  23. {
  24. public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
  25. {
  26. $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');
  27. $autoloadFile .= <<<'EOF'
  28. // autoload.php generated by Composer
  29. function init() {
  30. $loader = new ClassLoader();
  31. $map = require __DIR__.'/autoload_namespaces.php';
  32. foreach ($map as $namespace => $path) {
  33. $loader->add($namespace, $path);
  34. }
  35. $loader->register();
  36. return $loader;
  37. }
  38. return init();
  39. EOF;
  40. $filesystem = new Filesystem();
  41. $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
  42. $relVendorPath = ltrim(substr($vendorPath, strlen(getcwd())), '/');
  43. $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
  44. $namespacesFile = <<<EOF
  45. <?php
  46. // autoload_namespace.php generated by Composer
  47. \$vendorDir = $vendorDirCode;
  48. return array(
  49. EOF;
  50. // build package => install path map
  51. $packageMap = array();
  52. foreach ($localRepo->getPackages() as $installedPackage) {
  53. $packageMap[] = array(
  54. $installedPackage,
  55. $installationManager->getInstallPath($installedPackage)
  56. );
  57. }
  58. // add main package
  59. $packageMap[] = array($mainPackage, '');
  60. $autoloads = $this->parseAutoloads($packageMap);
  61. $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
  62. $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
  63. if (isset($autoloads['psr-0'])) {
  64. foreach ($autoloads['psr-0'] as $def) {
  65. $def['path'] = strtr($def['path'], '\\', '/');
  66. $baseDir = '';
  67. if (!$filesystem->isAbsolutePath($def['path'])) {
  68. if (strpos($def['path'], $relVendorPath) === 0) {
  69. $def['path'] = substr($def['path'], strlen($relVendorPath));
  70. $baseDir = '$vendorDir . ';
  71. } else {
  72. $def['path'] = '/'.$def['path'];
  73. $baseDir = $appBaseDir . ' . ';
  74. }
  75. } elseif (strpos($def['path'], $vendorPath) === 0) {
  76. $def['path'] = substr($def['path'], strlen($vendorPath));
  77. $baseDir = '$vendorDir . ';
  78. }
  79. $exportedPrefix = var_export($def['namespace'], true);
  80. $exportedPath = var_export($def['path'], true);
  81. $namespacesFile .= " $exportedPrefix => {$baseDir}{$exportedPath},\n";
  82. }
  83. }
  84. $namespacesFile .= ");\n";
  85. file_put_contents($targetDir.'/autoload.php', $autoloadFile);
  86. file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
  87. }
  88. /**
  89. * Compiles an ordered list of namespace => path mappings
  90. *
  91. * @param array $packageMap array of array(package, installDir-relative-to-composer.json)
  92. * @return array array('psr-0' => array(array('namespace' => 'Foo', 'path' => 'installDir')))
  93. */
  94. public function parseAutoloads(array $packageMap)
  95. {
  96. $autoloads = array();
  97. foreach ($packageMap as $item) {
  98. list($package, $installPath) = $item;
  99. if (null !== $package->getTargetDir()) {
  100. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  101. }
  102. foreach ($package->getAutoload() as $type => $mapping) {
  103. foreach ($mapping as $namespace => $path) {
  104. $autoloads[$type][] = array(
  105. 'namespace' => $namespace,
  106. 'path' => empty($installPath) ? $path : $installPath.'/'.$path,
  107. );
  108. }
  109. }
  110. }
  111. foreach ($autoloads as $type => $maps) {
  112. usort($autoloads[$type], function ($a, $b) {
  113. return strcmp($b['namespace'], $a['namespace']);
  114. });
  115. }
  116. return $autoloads;
  117. }
  118. /**
  119. * Registers an autoloader based on an autoload map returned by parseAutoloads
  120. *
  121. * @param array $autoloads see parseAutoloads return value
  122. * @return ClassLoader
  123. */
  124. public function createLoader(array $autoloads)
  125. {
  126. $loader = new ClassLoader();
  127. if (isset($autoloads['psr-0'])) {
  128. foreach ($autoloads['psr-0'] as $def) {
  129. $loader->add($def['namespace'], $def['path']);
  130. }
  131. }
  132. return $loader;
  133. }
  134. }