InstallerInstaller.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Autoload\AutoloadGenerator;
  13. use Composer\Downloader\DownloadManager;
  14. use Composer\Repository\WritableRepositoryInterface;
  15. use Composer\DependencyResolver\Operation\OperationInterface;
  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 string $vendorDir relative path for packages home
  28. * @param string $binDir relative path for binaries
  29. * @param DownloadManager $dm download manager
  30. * @param WritableRepositoryInterface $repository repository controller
  31. */
  32. public function __construct($vendorDir, $binDir, DownloadManager $dm, WritableRepositoryInterface $repository, InstallationManager $im)
  33. {
  34. parent::__construct($vendorDir, $binDir, $dm, $repository, 'composer-installer');
  35. $this->installationManager = $im;
  36. foreach ($repository->getPackages() as $package) {
  37. if ('composer-installer' === $package->getType()) {
  38. $this->registerInstaller($package);
  39. }
  40. }
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function install(PackageInterface $package)
  46. {
  47. $extra = $package->getExtra();
  48. if (empty($extra['class'])) {
  49. throw new \UnexpectedValueException('Error while installing '.$target->getPrettyName().', composer-installer packages should have a class defined in their extra key to be usable.');
  50. }
  51. parent::install($package);
  52. $this->registerInstaller($package);
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function update(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($initial, $target);
  64. $this->registerInstaller($target);
  65. }
  66. private function registerInstaller(PackageInterface $package)
  67. {
  68. $downloadPath = $this->getInstallPath($package);
  69. $extra = $package->getExtra();
  70. $class = $extra['class'];
  71. $generator = new AutoloadGenerator;
  72. $map = $generator->parseAutoloads(array(array($package, $downloadPath)));
  73. $classLoader = $generator->createLoader($map);
  74. $classLoader->register();
  75. if (class_exists($class, false)) {
  76. $code = file_get_contents($classLoader->findFile($class));
  77. $code = preg_replace('{^class\s+(\S+)}mi', 'class $1_composer_tmp'.self::$classCounter, $code);
  78. eval('?>'.$code);
  79. $class .= '_composer_tmp'.self::$classCounter;
  80. self::$classCounter++;
  81. }
  82. $extra = $package->getExtra();
  83. $installer = new $class($this->vendorDir, $this->binDir, $this->downloadManager, $this->repository);
  84. $this->installationManager->addInstaller($installer);
  85. }
  86. }