AutoloadGenerator.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. $filesystem = new Filesystem();
  27. $filesystem->ensureDirectoryExists($installationManager->getVendorPath());
  28. $filesystem->ensureDirectoryExists($targetDir);
  29. $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
  30. $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
  31. $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
  32. $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
  33. $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
  34. $namespacesFile = <<<EOF
  35. <?php
  36. // autoload_namespace.php generated by Composer
  37. \$vendorDir = $vendorDirCode;
  38. \$baseDir = $appBaseDir;
  39. return array(
  40. EOF;
  41. $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
  42. $autoloads = $this->parseAutoloads($packageMap);
  43. foreach ($autoloads['psr-0'] as $namespace => $paths) {
  44. $exportedPaths = array();
  45. foreach ($paths as $path) {
  46. $path = strtr($path, '\\', '/');
  47. $baseDir = '';
  48. if (!$filesystem->isAbsolutePath($path)) {
  49. if (strpos($path, $relVendorPath) === 0) {
  50. // path starts with vendor dir
  51. $path = substr($path, strlen($relVendorPath));
  52. $baseDir = '$vendorDir . ';
  53. } else {
  54. $path = '/'.$path;
  55. $baseDir = '$baseDir . ';
  56. }
  57. } elseif (strpos($path, $vendorPath) === 0) {
  58. $path = substr($path, strlen($vendorPath));
  59. $baseDir = '$vendorDir . ';
  60. }
  61. $exportedPaths[] = $baseDir.var_export($path, true);
  62. }
  63. $exportedPrefix = var_export($namespace, true);
  64. $namespacesFile .= " $exportedPrefix => ";
  65. if (count($exportedPaths) > 1) {
  66. $namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n";
  67. } else {
  68. $namespacesFile .= $exportedPaths[0].",\n";
  69. }
  70. }
  71. $namespacesFile .= ");\n";
  72. $classmapFile = <<<EOF
  73. <?php
  74. // autoload_classmap.php generated by Composer
  75. \$vendorDir = $vendorDirCode;
  76. \$baseDir = $appBaseDir;
  77. return array(
  78. EOF;
  79. // flatten array
  80. $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
  81. foreach ($autoloads['classmap'] as $dir) {
  82. foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
  83. $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
  84. $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n";
  85. }
  86. }
  87. $classmapFile .= ");\n";
  88. file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
  89. file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
  90. if ($includePathFile = $this->getIncludePathsFile($packageMap)) {
  91. file_put_contents($targetDir.'/include_paths.php', $includePathFile);
  92. }
  93. file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile));
  94. copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
  95. }
  96. public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)
  97. {
  98. // build package => install path map
  99. $packageMap = array();
  100. // add main package
  101. $packageMap[] = array($mainPackage, '');
  102. foreach ($packages as $package) {
  103. $packageMap[] = array(
  104. $package,
  105. $installationManager->getInstallPath($package)
  106. );
  107. }
  108. return $packageMap;
  109. }
  110. /**
  111. * Compiles an ordered list of namespace => path mappings
  112. *
  113. * @param array $packageMap array of array(package, installDir-relative-to-composer.json)
  114. * @return array array('psr-0' => array('Ns\\Foo' => array('installDir')))
  115. */
  116. public function parseAutoloads(array $packageMap)
  117. {
  118. $autoloads = array('classmap' => array(), 'psr-0' => array());
  119. foreach ($packageMap as $item) {
  120. list($package, $installPath) = $item;
  121. if (null !== $package->getTargetDir()) {
  122. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  123. }
  124. foreach ($package->getAutoload() as $type => $mapping) {
  125. foreach ($mapping as $namespace => $paths) {
  126. foreach ((array) $paths as $path) {
  127. $autoloads[$type][$namespace][] = empty($installPath) ? $path : $installPath.'/'.$path;
  128. }
  129. }
  130. }
  131. }
  132. foreach ($autoloads as $type => $maps) {
  133. krsort($autoloads[$type]);
  134. }
  135. return $autoloads;
  136. }
  137. /**
  138. * Registers an autoloader based on an autoload map returned by parseAutoloads
  139. *
  140. * @param array $autoloads see parseAutoloads return value
  141. * @return ClassLoader
  142. */
  143. public function createLoader(array $autoloads)
  144. {
  145. $loader = new ClassLoader();
  146. if (isset($autoloads['psr-0'])) {
  147. foreach ($autoloads['psr-0'] as $namespace => $path) {
  148. $loader->add($namespace, $path);
  149. }
  150. }
  151. return $loader;
  152. }
  153. protected function getIncludePathsFile(array $packageMap)
  154. {
  155. $includePaths = array();
  156. foreach ($packageMap as $item) {
  157. list($package, $installPath) = $item;
  158. if (null !== $package->getTargetDir()) {
  159. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  160. }
  161. foreach ($package->getIncludePaths() as $includePath) {
  162. $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
  163. }
  164. }
  165. if (!$includePaths) {
  166. return;
  167. }
  168. return sprintf(
  169. "<?php\nreturn %s;\n", var_export($includePaths, true)
  170. );
  171. }
  172. protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath)
  173. {
  174. $file = <<<'HEADER'
  175. <?php
  176. // autoload.php generated by Composer
  177. if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
  178. require __DIR__.'/ClassLoader.php';
  179. }
  180. return call_user_func(function() {
  181. $loader = new \Composer\Autoload\ClassLoader();
  182. HEADER;
  183. if ($useIncludePath) {
  184. $file .= <<<'INCLUDE_PATH'
  185. $includePaths = require __DIR__.'/include_paths.php';
  186. array_unshift($includePaths, get_include_path());
  187. set_include_path(join(PATH_SEPARATOR, $includePaths));
  188. INCLUDE_PATH;
  189. }
  190. if ($usePSR0) {
  191. $file .= <<<'PSR0'
  192. $map = require __DIR__.'/autoload_namespaces.php';
  193. foreach ($map as $namespace => $path) {
  194. $loader->add($namespace, $path);
  195. }
  196. PSR0;
  197. }
  198. if ($useClassMap) {
  199. $file .= <<<'CLASSMAP'
  200. $classMap = require __DIR__.'/autoload_classmap.php';
  201. if ($classMap) {
  202. $loader->addClassMap($classMap);
  203. }
  204. CLASSMAP;
  205. }
  206. return $file . <<<'FOOTER'
  207. $loader->register();
  208. return $loader;
  209. });
  210. FOOTER;
  211. }
  212. }