PearInstaller.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. use Composer\Util\ProcessExecutor;
  18. /**
  19. * Package installation manager.
  20. *
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  23. */
  24. class PearInstaller extends LibraryInstaller
  25. {
  26. /**
  27. * Initializes library installer.
  28. *
  29. * @param IOInterface $io io instance
  30. * @param Composer $composer
  31. * @param string $type package type that this installer handles
  32. */
  33. public function __construct(IOInterface $io, Composer $composer, $type = 'pear-library')
  34. {
  35. parent::__construct($io, $composer, $type);
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  41. {
  42. $this->uninstall($repo, $initial);
  43. $this->install($repo, $target);
  44. }
  45. protected function installCode(PackageInterface $package)
  46. {
  47. parent::installCode($package);
  48. parent::initializeBinDir();
  49. $isWindows = defined('PHP_WINDOWS_VERSION_BUILD');
  50. $php_bin = $this->binDir . ($isWindows ? '/composer-php.bat' : '/composer-php');
  51. if (!$isWindows) {
  52. $php_bin = '/usr/bin/env ' . $php_bin;
  53. }
  54. $installPath = $this->getInstallPath($package);
  55. $vars = array(
  56. 'os' => $isWindows ? 'windows' : 'linux',
  57. 'php_bin' => $php_bin,
  58. 'pear_php' => $installPath,
  59. 'php_dir' => $installPath,
  60. 'bin_dir' => $installPath . '/bin',
  61. 'data_dir' => $installPath . '/data',
  62. 'version' => $package->getPrettyVersion(),
  63. );
  64. $packageArchive = $this->getInstallPath($package).'/'.pathinfo($package->getDistUrl(), PATHINFO_BASENAME);
  65. $pearExtractor = new PearPackageExtractor($packageArchive);
  66. $pearExtractor->extractTo($this->getInstallPath($package), array('php' => '/', 'script' => '/bin', 'data' => '/data'), $vars);
  67. $this->io->writeError(' Cleaning up', true, IOInterface::VERBOSE);
  68. $this->filesystem->unlink($packageArchive);
  69. }
  70. protected function getBinaries(PackageInterface $package)
  71. {
  72. $binariesPath = $this->getInstallPath($package) . '/bin/';
  73. $binaries = array();
  74. if (file_exists($binariesPath)) {
  75. foreach (new \FilesystemIterator($binariesPath, \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::CURRENT_AS_FILEINFO) as $fileName => $value) {
  76. if (!$value->isDir()) {
  77. $binaries[] = 'bin/'.$fileName;
  78. }
  79. }
  80. }
  81. return $binaries;
  82. }
  83. protected function initializeBinDir()
  84. {
  85. parent::initializeBinDir();
  86. file_put_contents($this->binDir.'/composer-php', $this->generateUnixyPhpProxyCode());
  87. @chmod($this->binDir.'/composer-php', 0777);
  88. file_put_contents($this->binDir.'/composer-php.bat', $this->generateWindowsPhpProxyCode());
  89. @chmod($this->binDir.'/composer-php.bat', 0777);
  90. }
  91. protected function generateWindowsProxyCode($bin, $link)
  92. {
  93. $binPath = $this->filesystem->findShortestPath($link, $bin);
  94. if ('.bat' === substr($bin, -4)) {
  95. $caller = 'call';
  96. } else {
  97. $handle = fopen($bin, 'r');
  98. $line = fgets($handle);
  99. fclose($handle);
  100. if (preg_match('{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m', $line, $match)) {
  101. $caller = trim($match[1]);
  102. } else {
  103. $caller = 'php';
  104. }
  105. if ($caller === 'php') {
  106. return "@echo off\r\n".
  107. "pushd .\r\n".
  108. "cd %~dp0\r\n".
  109. "set PHP_PROXY=%CD%\\composer-php.bat\r\n".
  110. "cd ".ProcessExecutor::escape(dirname($binPath))."\r\n".
  111. "set BIN_TARGET=%CD%\\".basename($binPath)."\r\n".
  112. "popd\r\n".
  113. "%PHP_PROXY% \"%BIN_TARGET%\" %*\r\n";
  114. }
  115. }
  116. return "@echo off\r\n".
  117. "pushd .\r\n".
  118. "cd %~dp0\r\n".
  119. "cd ".ProcessExecutor::escape(dirname($binPath))."\r\n".
  120. "set BIN_TARGET=%CD%\\".basename($binPath)."\r\n".
  121. "popd\r\n".
  122. $caller." \"%BIN_TARGET%\" %*\r\n";
  123. }
  124. private function generateWindowsPhpProxyCode()
  125. {
  126. $binToVendor = $this->filesystem->findShortestPath($this->binDir, $this->vendorDir, true);
  127. return
  128. "@echo off\r\n" .
  129. "setlocal enabledelayedexpansion\r\n" .
  130. "set BIN_DIR=%~dp0\r\n" .
  131. "set VENDOR_DIR=%BIN_DIR%\\".$binToVendor."\r\n" .
  132. "set DIRS=.\r\n" .
  133. "FOR /D %%V IN (%VENDOR_DIR%\\*) DO (\r\n" .
  134. " FOR /D %%P IN (%%V\\*) DO (\r\n" .
  135. " set DIRS=!DIRS!;%%~fP\r\n" .
  136. " )\r\n" .
  137. ")\r\n" .
  138. "php.exe -d include_path=!DIRS! %*\r\n";
  139. }
  140. private function generateUnixyPhpProxyCode()
  141. {
  142. $binToVendor = $this->filesystem->findShortestPath($this->binDir, $this->vendorDir, true);
  143. return
  144. "#!/usr/bin/env sh\n".
  145. "SRC_DIR=`pwd`\n".
  146. "BIN_DIR=`dirname $0`\n".
  147. "VENDOR_DIR=\$BIN_DIR/".escapeshellarg($binToVendor)."\n".
  148. "DIRS=\"\"\n".
  149. "for vendor in \$VENDOR_DIR/*; do\n".
  150. " if [ -d \"\$vendor\" ]; then\n".
  151. " for package in \$vendor/*; do\n".
  152. " if [ -d \"\$package\" ]; then\n".
  153. " DIRS=\"\${DIRS}:\${package}\"\n".
  154. " fi\n".
  155. " done\n".
  156. " fi\n".
  157. "done\n".
  158. "php -d include_path=\".\$DIRS\" $@\n";
  159. }
  160. }