AutoloadGenerator.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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\Config;
  13. use Composer\EventDispatcher\EventDispatcher;
  14. use Composer\Installer\InstallationManager;
  15. use Composer\Package\AliasPackage;
  16. use Composer\Package\PackageInterface;
  17. use Composer\Repository\InstalledRepositoryInterface;
  18. use Composer\Util\Filesystem;
  19. use Composer\Script\ScriptEvents;
  20. /**
  21. * @author Igor Wiedler <igor@wiedler.ch>
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class AutoloadGenerator
  25. {
  26. /**
  27. * @var EventDispatcher
  28. */
  29. private $eventDispatcher;
  30. public function __construct(EventDispatcher $eventDispatcher)
  31. {
  32. $this->eventDispatcher = $eventDispatcher;
  33. }
  34. public function dump(Config $config, InstalledRepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
  35. {
  36. $this->eventDispatcher->dispatchScript(ScriptEvents::PRE_AUTOLOAD_DUMP);
  37. $filesystem = new Filesystem();
  38. $filesystem->ensureDirectoryExists($config->get('vendor-dir'));
  39. $basePath = $filesystem->normalizePath(realpath(getcwd()));
  40. $vendorPath = $filesystem->normalizePath(realpath($config->get('vendor-dir')));
  41. $useGlobalIncludePath = (bool) $config->get('use-include-path');
  42. $prependAutoloader = $config->get('prepend-autoloader') === false ? 'false' : 'true';
  43. $targetDir = $vendorPath.'/'.$targetDir;
  44. $filesystem->ensureDirectoryExists($targetDir);
  45. $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
  46. $vendorPathCode52 = str_replace('__DIR__', 'dirname(__FILE__)', $vendorPathCode);
  47. $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);
  48. $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, $basePath, true);
  49. $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
  50. $namespacesFile = <<<EOF
  51. <?php
  52. // autoload_namespaces.php @generated by Composer
  53. \$vendorDir = $vendorPathCode52;
  54. \$baseDir = $appBaseDirCode;
  55. return array(
  56. EOF;
  57. $psr4File = <<<EOF
  58. <?php
  59. // autoload_psr4.php @generated by Composer
  60. \$vendorDir = $vendorPathCode52;
  61. \$baseDir = $appBaseDirCode;
  62. return array(
  63. EOF;
  64. // Collect information from all packages.
  65. $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getCanonicalPackages());
  66. $autoloads = $this->parseAutoloads($packageMap, $mainPackage);
  67. // Process the 'psr-0' base directories.
  68. foreach ($autoloads['psr-0'] as $namespace => $paths) {
  69. $exportedPaths = array();
  70. foreach ($paths as $path) {
  71. $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
  72. }
  73. $exportedPrefix = var_export($namespace, true);
  74. $namespacesFile .= " $exportedPrefix => ";
  75. $namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n";
  76. }
  77. $namespacesFile .= ");\n";
  78. // Process the 'psr-4' base directories.
  79. foreach ($autoloads['psr-4'] as $namespace => $paths) {
  80. if ('\\' !== $namespace[strlen($namespace) - 1]) {
  81. throw new \Exception("PSR-4 namespaces must end with a namespace separator. '$namespace' does not.");
  82. }
  83. $exportedPaths = array();
  84. foreach ($paths as $path) {
  85. $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
  86. }
  87. $exportedPrefix = var_export($namespace, true);
  88. $psr4File .= " $exportedPrefix => ";
  89. $psr4File .= "array(".implode(', ', $exportedPaths)."),\n";
  90. }
  91. $psr4File .= ");\n";
  92. $classmapFile = <<<EOF
  93. <?php
  94. // autoload_classmap.php @generated by Composer
  95. \$vendorDir = $vendorPathCode52;
  96. \$baseDir = $appBaseDirCode;
  97. return array(
  98. EOF;
  99. // add custom psr-0 autoloading if the root package has a target dir
  100. $targetDirLoader = null;
  101. $mainAutoload = $mainPackage->getAutoload();
  102. if ($mainPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) {
  103. $levels = count(explode('/', $filesystem->normalizePath($mainPackage->getTargetDir())));
  104. $prefixes = implode(', ', array_map(function ($prefix) {
  105. return var_export($prefix, true);
  106. }, array_keys($mainAutoload['psr-0'])));
  107. $baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, $basePath, true);
  108. $targetDirLoader = <<<EOF
  109. public static function autoload(\$class)
  110. {
  111. \$dir = $baseDirFromTargetDirCode . '/';
  112. \$prefixes = array($prefixes);
  113. foreach (\$prefixes as \$prefix) {
  114. if (0 !== strpos(\$class, \$prefix)) {
  115. continue;
  116. }
  117. \$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), $levels)).'.php';
  118. if (!\$path = stream_resolve_include_path(\$path)) {
  119. return false;
  120. }
  121. require \$path;
  122. return true;
  123. }
  124. }
  125. EOF;
  126. }
  127. // flatten array
  128. $classMap = array();
  129. if ($scanPsr0Packages) {
  130. // Scan the PSR-0 directories for class files, and add them to the
  131. // class map.
  132. foreach ($autoloads['psr-0'] as $namespace => $paths) {
  133. foreach ($paths as $dir) {
  134. $dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath.'/'.$dir);
  135. if (!is_dir($dir)) {
  136. continue;
  137. }
  138. $whitelist = sprintf(
  139. '{%s/%s.+(?<!(?<!/)Test\.php)$}',
  140. preg_quote($dir),
  141. strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
  142. );
  143. foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
  144. if ('' === $namespace || 0 === strpos($class, $namespace)) {
  145. if (!isset($classMap[$class])) {
  146. $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
  147. $classMap[$class] = $path.",\n";
  148. }
  149. }
  150. }
  151. }
  152. }
  153. // Scan the PSR-4 directories for class files, and add them to the
  154. // class map.
  155. foreach ($autoloads['psr-4'] as $namespace => $paths) {
  156. foreach ($paths as $dir) {
  157. $dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath.'/'.$dir);
  158. if (!is_dir($dir)) {
  159. continue;
  160. }
  161. $whitelist = sprintf(
  162. '{%s/%s.+(?<!(?<!/)Test\.php)$}',
  163. preg_quote($dir),
  164. strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
  165. );
  166. foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
  167. if ('' === $namespace || 0 === strpos($class, $namespace)) {
  168. if (!isset($classMap[$class])) {
  169. $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
  170. $classMap[$class] = $path.",\n";
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
  178. foreach ($autoloads['classmap'] as $dir) {
  179. foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
  180. $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
  181. $classMap[$class] = $path.",\n";
  182. }
  183. }
  184. ksort($classMap);
  185. foreach ($classMap as $class => $code) {
  186. $classmapFile .= ' '.var_export($class, true).' => '.$code;
  187. }
  188. $classmapFile .= ");\n";
  189. if (!$suffix) {
  190. $suffix = $config->get('autoloader-suffix') ?: md5(uniqid('', true));
  191. }
  192. file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
  193. file_put_contents($targetDir.'/autoload_psr4.php', $psr4File);
  194. file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
  195. if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
  196. file_put_contents($targetDir.'/include_paths.php', $includePathFile);
  197. }
  198. if ($includeFilesFile = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
  199. file_put_contents($targetDir.'/autoload_files.php', $includeFilesFile);
  200. }
  201. file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix));
  202. file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFile, $targetDirLoader, (bool) $includeFilesFile, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader));
  203. // use stream_copy_to_stream instead of copy
  204. // to work around https://bugs.php.net/bug.php?id=64634
  205. $sourceLoader = fopen(__DIR__.'/ClassLoader.php', 'r');
  206. $targetLoader = fopen($targetDir.'/ClassLoader.php', 'w+');
  207. stream_copy_to_stream($sourceLoader, $targetLoader);
  208. fclose($sourceLoader);
  209. fclose($targetLoader);
  210. unset($sourceLoader, $targetLoader);
  211. $this->eventDispatcher->dispatchScript(ScriptEvents::POST_AUTOLOAD_DUMP);
  212. }
  213. public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)
  214. {
  215. // build package => install path map
  216. $packageMap = array(array($mainPackage, ''));
  217. foreach ($packages as $package) {
  218. if ($package instanceof AliasPackage) {
  219. continue;
  220. }
  221. $this->validatePackage($package);
  222. $packageMap[] = array(
  223. $package,
  224. $installationManager->getInstallPath($package)
  225. );
  226. }
  227. return $packageMap;
  228. }
  229. /**
  230. * @param PackageInterface $package
  231. *
  232. * @throws \Exception
  233. * Throws an exception, if the package has illegal settings.
  234. */
  235. protected function validatePackage(PackageInterface $package) {
  236. $autoload = $package->getAutoload();
  237. if (!empty($autoload['psr-4']) && null !== $package->getTargetDir()) {
  238. $name = $package->getName();
  239. $package->getTargetDir();
  240. throw new \Exception("The ['autoload']['psr-4'] setting is incompatible with the ['target-dir'] setting, in package '$name'.");
  241. }
  242. }
  243. /**
  244. * Compiles an ordered list of namespace => path mappings
  245. *
  246. * @param array $packageMap array of array(package, installDir-relative-to-composer.json)
  247. * @param PackageInterface $mainPackage root package instance
  248. * @return array array('psr-0' => array('Ns\\Foo' => array('installDir')))
  249. */
  250. public function parseAutoloads(array $packageMap, PackageInterface $mainPackage)
  251. {
  252. $mainPackageMap = array_shift($packageMap);
  253. $sortedPackageMap = $this->sortPackageMap($packageMap);
  254. $sortedPackageMap[] = $mainPackageMap;
  255. array_unshift($packageMap, $mainPackageMap);
  256. $psr0 = $this->parseAutoloadsType($packageMap, 'psr-0', $mainPackage);
  257. $psr4 = $this->parseAutoloadsType($packageMap, 'psr-4', $mainPackage);
  258. $classmap = $this->parseAutoloadsType($sortedPackageMap, 'classmap', $mainPackage);
  259. $files = $this->parseAutoloadsType($sortedPackageMap, 'files', $mainPackage);
  260. krsort($psr0);
  261. krsort($psr4);
  262. return array('psr-0' => $psr0, 'psr-4' => $psr4, 'classmap' => $classmap, 'files' => $files);
  263. }
  264. /**
  265. * Registers an autoloader based on an autoload map returned by parseAutoloads
  266. *
  267. * @param array $autoloads see parseAutoloads return value
  268. * @return ClassLoader
  269. */
  270. public function createLoader(array $autoloads)
  271. {
  272. $loader = new ClassLoader();
  273. if (isset($autoloads['psr-0'])) {
  274. foreach ($autoloads['psr-0'] as $namespace => $path) {
  275. $loader->add($namespace, $path);
  276. }
  277. }
  278. if (isset($autoloads['psr-4'])) {
  279. foreach ($autoloads['psr-4'] as $namespace => $path) {
  280. $loader->addPsr4($namespace, $path);
  281. }
  282. }
  283. return $loader;
  284. }
  285. protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode)
  286. {
  287. $includePaths = array();
  288. foreach ($packageMap as $item) {
  289. list($package, $installPath) = $item;
  290. if (null !== $package->getTargetDir() && strlen($package->getTargetDir()) > 0) {
  291. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  292. }
  293. foreach ($package->getIncludePaths() as $includePath) {
  294. $includePath = trim($includePath, '/');
  295. $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
  296. }
  297. }
  298. if (!$includePaths) {
  299. return;
  300. }
  301. $includePathsCode = '';
  302. foreach ($includePaths as $path) {
  303. $includePathsCode .= " " . $this->getPathCode($filesystem, $basePath, $vendorPath, $path) . ",\n";
  304. }
  305. return <<<EOF
  306. <?php
  307. // include_paths.php @generated by Composer
  308. \$vendorDir = $vendorPathCode;
  309. \$baseDir = $appBaseDirCode;
  310. return array(
  311. $includePathsCode);
  312. EOF;
  313. }
  314. protected function getIncludeFilesFile(array $files, Filesystem $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode)
  315. {
  316. $filesCode = '';
  317. $files = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($files));
  318. foreach ($files as $functionFile) {
  319. $filesCode .= ' '.$this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile).",\n";
  320. }
  321. if (!$filesCode) {
  322. return FALSE;
  323. }
  324. return <<<EOF
  325. <?php
  326. // autoload_files.php @generated by Composer
  327. \$vendorDir = $vendorPathCode;
  328. \$baseDir = $appBaseDirCode;
  329. return array(
  330. $filesCode);
  331. EOF;
  332. }
  333. protected function getPathCode(Filesystem $filesystem, $basePath, $vendorPath, $path)
  334. {
  335. if (!$filesystem->isAbsolutePath($path)) {
  336. $path = $basePath . '/' . $path;
  337. }
  338. $path = $filesystem->normalizePath($path);
  339. $baseDir = '';
  340. if (strpos($path.'/', $vendorPath.'/') === 0) {
  341. $path = substr($path, strlen($vendorPath));
  342. $baseDir = '$vendorDir';
  343. if ($path !== false) {
  344. $baseDir .= " . ";
  345. }
  346. } else {
  347. $path = $filesystem->normalizePath($filesystem->findShortestPath($basePath, $path, true));
  348. if (!$filesystem->isAbsolutePath($path)) {
  349. $baseDir = '$baseDir . ';
  350. $path = '/' . $path;
  351. }
  352. }
  353. if (preg_match('/\.phar$/', $path)) {
  354. $baseDir = "'phar://' . " . $baseDir;
  355. }
  356. return $baseDir . (($path !== false) ? var_export($path, true) : "");
  357. }
  358. protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix)
  359. {
  360. return <<<AUTOLOAD
  361. <?php
  362. // autoload.php @generated by Composer
  363. require_once $vendorPathToTargetDirCode . '/autoload_real.php';
  364. return ComposerAutoloaderInit$suffix::getLoader();
  365. AUTOLOAD;
  366. }
  367. protected function getAutoloadRealFile($useClassMap, $useIncludePath, $targetDirLoader, $useIncludeFiles, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader)
  368. {
  369. // TODO the class ComposerAutoloaderInit should be revert to a closure
  370. // when APC has been fixed:
  371. // - https://github.com/composer/composer/issues/959
  372. // - https://bugs.php.net/bug.php?id=52144
  373. // - https://bugs.php.net/bug.php?id=61576
  374. // - https://bugs.php.net/bug.php?id=59298
  375. $file = <<<HEADER
  376. <?php
  377. // autoload_real.php @generated by Composer
  378. class ComposerAutoloaderInit$suffix
  379. {
  380. private static \$loader;
  381. public static function loadClassLoader(\$class)
  382. {
  383. if ('Composer\\Autoload\\ClassLoader' === \$class) {
  384. require __DIR__ . '/ClassLoader.php';
  385. }
  386. }
  387. public static function getLoader()
  388. {
  389. if (null !== self::\$loader) {
  390. return self::\$loader;
  391. }
  392. spl_autoload_register(array('ComposerAutoloaderInit$suffix', 'loadClassLoader'), true, $prependAutoloader);
  393. self::\$loader = \$loader = new \\Composer\\Autoload\\ClassLoader();
  394. spl_autoload_unregister(array('ComposerAutoloaderInit$suffix', 'loadClassLoader'));
  395. \$vendorDir = $vendorPathCode;
  396. \$baseDir = $appBaseDirCode;
  397. HEADER;
  398. if ($useIncludePath) {
  399. $file .= <<<'INCLUDE_PATH'
  400. $includePaths = require __DIR__ . '/include_paths.php';
  401. array_push($includePaths, get_include_path());
  402. set_include_path(join(PATH_SEPARATOR, $includePaths));
  403. INCLUDE_PATH;
  404. }
  405. $file .= <<<'PSR0'
  406. $map = require __DIR__ . '/autoload_namespaces.php';
  407. foreach ($map as $namespace => $path) {
  408. $loader->set($namespace, $path);
  409. }
  410. PSR0;
  411. $file .= <<<'PSR4'
  412. $map = require __DIR__ . '/autoload_psr4.php';
  413. foreach ($map as $namespace => $path) {
  414. $loader->setPsr4($namespace, $path);
  415. }
  416. PSR4;
  417. if ($useClassMap) {
  418. $file .= <<<'CLASSMAP'
  419. $classMap = require __DIR__ . '/autoload_classmap.php';
  420. if ($classMap) {
  421. $loader->addClassMap($classMap);
  422. }
  423. CLASSMAP;
  424. }
  425. if ($useGlobalIncludePath) {
  426. $file .= <<<'INCLUDEPATH'
  427. $loader->setUseIncludePath(true);
  428. INCLUDEPATH;
  429. }
  430. if ($targetDirLoader) {
  431. $file .= <<<REGISTER_AUTOLOAD
  432. spl_autoload_register(array('ComposerAutoloaderInit$suffix', 'autoload'), true, true);
  433. REGISTER_AUTOLOAD;
  434. }
  435. $file .= <<<REGISTER_LOADER
  436. \$loader->register($prependAutoloader);
  437. REGISTER_LOADER;
  438. if ($useIncludeFiles) {
  439. $file .= <<<'INCLUDE_FILES'
  440. $includeFiles = require __DIR__ . '/autoload_files.php';
  441. foreach ($includeFiles as $file) {
  442. require $file;
  443. }
  444. INCLUDE_FILES;
  445. }
  446. $file .= <<<METHOD_FOOTER
  447. return \$loader;
  448. }
  449. METHOD_FOOTER;
  450. $file .= $targetDirLoader;
  451. return $file . <<<FOOTER
  452. }
  453. FOOTER;
  454. }
  455. protected function parseAutoloadsType(array $packageMap, $type, PackageInterface $mainPackage)
  456. {
  457. $autoloads = array();
  458. foreach ($packageMap as $item) {
  459. list($package, $installPath) = $item;
  460. $autoload = $package->getAutoload();
  461. // skip misconfigured packages
  462. if (!isset($autoload[$type]) || !is_array($autoload[$type])) {
  463. continue;
  464. }
  465. if (null !== $package->getTargetDir() && $package !== $mainPackage) {
  466. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  467. }
  468. foreach ($autoload[$type] as $namespace => $paths) {
  469. foreach ((array) $paths as $path) {
  470. // remove target-dir from file paths of the root package
  471. if ($type === 'files' && $package === $mainPackage && $package->getTargetDir() && !is_readable($installPath.'/'.$path)) {
  472. $targetDir = str_replace('\\<dirsep\\>', '[\\\\/]', preg_quote(str_replace(array('/', '\\'), '<dirsep>', $package->getTargetDir())));
  473. $path = ltrim(preg_replace('{^'.$targetDir.'}', '', ltrim($path, '\\/')), '\\/');
  474. }
  475. // add target-dir from file paths that don't have it
  476. if ($type === 'files' && $package !== $mainPackage && $package->getTargetDir() && !is_readable($installPath.'/'.$path)) {
  477. $path = $package->getTargetDir() . '/' . $path;
  478. }
  479. // remove target-dir from classmap entries of the root package
  480. if ($type === 'classmap' && $package === $mainPackage && $package->getTargetDir() && !is_readable($installPath.'/'.$path)) {
  481. $targetDir = str_replace('\\<dirsep\\>', '[\\\\/]', preg_quote(str_replace(array('/', '\\'), '<dirsep>', $package->getTargetDir())));
  482. $path = ltrim(preg_replace('{^'.$targetDir.'}', '', ltrim($path, '\\/')), '\\/');
  483. }
  484. // add target-dir to classmap entries that don't have it
  485. if ($type === 'classmap' && $package !== $mainPackage && $package->getTargetDir() && !is_readable($installPath.'/'.$path)) {
  486. $path = $package->getTargetDir() . '/' . $path;
  487. }
  488. if (empty($installPath)) {
  489. $autoloads[$namespace][] = empty($path) ? '.' : $path;
  490. } else {
  491. $autoloads[$namespace][] = $installPath.'/'.$path;
  492. }
  493. }
  494. }
  495. }
  496. return $autoloads;
  497. }
  498. protected function sortPackageMap(array $packageMap)
  499. {
  500. $positions = array();
  501. $names = array();
  502. $indexes = array();
  503. foreach ($packageMap as $position => $item) {
  504. $mainName = $item[0]->getName();
  505. $names = array_merge(array_fill_keys($item[0]->getNames(), $mainName), $names);
  506. $names[$mainName] = $mainName;
  507. $indexes[$mainName] = $positions[$mainName] = $position;
  508. }
  509. foreach ($packageMap as $item) {
  510. $position = $positions[$item[0]->getName()];
  511. foreach (array_merge($item[0]->getRequires(), $item[0]->getDevRequires()) as $link) {
  512. $target = $link->getTarget();
  513. if (!isset($names[$target])) {
  514. continue;
  515. }
  516. $target = $names[$target];
  517. if ($positions[$target] <= $position) {
  518. continue;
  519. }
  520. foreach ($positions as $key => $value) {
  521. if ($value >= $position) {
  522. break;
  523. }
  524. $positions[$key]--;
  525. }
  526. $positions[$target] = $position - 1;
  527. }
  528. asort($positions);
  529. }
  530. $sortedPackageMap = array();
  531. foreach (array_keys($positions) as $packageName) {
  532. $sortedPackageMap[] = $packageMap[$indexes[$packageName]];
  533. }
  534. return $sortedPackageMap;
  535. }
  536. }