LibraryInstaller.php 9.2 KB

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