InstallerInstaller.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Installer;
  12. use Composer\Composer;
  13. use Composer\IO\IOInterface;
  14. use Composer\Autoload\AutoloadGenerator;
  15. use Composer\Repository\InstalledRepositoryInterface;
  16. use Composer\Package\PackageInterface;
  17. /**
  18. * Installer installation manager.
  19. *
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class InstallerInstaller extends LibraryInstaller
  23. {
  24. private $installationManager;
  25. private static $classCounter = 0;
  26. /**
  27. * @param IOInterface $io
  28. * @param Composer $composer
  29. */
  30. public function __construct(IOInterface $io, Composer $composer, $type = 'library')
  31. {
  32. parent::__construct($io, $composer, 'composer-installer');
  33. $this->installationManager = $composer->getInstallationManager();
  34. foreach ($composer->getRepositoryManager()->getLocalRepositories() as $repo) {
  35. foreach ($repo->getPackages() as $package) {
  36. if ('composer-installer' === $package->getType()) {
  37. $this->registerInstaller($package);
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  46. {
  47. $extra = $package->getExtra();
  48. if (empty($extra['class'])) {
  49. throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-installer packages should have a class defined in their extra key to be usable.');
  50. }
  51. parent::install($repo, $package);
  52. $this->registerInstaller($package);
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  58. {
  59. $extra = $target->getExtra();
  60. if (empty($extra['class'])) {
  61. throw new \UnexpectedValueException('Error while installing '.$target->getPrettyName().', composer-installer packages should have a class defined in their extra key to be usable.');
  62. }
  63. parent::update($repo, $initial, $target);
  64. $this->registerInstaller($target);
  65. }
  66. private function registerInstaller(PackageInterface $package)
  67. {
  68. $downloadPath = $this->getInstallPath($package);
  69. $extra = $package->getExtra();
  70. $classes = is_array($extra['class']) ? $extra['class'] : array($extra['class']);
  71. $generator = new AutoloadGenerator;
  72. $map = $generator->parseAutoloads(array(array($package, $downloadPath)));
  73. $classLoader = $generator->createLoader($map);
  74. $classLoader->register();
  75. foreach ($classes as $class) {
  76. if (class_exists($class, false)) {
  77. $code = file_get_contents($classLoader->findFile($class));
  78. $code = preg_replace('{^class\s+(\S+)}mi', 'class $1_composer_tmp'.self::$classCounter, $code);
  79. eval('?>'.$code);
  80. $class .= '_composer_tmp'.self::$classCounter;
  81. self::$classCounter++;
  82. }
  83. $installer = new $class($this->io, $this->composer);
  84. $this->installationManager->addInstaller($installer);
  85. }
  86. }
  87. }