InstallerInstaller.php 3.4 KB

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