LibraryInstaller.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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\Repository\InstalledRepositoryInterface;
  15. use Composer\Package\PackageInterface;
  16. use Composer\Util\Filesystem;
  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 LibraryInstaller implements InstallerInterface
  24. {
  25. protected $composer;
  26. protected $vendorDir;
  27. protected $binDir;
  28. protected $downloadManager;
  29. protected $io;
  30. protected $type;
  31. protected $filesystem;
  32. /**
  33. * Initializes library installer.
  34. *
  35. * @param IOInterface $io
  36. * @param Composer $composer
  37. * @param string $type
  38. * @param Filesystem $filesystem
  39. */
  40. public function __construct(IOInterface $io, Composer $composer, $type = 'library', Filesystem $filesystem = null)
  41. {
  42. $this->composer = $composer;
  43. $this->downloadManager = $composer->getDownloadManager();
  44. $this->io = $io;
  45. $this->type = $type;
  46. $this->filesystem = $filesystem ?: new Filesystem();
  47. $this->vendorDir = rtrim($composer->getConfig()->get('vendor-dir'), '/');
  48. $this->binDir = rtrim($composer->getConfig()->get('bin-dir'), '/');
  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->installCode($package);
  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. $this->removeBinaries($initial);
  91. $this->updateCode($initial, $target);
  92. $this->installBinaries($target);
  93. $repo->removePackage($initial);
  94. if (!$repo->hasPackage($target)) {
  95. $repo->addPackage(clone $target);
  96. }
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
  102. {
  103. if (!$repo->hasPackage($package)) {
  104. throw new \InvalidArgumentException('Package is not installed: '.$package);
  105. }
  106. $this->removeCode($package);
  107. $this->removeBinaries($package);
  108. $repo->removePackage($package);
  109. $downloadPath = $this->getPackageBasePath($package);
  110. if (strpos($package->getName(), '/')) {
  111. $packageVendorDir = dirname($downloadPath);
  112. if (is_dir($packageVendorDir) && !glob($packageVendorDir.'/*')) {
  113. @rmdir($packageVendorDir);
  114. }
  115. }
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function getInstallPath(PackageInterface $package)
  121. {
  122. $targetDir = $package->getTargetDir();
  123. return $this->getPackageBasePath($package) . ($targetDir ? '/'.$targetDir : '');
  124. }
  125. protected function getPackageBasePath(PackageInterface $package)
  126. {
  127. $this->initializeVendorDir();
  128. return ($this->vendorDir ? $this->vendorDir.'/' : '') . $package->getPrettyName();
  129. }
  130. protected function installCode(PackageInterface $package)
  131. {
  132. $downloadPath = $this->getInstallPath($package);
  133. $this->downloadManager->download($package, $downloadPath);
  134. }
  135. protected function updateCode(PackageInterface $initial, PackageInterface $target)
  136. {
  137. $initialDownloadPath = $this->getInstallPath($initial);
  138. $targetDownloadPath = $this->getInstallPath($target);
  139. if ($targetDownloadPath !== $initialDownloadPath) {
  140. // if the target is part of the initial dir, we force a remove + install
  141. // to avoid the rename wiping the target dir as part of the initial dir cleanup
  142. if (strpos($initialDownloadPath, $targetDownloadPath) === 0) {
  143. $this->removeCode($initial);
  144. $this->installCode($target);
  145. return;
  146. }
  147. $this->filesystem->rename($initialDownloadPath, $targetDownloadPath);
  148. }
  149. $this->downloadManager->update($initial, $target, $targetDownloadPath);
  150. }
  151. protected function removeCode(PackageInterface $package)
  152. {
  153. $downloadPath = $this->getPackageBasePath($package);
  154. $this->downloadManager->remove($package, $downloadPath);
  155. }
  156. protected function getBinaries(PackageInterface $package)
  157. {
  158. return $package->getBinaries();
  159. }
  160. protected function installBinaries(PackageInterface $package)
  161. {
  162. $binaries = $this->getBinaries($package);
  163. if (!$binaries) {
  164. return;
  165. }
  166. foreach ($binaries as $bin) {
  167. $binPath = $this->getInstallPath($package).'/'.$bin;
  168. if (!file_exists($binPath)) {
  169. $this->io->write(' <warning>Skipped installation of '.$bin.' for package '.$package->getName().': file not found in package</warning>');
  170. continue;
  171. }
  172. $this->initializeBinDir();
  173. $link = $this->binDir.'/'.basename($bin);
  174. if (file_exists($link)) {
  175. if (is_link($link)) {
  176. // likely leftover from a previous install, make sure
  177. // that the target is still executable in case this
  178. // is a fresh install of the vendor.
  179. @chmod($link, 0777 & ~umask());
  180. }
  181. $this->io->write(' Skipped installation of '.$bin.' for package '.$package->getName().': name conflicts with an existing file');
  182. continue;
  183. }
  184. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  185. // add unixy support for cygwin and similar environments
  186. if ('.bat' !== substr($binPath, -4)) {
  187. file_put_contents($link, $this->generateUnixyProxyCode($binPath, $link));
  188. @chmod($link, 0777 & ~umask());
  189. $link .= '.bat';
  190. if (file_exists($link)) {
  191. $this->io->write(' Skipped installation of '.$bin.'.bat proxy for package '.$package->getName().': a .bat proxy was already installed');
  192. }
  193. }
  194. if (!file_exists($link)) {
  195. file_put_contents($link, $this->generateWindowsProxyCode($binPath, $link));
  196. }
  197. } else {
  198. $cwd = getcwd();
  199. try {
  200. // under linux symlinks are not always supported for example
  201. // when using it in smbfs mounted folder
  202. $relativeBin = $this->filesystem->findShortestPath($link, $binPath);
  203. chdir(dirname($link));
  204. if (false === symlink($relativeBin, $link)) {
  205. throw new \ErrorException();
  206. }
  207. } catch (\ErrorException $e) {
  208. file_put_contents($link, $this->generateUnixyProxyCode($binPath, $link));
  209. }
  210. chdir($cwd);
  211. }
  212. @chmod($link, 0777 & ~umask());
  213. }
  214. }
  215. protected function removeBinaries(PackageInterface $package)
  216. {
  217. $binaries = $this->getBinaries($package);
  218. if (!$binaries) {
  219. return;
  220. }
  221. foreach ($binaries as $bin) {
  222. $link = $this->binDir.'/'.basename($bin);
  223. if (is_link($link) || file_exists($link)) {
  224. unlink($link);
  225. }
  226. if (file_exists($link.'.bat')) {
  227. unlink($link.'.bat');
  228. }
  229. }
  230. }
  231. protected function initializeVendorDir()
  232. {
  233. $this->filesystem->ensureDirectoryExists($this->vendorDir);
  234. $this->vendorDir = realpath($this->vendorDir);
  235. }
  236. protected function initializeBinDir()
  237. {
  238. $this->filesystem->ensureDirectoryExists($this->binDir);
  239. $this->binDir = realpath($this->binDir);
  240. }
  241. protected function generateWindowsProxyCode($bin, $link)
  242. {
  243. $binPath = $this->filesystem->findShortestPath($link, $bin);
  244. if ('.bat' === substr($bin, -4) || '.exe' === substr($bin, -4)) {
  245. $caller = 'call';
  246. } else {
  247. $handle = fopen($bin, 'r');
  248. $line = fgets($handle);
  249. fclose($handle);
  250. if (preg_match('{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m', $line, $match)) {
  251. $caller = trim($match[1]);
  252. } else {
  253. $caller = 'php';
  254. }
  255. }
  256. return "@ECHO OFF\r\n".
  257. "SET BIN_TARGET=%~dp0/".trim(escapeshellarg($binPath), '"')."\r\n".
  258. "{$caller} \"%BIN_TARGET%\" %*\r\n";
  259. }
  260. protected function generateUnixyProxyCode($bin, $link)
  261. {
  262. $binPath = $this->filesystem->findShortestPath($link, $bin);
  263. return "#!/usr/bin/env sh\n".
  264. 'SRC_DIR="`pwd`"'."\n".
  265. 'cd "`dirname "$0"`"'."\n".
  266. 'cd '.escapeshellarg(dirname($binPath))."\n".
  267. 'BIN_TARGET="`pwd`/'.basename($binPath)."\"\n".
  268. 'cd "$SRC_DIR"'."\n".
  269. '"$BIN_TARGET" "$@"'."\n";
  270. }
  271. }