PearInstaller.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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');
  49. $php_bin = $this->binDir . ($isWindows ? '/composer-php.bat' : '/composer-php');
  50. if (!$isWindows) {
  51. $php_bin = '/usr/bin/env ' . $php_bin;
  52. }
  53. $installPath = $this->getInstallPath($package);
  54. $vars = array(
  55. 'os' => $isWindows ? 'windows' : 'linux',
  56. 'php_bin' => $php_bin,
  57. 'pear_php' => $installPath,
  58. 'php_dir' => $installPath,
  59. 'bin_dir' => $installPath . '/bin',
  60. 'data_dir' => $installPath . '/data',
  61. 'version' => $package->getPrettyVersion(),
  62. );
  63. $packageArchive = $this->getInstallPath($package).'/'.pathinfo($package->getDistUrl(), PATHINFO_BASENAME);
  64. $pearExtractor = new PearPackageExtractor($packageArchive);
  65. $pearExtractor->extractTo($this->getInstallPath($package), array('php' => '/', 'script' => '/bin', 'data' => '/data'), $vars);
  66. if ($this->io->isVerbose()) {
  67. $this->io->write(' Cleaning up');
  68. }
  69. unlink($packageArchive);
  70. }
  71. protected function getBinaries(PackageInterface $package)
  72. {
  73. $binariesPath = $this->getInstallPath($package) . '/bin/';
  74. $binaries = array();
  75. if (file_exists($binariesPath)) {
  76. foreach (new \FilesystemIterator($binariesPath, \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::CURRENT_AS_FILEINFO) as $fileName => $value) {
  77. if (!$value->isDir()) {
  78. $binaries[] = 'bin/'.$fileName;
  79. }
  80. }
  81. }
  82. return $binaries;
  83. }
  84. protected function initializeBinDir()
  85. {
  86. parent::initializeBinDir();
  87. file_put_contents($this->binDir.'/composer-php', $this->generateUnixyPhpProxyCode());
  88. chmod($this->binDir.'/composer-php', 0777);
  89. file_put_contents($this->binDir.'/composer-php.bat', $this->generateWindowsPhpProxyCode());
  90. chmod($this->binDir.'/composer-php.bat', 0777);
  91. }
  92. protected function generateWindowsProxyCode($bin, $link)
  93. {
  94. $binPath = $this->filesystem->findShortestPath($link, $bin);
  95. if ('.bat' === substr($bin, -4)) {
  96. $caller = 'call';
  97. } else {
  98. $handle = fopen($bin, 'r');
  99. $line = fgets($handle);
  100. fclose($handle);
  101. if (preg_match('{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m', $line, $match)) {
  102. $caller = trim($match[1]);
  103. } else {
  104. $caller = 'php';
  105. }
  106. if ($caller === 'php') {
  107. return "@echo off\r\n".
  108. "pushd .\r\n".
  109. "cd %~dp0\r\n".
  110. "set PHP_PROXY=%CD%\\composer-php.bat\r\n".
  111. "cd ".escapeshellarg(dirname($binPath))."\r\n".
  112. "set BIN_TARGET=%CD%\\".basename($binPath)."\r\n".
  113. "popd\r\n".
  114. "%PHP_PROXY% \"%BIN_TARGET%\" %*\r\n";
  115. }
  116. }
  117. return "@echo off\r\n".
  118. "pushd .\r\n".
  119. "cd %~dp0\r\n".
  120. "cd ".escapeshellarg(dirname($binPath))."\r\n".
  121. "set BIN_TARGET=%CD%\\".basename($binPath)."\r\n".
  122. "popd\r\n".
  123. $caller." \"%BIN_TARGET%\" %*\r\n";
  124. }
  125. private function generateWindowsPhpProxyCode()
  126. {
  127. $binToVendor = $this->filesystem->findShortestPath($this->binDir, $this->vendorDir, true);
  128. return
  129. "@echo off\r\n" .
  130. "setlocal enabledelayedexpansion\r\n" .
  131. "set BIN_DIR=%~dp0\r\n" .
  132. "set VENDOR_DIR=%BIN_DIR%\\".$binToVendor."\r\n" .
  133. "set DIRS=.\r\n" .
  134. "FOR /D %%V IN (%VENDOR_DIR%\\*) DO (\r\n" .
  135. " FOR /D %%P IN (%%V\\*) DO (\r\n" .
  136. " set DIRS=!DIRS!;%%~fP\r\n" .
  137. " )\r\n" .
  138. ")\r\n" .
  139. "php.exe -d include_path=!DIRS! %*\r\n";
  140. }
  141. private function generateUnixyPhpProxyCode()
  142. {
  143. $binToVendor = $this->filesystem->findShortestPath($this->binDir, $this->vendorDir, true);
  144. return
  145. "#!/usr/bin/env sh\n".
  146. "SRC_DIR=`pwd`\n".
  147. "BIN_DIR=`dirname $0`\n".
  148. "VENDOR_DIR=\$BIN_DIR/".escapeshellarg($binToVendor)."\n".
  149. "DIRS=\"\"\n".
  150. "for vendor in \$VENDOR_DIR/*; do\n".
  151. " if [ -d \"\$vendor\" ]; then\n".
  152. " for package in \$vendor/*; do\n".
  153. " if [ -d \"\$package\" ]; then\n".
  154. " DIRS=\"\${DIRS}:\${package}\"\n".
  155. " fi\n".
  156. " done\n".
  157. " fi\n".
  158. "done\n".
  159. "php -d include_path=\".\$DIRS\" $@\n";
  160. }
  161. }