PearInstaller.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\IO\IOInterface;
  13. use Composer\Composer;
  14. use Composer\Downloader\PearPackageExtractor;
  15. use Composer\Repository\InstalledRepositoryInterface;
  16. use Composer\Package\PackageInterface;
  17. /**
  18. * Package installation manager.
  19. *
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  22. */
  23. class PearInstaller extends LibraryInstaller
  24. {
  25. /**
  26. * Initializes library installer.
  27. *
  28. * @param IOInterface $io io instance
  29. * @param Composer $composer
  30. * @param string $type package type that this installer handles
  31. */
  32. public function __construct(IOInterface $io, Composer $composer, $type = 'pear-library')
  33. {
  34. parent::__construct($io, $composer, $type);
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  40. {
  41. $this->uninstall($repo, $initial);
  42. $this->install($repo, $target);
  43. }
  44. protected function installCode(PackageInterface $package)
  45. {
  46. parent::installCode($package);
  47. parent::initializeBinDir();
  48. $isWindows = defined('PHP_WINDOWS_VERSION_BUILD') ? true : false;
  49. $php_bin = $this->binDir . ($isWindows ? '/composer-php.bat' : '/composer-php');
  50. $installPath = $this->getInstallPath($package);
  51. $vars = array(
  52. 'os' => $isWindows ? 'windows' : 'linux',
  53. 'php_bin' => $php_bin,
  54. 'pear_php' => $installPath,
  55. 'php_dir' => $installPath,
  56. 'bin_dir' => $installPath . '/bin',
  57. 'data_dir' => $installPath . '/data',
  58. 'version' => $package->getPrettyVersion(),
  59. );
  60. $packageArchive = $this->getInstallPath($package).'/'.pathinfo($package->getDistUrl(), PATHINFO_BASENAME);
  61. $pearExtractor = new PearPackageExtractor($packageArchive);
  62. $pearExtractor->extractTo($this->getInstallPath($package), array('php' => '/', 'script' => '/bin', 'data' => '/data'), $vars);
  63. if ($this->io->isVerbose()) {
  64. $this->io->write(' Cleaning up');
  65. }
  66. unlink($packageArchive);
  67. }
  68. protected function getBinaries(PackageInterface $package)
  69. {
  70. $binariesPath = $this->getInstallPath($package) . '/bin/';
  71. $binaries = array();
  72. if (file_exists($binariesPath)) {
  73. foreach (new \FilesystemIterator($binariesPath, \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::CURRENT_AS_FILEINFO) as $fileName => $value) {
  74. if (!$value->isDir()) {
  75. $binaries[] = 'bin/'.$fileName;
  76. }
  77. }
  78. }
  79. return $binaries;
  80. }
  81. protected function initializeBinDir()
  82. {
  83. parent::initializeBinDir();
  84. file_put_contents($this->binDir.'/composer-php', $this->generateUnixyPhpProxyCode());
  85. chmod($this->binDir.'/composer-php', 0777);
  86. file_put_contents($this->binDir.'/composer-php.bat', $this->generateWindowsPhpProxyCode());
  87. chmod($this->binDir.'/composer-php.bat', 0777);
  88. }
  89. private function generateWindowsPhpProxyCode()
  90. {
  91. return
  92. "@echo off\r\n" .
  93. "setlocal enabledelayedexpansion\r\n" .
  94. "set BIN_DIR=%~dp0\r\n" .
  95. "set VENDOR_DIR=%BIN_DIR%..\\\r\n" .
  96. " set DIRS=.\r\n" .
  97. "FOR /D %%V IN (%VENDOR_DIR%*) DO (\r\n" .
  98. " FOR /D %%P IN (%%V\\*) DO (\r\n" .
  99. " set DIRS=!DIRS!;%%~fP\r\n" .
  100. " )\r\n" .
  101. ")\r\n" .
  102. "php.exe -d include_path=!DIRS! %*\r\n";
  103. }
  104. private function generateUnixyPhpProxyCode()
  105. {
  106. return
  107. "#!/usr/bin/env sh\n".
  108. "SRC_DIR=`pwd`\n".
  109. "BIN_DIR=`dirname $(readlink -f $0)`\n".
  110. "VENDOR_DIR=`dirname \$BIN_DIR`\n".
  111. "cd \$BIN_DIR\n".
  112. "DIRS=\"\"\n".
  113. "for vendor in \$VENDOR_DIR/*; do\n".
  114. " if [ -d \"\$vendor\" ]; then\n".
  115. " for package in \$vendor/*; do\n".
  116. " if [ -d \"\$package\" ]; then\n".
  117. " DIRS=\"\${DIRS}:\${package}\"\n".
  118. " fi\n".
  119. " done\n".
  120. " fi\n".
  121. "done\n".
  122. "cd \$SRC_DIR\n".
  123. "`which php` -d include_path=\".\$DIRS\" $@\n";
  124. }
  125. }