AutoloadGenerator.php 5.4 KB

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