InstallerInstaller.php 3.5 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\Package\Package;
  14. use Composer\IO\IOInterface;
  15. use Composer\Autoload\AutoloadGenerator;
  16. use Composer\Repository\InstalledRepositoryInterface;
  17. use Composer\Package\PackageInterface;
  18. /**
  19. * Installer installation manager.
  20. *
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class InstallerInstaller extends LibraryInstaller
  24. {
  25. private $installationManager;
  26. private static $classCounter = 0;
  27. /**
  28. * Initializes Installer installer.
  29. *
  30. * @param IOInterface $io
  31. * @param Composer $composer
  32. * @param string $type
  33. */
  34. public function __construct(IOInterface $io, Composer $composer, $type = 'library')
  35. {
  36. parent::__construct($io, $composer, 'composer-installer');
  37. $this->installationManager = $composer->getInstallationManager();
  38. $repo = $composer->getRepositoryManager()->getLocalRepository();
  39. foreach ($repo->getPackages() as $package) {
  40. if ('composer-installer' === $package->getType()) {
  41. $this->registerInstaller($package);
  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 = $this->composer->getAutoloadGenerator();
  75. $map = $generator->parseAutoloads(array(array($package, $downloadPath)), new Package('dummy', '1.0.0.0', '1.0.0'));
  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. }