AutoloadGenerator.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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, $bcLinks = false)
  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. $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
  33. $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
  34. $namespacesFile = <<<EOF
  35. <?php
  36. // autoload_namespace.php generated by Composer
  37. \$vendorDir = $vendorDirCode;
  38. \$baseDir = $appBaseDirCode;
  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. $exportedPaths[] = $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path);
  47. }
  48. $exportedPrefix = var_export($namespace, true);
  49. $namespacesFile .= " $exportedPrefix => ";
  50. if (count($exportedPaths) > 1) {
  51. $namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n";
  52. } else {
  53. $namespacesFile .= $exportedPaths[0].",\n";
  54. }
  55. }
  56. $namespacesFile .= ");\n";
  57. $classmapFile = <<<EOF
  58. <?php
  59. // autoload_classmap.php generated by Composer
  60. \$vendorDir = $vendorDirCode;
  61. \$baseDir = $appBaseDirCode;
  62. return array(
  63. EOF;
  64. // flatten array
  65. $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
  66. foreach ($autoloads['classmap'] as $dir) {
  67. foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
  68. $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
  69. $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n";
  70. }
  71. }
  72. $classmapFile .= ");\n";
  73. file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
  74. file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
  75. if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)) {
  76. file_put_contents($targetDir.'/include_paths.php', $includePathFile);
  77. }
  78. file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile));
  79. copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
  80. // TODO BC feature, add E_DEPRECATED in autoload.php on April 30th, remove after May 30th
  81. if ($bcLinks) {
  82. $filesystem->ensureDirectoryExists($targetDir.'/.composer');
  83. file_put_contents($targetDir.'/.composer/autoload_namespaces.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/autoload_namespaces.php';\n");
  84. file_put_contents($targetDir.'/.composer/autoload_classmap.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/autoload_classmap.php';\n");
  85. file_put_contents($targetDir.'/.composer/autoload.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/autoload.php';\n");
  86. file_put_contents($targetDir.'/.composer/ClassLoader.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/ClassLoader.php';\n");
  87. if ($includePathFile) {
  88. file_put_contents($targetDir.'/.composer/include_paths.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/include_paths.php';\n");
  89. }
  90. }
  91. }
  92. public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)
  93. {
  94. // build package => install path map
  95. $packageMap = array();
  96. // add main package
  97. $packageMap[] = array($mainPackage, '');
  98. foreach ($packages as $package) {
  99. $packageMap[] = array(
  100. $package,
  101. $installationManager->getInstallPath($package)
  102. );
  103. }
  104. return $packageMap;
  105. }
  106. /**
  107. * Compiles an ordered list of namespace => path mappings
  108. *
  109. * @param array $packageMap array of array(package, installDir-relative-to-composer.json)
  110. * @return array array('psr-0' => array('Ns\\Foo' => array('installDir')))
  111. */
  112. public function parseAutoloads(array $packageMap)
  113. {
  114. $autoloads = array('classmap' => array(), 'psr-0' => array());
  115. foreach ($packageMap as $item) {
  116. list($package, $installPath) = $item;
  117. if (null !== $package->getTargetDir()) {
  118. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  119. }
  120. foreach ($package->getAutoload() as $type => $mapping) {
  121. // skip misconfigured packages
  122. if (!is_array($mapping)) {
  123. continue;
  124. }
  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, Filesystem $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)
  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. $includePath = trim($includePath, '/');
  163. $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
  164. }
  165. }
  166. if (!$includePaths) {
  167. return;
  168. }
  169. $includePathsFile = <<<EOF
  170. <?php
  171. // include_paths.php generated by Composer
  172. \$vendorDir = $vendorDirCode;
  173. \$baseDir = $appBaseDirCode;
  174. return array(
  175. EOF;
  176. foreach ($includePaths as $path) {
  177. $includePathsFile .= " " . $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path) . ",\n";
  178. }
  179. return $includePathsFile . ");\n";
  180. }
  181. protected function getPathCode(Filesystem $filesystem, $relVendorPath, $vendorPath, $path)
  182. {
  183. $path = strtr($path, '\\', '/');
  184. $baseDir = '';
  185. if (!$filesystem->isAbsolutePath($path)) {
  186. if (strpos($path, $relVendorPath) === 0) {
  187. // path starts with vendor dir
  188. $path = substr($path, strlen($relVendorPath));
  189. $baseDir = '$vendorDir . ';
  190. } else {
  191. $path = '/'.$path;
  192. $baseDir = '$baseDir . ';
  193. }
  194. } elseif (strpos($path, $vendorPath) === 0) {
  195. $path = substr($path, strlen($vendorPath));
  196. $baseDir = '$vendorDir . ';
  197. }
  198. return $baseDir.var_export($path, true);
  199. }
  200. protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath)
  201. {
  202. $file = <<<'HEADER'
  203. <?php
  204. // autoload.php generated by Composer
  205. if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
  206. require __DIR__.'/ClassLoader.php';
  207. }
  208. return call_user_func(function() {
  209. $loader = new \Composer\Autoload\ClassLoader();
  210. HEADER;
  211. if ($useIncludePath) {
  212. $file .= <<<'INCLUDE_PATH'
  213. $includePaths = require __DIR__.'/include_paths.php';
  214. array_unshift($includePaths, get_include_path());
  215. set_include_path(join(PATH_SEPARATOR, $includePaths));
  216. INCLUDE_PATH;
  217. }
  218. if ($usePSR0) {
  219. $file .= <<<'PSR0'
  220. $map = require __DIR__.'/autoload_namespaces.php';
  221. foreach ($map as $namespace => $path) {
  222. $loader->add($namespace, $path);
  223. }
  224. PSR0;
  225. }
  226. if ($useClassMap) {
  227. $file .= <<<'CLASSMAP'
  228. $classMap = require __DIR__.'/autoload_classmap.php';
  229. if ($classMap) {
  230. $loader->addClassMap($classMap);
  231. }
  232. CLASSMAP;
  233. }
  234. return $file . <<<'FOOTER'
  235. $loader->register();
  236. return $loader;
  237. });
  238. FOOTER;
  239. }
  240. }