AutoloadGenerator.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\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. return call_user_func(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. $classMap = require __DIR__.'/autoload_classmap.php';
  39. if ($classMap) {
  40. $loader->addClassMap($classMap);
  41. }
  42. $loader->register();
  43. return $loader;
  44. });
  45. EOF;
  46. $filesystem = new Filesystem();
  47. $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
  48. $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
  49. $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
  50. $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
  51. $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
  52. $namespacesFile = <<<EOF
  53. <?php
  54. // autoload_namespace.php generated by Composer
  55. \$vendorDir = $vendorDirCode;
  56. \$baseDir = $appBaseDir;
  57. return array(
  58. EOF;
  59. $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
  60. $autoloads = $this->parseAutoloads($packageMap);
  61. foreach ($autoloads['psr-0'] as $namespace => $paths) {
  62. $exportedPaths = array();
  63. foreach ($paths as $path) {
  64. $path = strtr($path, '\\', '/');
  65. $baseDir = '';
  66. if (!$filesystem->isAbsolutePath($path)) {
  67. if (strpos($path, $relVendorPath) === 0) {
  68. // path starts with vendor dir
  69. $path = substr($path, strlen($relVendorPath));
  70. $baseDir = '$vendorDir . ';
  71. } else {
  72. $path = '/'.$path;
  73. $baseDir = '$baseDir . ';
  74. }
  75. } elseif (strpos($path, $vendorPath) === 0) {
  76. $path = substr($path, strlen($vendorPath));
  77. $baseDir = '$vendorDir . ';
  78. }
  79. $exportedPaths[] = $baseDir.var_export($path, true);
  80. }
  81. $exportedPrefix = var_export($namespace, true);
  82. $namespacesFile .= " $exportedPrefix => ";
  83. if (count($exportedPaths) > 1) {
  84. $namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n";
  85. } else {
  86. $namespacesFile .= $exportedPaths[0].",\n";
  87. }
  88. }
  89. $namespacesFile .= ");\n";
  90. $classmapFile = <<<EOF
  91. <?php
  92. // autoload_classmap.php generated by Composer
  93. \$vendorDir = $vendorDirCode;
  94. \$baseDir = $appBaseDir;
  95. return array(
  96. EOF;
  97. // flatten array
  98. $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
  99. foreach ($autoloads['classmap'] as $dir) {
  100. foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
  101. $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
  102. $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n";
  103. }
  104. }
  105. $classmapFile .= ");\n";
  106. file_put_contents($targetDir.'/autoload.php', $autoloadFile);
  107. file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
  108. file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
  109. copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
  110. }
  111. public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)
  112. {
  113. // build package => install path map
  114. $packageMap = array();
  115. // add main package
  116. $packageMap[] = array($mainPackage, '');
  117. foreach ($packages as $package) {
  118. $packageMap[] = array(
  119. $package,
  120. $installationManager->getInstallPath($package)
  121. );
  122. }
  123. return $packageMap;
  124. }
  125. /**
  126. * Compiles an ordered list of namespace => path mappings
  127. *
  128. * @param array $packageMap array of array(package, installDir-relative-to-composer.json)
  129. * @return array array('psr-0' => array('Ns\\Foo' => array('installDir')))
  130. */
  131. public function parseAutoloads(array $packageMap)
  132. {
  133. $autoloads = array('classmap' => array(), 'psr-0' => array());
  134. foreach ($packageMap as $item) {
  135. list($package, $installPath) = $item;
  136. if (null !== $package->getTargetDir()) {
  137. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  138. }
  139. foreach ($package->getAutoload() as $type => $mapping) {
  140. foreach ($mapping as $namespace => $path) {
  141. $autoloads[$type][$namespace][] = empty($installPath) ? $path : $installPath.'/'.$path;
  142. }
  143. }
  144. }
  145. foreach ($autoloads as $type => $maps) {
  146. krsort($autoloads[$type]);
  147. }
  148. return $autoloads;
  149. }
  150. /**
  151. * Registers an autoloader based on an autoload map returned by parseAutoloads
  152. *
  153. * @param array $autoloads see parseAutoloads return value
  154. * @return ClassLoader
  155. */
  156. public function createLoader(array $autoloads)
  157. {
  158. $loader = new ClassLoader();
  159. if (isset($autoloads['psr-0'])) {
  160. foreach ($autoloads['psr-0'] as $namespace => $path) {
  161. $loader->add($namespace, $path);
  162. }
  163. }
  164. return $loader;
  165. }
  166. }