InstallerInstaller.php 3.4 KB

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