AutoloadGenerator.php 4.4 KB

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