PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/session26/Mobile Management System/vendor/composer/composer/src/Composer/Installer/BinaryInstaller.php

https://gitlab.com/imamul68e/137619_PHP31
PHP | 212 lines | 139 code | 24 blank | 49 comment | 27 complexity | ce5d322af14cac54ec5fadaad840a8e7 MD5 | raw file
  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\Package\PackageInterface;
  14. use Composer\Util\Filesystem;
  15. use Composer\Util\Platform;
  16. use Composer\Util\ProcessExecutor;
  17. use Composer\Util\Silencer;
  18. /**
  19. * Message to handle installation of package "bin"/binaries
  20. *
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  23. * @author Helmut Hummel <info@helhum.io>
  24. */
  25. class BinaryInstaller
  26. {
  27. protected $binDir;
  28. protected $binCompat;
  29. protected $io;
  30. protected $filesystem;
  31. /**
  32. * @param IOInterface $io
  33. * @param string $binDir
  34. * @param string $binCompat
  35. * @param Filesystem $filesystem
  36. */
  37. public function __construct(IOInterface $io, $binDir, $binCompat, Filesystem $filesystem = null)
  38. {
  39. $this->binDir = $binDir;
  40. $this->binCompat = $binCompat;
  41. $this->io = $io;
  42. $this->filesystem = $filesystem ?: new Filesystem();
  43. }
  44. public function installBinaries(PackageInterface $package, $installPath, $warnOnOverwrite = true)
  45. {
  46. $binaries = $this->getBinaries($package);
  47. if (!$binaries) {
  48. return;
  49. }
  50. foreach ($binaries as $bin) {
  51. $binPath = $installPath.'/'.$bin;
  52. if (!file_exists($binPath)) {
  53. $this->io->writeError(' <warning>Skipped installation of bin '.$bin.' for package '.$package->getName().': file not found in package</warning>');
  54. continue;
  55. }
  56. // in case a custom installer returned a relative path for the
  57. // $package, we can now safely turn it into a absolute path (as we
  58. // already checked the binary's existence). The following helpers
  59. // will require absolute paths to work properly.
  60. $binPath = realpath($binPath);
  61. $this->initializeBinDir();
  62. $link = $this->binDir.'/'.basename($bin);
  63. if (file_exists($link)) {
  64. if (is_link($link)) {
  65. // likely leftover from a previous install, make sure
  66. // that the target is still executable in case this
  67. // is a fresh install of the vendor.
  68. Silencer::call('chmod', $link, 0777 & ~umask());
  69. }
  70. if ($warnOnOverwrite) {
  71. $this->io->writeError(' Skipped installation of bin '.$bin.' for package '.$package->getName().': name conflicts with an existing file');
  72. }
  73. continue;
  74. }
  75. if ($this->binCompat === "auto") {
  76. if (Platform::isWindows()) {
  77. $this->installFullBinaries($binPath, $link, $bin, $package);
  78. } else {
  79. $this->installSymlinkBinaries($binPath, $link);
  80. }
  81. } elseif ($this->binCompat === "full") {
  82. $this->installFullBinaries($binPath, $link, $bin, $package);
  83. }
  84. Silencer::call('chmod', $link, 0777 & ~umask());
  85. }
  86. }
  87. public function removeBinaries(PackageInterface $package)
  88. {
  89. $this->initializeBinDir();
  90. $binaries = $this->getBinaries($package);
  91. if (!$binaries) {
  92. return;
  93. }
  94. foreach ($binaries as $bin) {
  95. $link = $this->binDir.'/'.basename($bin);
  96. if (is_link($link) || file_exists($link)) {
  97. $this->filesystem->unlink($link);
  98. }
  99. if (file_exists($link.'.bat')) {
  100. $this->filesystem->unlink($link.'.bat');
  101. }
  102. }
  103. // attempt removing the bin dir in case it is left empty
  104. if ((is_dir($this->binDir)) && ($this->filesystem->isDirEmpty($this->binDir))) {
  105. Silencer::call('rmdir', $this->binDir);
  106. }
  107. }
  108. protected function getBinaries(PackageInterface $package)
  109. {
  110. return $package->getBinaries();
  111. }
  112. protected function installFullBinaries($binPath, $link, $bin, PackageInterface $package)
  113. {
  114. // add unixy support for cygwin and similar environments
  115. if ('.bat' !== substr($binPath, -4)) {
  116. $this->installUnixyProxyBinaries($binPath, $link);
  117. @chmod($link, 0777 & ~umask());
  118. $link .= '.bat';
  119. if (file_exists($link)) {
  120. $this->io->writeError(' Skipped installation of bin '.$bin.'.bat proxy for package '.$package->getName().': a .bat proxy was already installed');
  121. }
  122. }
  123. if (!file_exists($link)) {
  124. file_put_contents($link, $this->generateWindowsProxyCode($binPath, $link));
  125. }
  126. }
  127. protected function installSymlinkBinaries($binPath, $link)
  128. {
  129. if (!$this->filesystem->relativeSymlink($binPath, $link)) {
  130. $this->installUnixyProxyBinaries($binPath, $link);
  131. }
  132. }
  133. protected function installUnixyProxyBinaries($binPath, $link)
  134. {
  135. file_put_contents($link, $this->generateUnixyProxyCode($binPath, $link));
  136. }
  137. protected function initializeBinDir()
  138. {
  139. $this->filesystem->ensureDirectoryExists($this->binDir);
  140. $this->binDir = realpath($this->binDir);
  141. }
  142. protected function generateWindowsProxyCode($bin, $link)
  143. {
  144. $binPath = $this->filesystem->findShortestPath($link, $bin);
  145. if ('.bat' === substr($bin, -4) || '.exe' === substr($bin, -4)) {
  146. $caller = 'call';
  147. } else {
  148. $handle = fopen($bin, 'r');
  149. $line = fgets($handle);
  150. fclose($handle);
  151. if (preg_match('{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m', $line, $match)) {
  152. $caller = trim($match[1]);
  153. } else {
  154. $caller = 'php';
  155. }
  156. }
  157. return "@ECHO OFF\r\n".
  158. "setlocal DISABLEDELAYEDEXPANSION\r\n".
  159. "SET BIN_TARGET=%~dp0/".trim(ProcessExecutor::escape($binPath), '"\'')."\r\n".
  160. "{$caller} \"%BIN_TARGET%\" %*\r\n";
  161. }
  162. protected function generateUnixyProxyCode($bin, $link)
  163. {
  164. $binPath = $this->filesystem->findShortestPath($link, $bin);
  165. $binDir = ProcessExecutor::escape(dirname($binPath));
  166. $binFile = basename($binPath);
  167. $proxyCode = <<<PROXY
  168. #!/usr/bin/env sh
  169. dir=$(d=\${0%[/\\\\]*}; cd "\$d"; cd $binDir && pwd)
  170. # See if we are running in Cygwin by checking for cygpath program
  171. if command -v 'cygpath' >/dev/null 2>&1; then
  172. # Cygwin paths start with /cygdrive/ which will break windows PHP,
  173. # so we need to translate the dir path to windows format. However
  174. # we could be using cygwin PHP which does not require this, so we
  175. # test if the path to PHP starts with /cygdrive/ rather than /usr/bin
  176. if [[ $(which php) == /cygdrive/* ]]; then
  177. dir=$(cygpath -m "\$dir");
  178. fi
  179. fi
  180. dir=$(echo \$dir | sed 's/ /\ /g')
  181. "\${dir}/$binFile" "$@"
  182. PROXY;
  183. return $proxyCode;
  184. }
  185. }