LibraryInstaller.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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\Downloader\DownloadManager;
  14. use Composer\Repository\InstalledRepositoryInterface;
  15. use Composer\DependencyResolver\Operation\OperationInterface;
  16. use Composer\Package\PackageInterface;
  17. use Composer\Util\Filesystem;
  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 LibraryInstaller implements InstallerInterface
  25. {
  26. protected $vendorDir;
  27. protected $binDir;
  28. protected $downloadManager;
  29. protected $io;
  30. private $type;
  31. private $filesystem;
  32. /**
  33. * Initializes library installer.
  34. *
  35. * @param string $vendorDir relative path for packages home
  36. * @param string $binDir relative path for binaries
  37. * @param DownloadManager $dm download manager
  38. * @param IOInterface $io io instance
  39. * @param string $type package type that this installer handles
  40. */
  41. public function __construct($vendorDir, $binDir, DownloadManager $dm, IOInterface $io, $type = 'library')
  42. {
  43. $this->downloadManager = $dm;
  44. $this->io = $io;
  45. $this->type = $type;
  46. $this->filesystem = new Filesystem();
  47. $this->vendorDir = rtrim($vendorDir, '/');
  48. $this->binDir = rtrim($binDir, '/');
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function supports($packageType)
  54. {
  55. return $packageType === $this->type || null === $this->type;
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
  61. {
  62. return $repo->hasPackage($package) && is_readable($this->getInstallPath($package));
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  68. {
  69. $this->initializeVendorDir();
  70. $downloadPath = $this->getInstallPath($package);
  71. // remove the binaries if it appears the package files are missing
  72. if (!is_readable($downloadPath) && $repo->hasPackage($package)) {
  73. $this->removeBinaries($package);
  74. }
  75. $this->downloadManager->download($package, $downloadPath);
  76. $this->installBinaries($package);
  77. if (!$repo->hasPackage($package)) {
  78. $repo->addPackage(clone $package);
  79. }
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  85. {
  86. if (!$repo->hasPackage($initial)) {
  87. throw new \InvalidArgumentException('Package is not installed: '.$initial);
  88. }
  89. $this->initializeVendorDir();
  90. $downloadPath = $this->getInstallPath($initial);
  91. $this->removeBinaries($initial);
  92. $this->downloadManager->update($initial, $target, $downloadPath);
  93. $this->installBinaries($target);
  94. $repo->removePackage($initial);
  95. if (!$repo->hasPackage($target)) {
  96. $repo->addPackage(clone $target);
  97. }
  98. }
  99. /**
  100. * {@inheritDoc}
  101. */
  102. public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
  103. {
  104. if (!$repo->hasPackage($package)) {
  105. // TODO throw exception again here, when update is fixed and we don't have to remove+install (see #125)
  106. return;
  107. throw new \InvalidArgumentException('Package is not installed: '.$package);
  108. }
  109. $downloadPath = $this->getInstallPath($package);
  110. $this->downloadManager->remove($package, $downloadPath);
  111. $this->removeBinaries($package);
  112. $repo->removePackage($package);
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. public function getInstallPath(PackageInterface $package)
  118. {
  119. $targetDir = $package->getTargetDir();
  120. return ($this->vendorDir ? $this->vendorDir.'/' : '') . $package->getPrettyName() . ($targetDir ? '/'.$targetDir : '');
  121. }
  122. protected function installBinaries(PackageInterface $package)
  123. {
  124. if (!$package->getBinaries()) {
  125. return;
  126. }
  127. foreach ($package->getBinaries() as $bin) {
  128. $this->initializeBinDir();
  129. $link = $this->binDir.'/'.basename($bin);
  130. if (file_exists($link)) {
  131. if (is_link($link)) {
  132. // likely leftover from a previous install, make sure
  133. // that the target is still executable in case this
  134. // is a fresh install of the vendor.
  135. chmod($link, 0777 & ~umask());
  136. }
  137. $this->io->write('Skipped installation of '.$bin.' for package '.$package->getName().', name conflicts with an existing file');
  138. continue;
  139. }
  140. $bin = $this->getInstallPath($package).'/'.$bin;
  141. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  142. // add unixy support for cygwin and similar environments
  143. if ('.bat' !== substr($bin, -4)) {
  144. file_put_contents($link, $this->generateUnixyProxyCode($bin, $link));
  145. chmod($link, 0777 & ~umask());
  146. $link .= '.bat';
  147. }
  148. file_put_contents($link, $this->generateWindowsProxyCode($bin, $link));
  149. } else {
  150. $cwd = getcwd();
  151. try {
  152. // under linux symlinks are not always supported for example
  153. // when using it in smbfs mounted folder
  154. $relativeBin = $this->filesystem->findShortestPath($link, $bin);
  155. chdir(dirname($link));
  156. symlink($relativeBin, $link);
  157. } catch (\ErrorException $e) {
  158. file_put_contents($link, $this->generateUnixyProxyCode($bin, $link));
  159. }
  160. chdir($cwd);
  161. }
  162. chmod($link, 0777 & ~umask());
  163. }
  164. }
  165. protected function removeBinaries(PackageInterface $package)
  166. {
  167. if (!$package->getBinaries()) {
  168. return;
  169. }
  170. foreach ($package->getBinaries() as $bin) {
  171. $link = $this->binDir.'/'.basename($bin);
  172. if (!file_exists($link)) {
  173. continue;
  174. }
  175. unlink($link);
  176. }
  177. }
  178. protected function initializeVendorDir()
  179. {
  180. $this->filesystem->ensureDirectoryExists($this->vendorDir);
  181. $this->vendorDir = realpath($this->vendorDir);
  182. }
  183. protected function initializeBinDir()
  184. {
  185. $this->filesystem->ensureDirectoryExists($this->binDir);
  186. $this->binDir = realpath($this->binDir);
  187. }
  188. private function generateWindowsProxyCode($bin, $link)
  189. {
  190. $binPath = $this->filesystem->findShortestPath($link, $bin);
  191. if ('.bat' === substr($bin, -4)) {
  192. $caller = 'call';
  193. } else {
  194. $handle = fopen($bin, 'r');
  195. $line = fgets($handle);
  196. fclose($handle);
  197. if (preg_match('{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m', $line, $match)) {
  198. $caller = $match[1];
  199. } else {
  200. $caller = 'php';
  201. }
  202. }
  203. return "@echo off\r\n".
  204. "pushd .\r\n".
  205. "cd %~dp0\r\n".
  206. "cd ".escapeshellarg(dirname($binPath))."\r\n".
  207. "set BIN_TARGET=%CD%\\".basename($binPath)."\r\n".
  208. "popd\r\n".
  209. $caller." %BIN_TARGET% %*\r\n";
  210. }
  211. private function generateUnixyProxyCode($bin, $link)
  212. {
  213. $binPath = $this->filesystem->findShortestPath($link, $bin);
  214. return "#!/usr/bin/env sh\n".
  215. 'SRC_DIR=`pwd`'."\n".
  216. 'cd `dirname "$0"`'."\n".
  217. 'cd '.escapeshellarg(dirname($binPath))."\n".
  218. 'BIN_TARGET=`pwd`/'.basename($binPath)."\n".
  219. 'cd $SRC_DIR'."\n".
  220. '$BIN_TARGET "$@"'."\n";
  221. }
  222. }