Composer.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace Illuminate\Support;
  3. use Closure;
  4. use Illuminate\Filesystem\Filesystem;
  5. use RuntimeException;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Process\PhpExecutableFinder;
  8. use Symfony\Component\Process\Process;
  9. class Composer
  10. {
  11. /**
  12. * The filesystem instance.
  13. *
  14. * @var \Illuminate\Filesystem\Filesystem
  15. */
  16. protected $files;
  17. /**
  18. * The working path to regenerate from.
  19. *
  20. * @var string|null
  21. */
  22. protected $workingPath;
  23. /**
  24. * Create a new Composer manager instance.
  25. *
  26. * @param \Illuminate\Filesystem\Filesystem $files
  27. * @param string|null $workingPath
  28. * @return void
  29. */
  30. public function __construct(Filesystem $files, $workingPath = null)
  31. {
  32. $this->files = $files;
  33. $this->workingPath = $workingPath;
  34. }
  35. /**
  36. * Determine if the given Composer package is installed.
  37. *
  38. * @param string $package
  39. * @return bool
  40. *
  41. * @throw \RuntimeException
  42. */
  43. protected function hasPackage($package)
  44. {
  45. $composer = json_decode(file_get_contents($this->findComposerFile()), true);
  46. return array_key_exists($package, $composer['require'] ?? [])
  47. || array_key_exists($package, $composer['require-dev'] ?? []);
  48. }
  49. /**
  50. * Install the given Composer packages into the application.
  51. *
  52. * @param array<int, string> $packages
  53. * @param bool $dev
  54. * @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
  55. * @param string|null $composerBinary
  56. * @return bool
  57. */
  58. public function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface|null $output = null, $composerBinary = null)
  59. {
  60. $command = collect([
  61. ...$this->findComposer($composerBinary),
  62. 'require',
  63. ...$packages,
  64. ])
  65. ->when($dev, function ($command) {
  66. $command->push('--dev');
  67. })->all();
  68. return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1'])
  69. ->run(
  70. $output instanceof OutputInterface
  71. ? function ($type, $line) use ($output) {
  72. $output->write(' '.$line);
  73. } : $output
  74. );
  75. }
  76. /**
  77. * Remove the given Composer packages from the application.
  78. *
  79. * @param array<int, string> $packages
  80. * @param bool $dev
  81. * @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
  82. * @param string|null $composerBinary
  83. * @return bool
  84. */
  85. public function removePackages(array $packages, bool $dev = false, Closure|OutputInterface|null $output = null, $composerBinary = null)
  86. {
  87. $command = collect([
  88. ...$this->findComposer($composerBinary),
  89. 'remove',
  90. ...$packages,
  91. ])
  92. ->when($dev, function ($command) {
  93. $command->push('--dev');
  94. })->all();
  95. return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1'])
  96. ->run(
  97. $output instanceof OutputInterface
  98. ? function ($type, $line) use ($output) {
  99. $output->write(' '.$line);
  100. } : $output
  101. );
  102. }
  103. /**
  104. * Modify the "composer.json" file contents using the given callback.
  105. *
  106. * @param callable(array):array $callback
  107. * @return void
  108. *
  109. * @throw \RuntimeException
  110. */
  111. public function modify(callable $callback)
  112. {
  113. $composerFile = $this->findComposerFile();
  114. $composer = json_decode(file_get_contents($composerFile), true, 512, JSON_THROW_ON_ERROR);
  115. file_put_contents(
  116. $composerFile,
  117. json_encode(
  118. call_user_func($callback, $composer),
  119. JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
  120. )
  121. );
  122. }
  123. /**
  124. * Regenerate the Composer autoloader files.
  125. *
  126. * @param string|array $extra
  127. * @param string|null $composerBinary
  128. * @return int
  129. */
  130. public function dumpAutoloads($extra = '', $composerBinary = null)
  131. {
  132. $extra = $extra ? (array) $extra : [];
  133. $command = array_merge($this->findComposer($composerBinary), ['dump-autoload'], $extra);
  134. return $this->getProcess($command)->run();
  135. }
  136. /**
  137. * Regenerate the optimized Composer autoloader files.
  138. *
  139. * @param string|null $composerBinary
  140. * @return int
  141. */
  142. public function dumpOptimized($composerBinary = null)
  143. {
  144. return $this->dumpAutoloads('--optimize', $composerBinary);
  145. }
  146. /**
  147. * Get the Composer binary / command for the environment.
  148. *
  149. * @param string|null $composerBinary
  150. * @return array
  151. */
  152. public function findComposer($composerBinary = null)
  153. {
  154. if (! is_null($composerBinary) && $this->files->exists($composerBinary)) {
  155. return [$this->phpBinary(), $composerBinary];
  156. } elseif ($this->files->exists($this->workingPath.'/composer.phar')) {
  157. return [$this->phpBinary(), 'composer.phar'];
  158. }
  159. return ['composer'];
  160. }
  161. /**
  162. * Get the path to the "composer.json" file.
  163. *
  164. * @return string
  165. *
  166. * @throw \RuntimeException
  167. */
  168. protected function findComposerFile()
  169. {
  170. $composerFile = "{$this->workingPath}/composer.json";
  171. if (! file_exists($composerFile)) {
  172. throw new RuntimeException("Unable to locate `composer.json` file at [{$this->workingPath}].");
  173. }
  174. return $composerFile;
  175. }
  176. /**
  177. * Get the PHP binary.
  178. *
  179. * @return string
  180. */
  181. protected function phpBinary()
  182. {
  183. return ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
  184. }
  185. /**
  186. * Get a new Symfony process instance.
  187. *
  188. * @param array $command
  189. * @param array $env
  190. * @return \Symfony\Component\Process\Process
  191. */
  192. protected function getProcess(array $command, array $env = [])
  193. {
  194. return (new Process($command, $this->workingPath, $env))->setTimeout(null);
  195. }
  196. /**
  197. * Set the working path used by the class.
  198. *
  199. * @param string $path
  200. * @return $this
  201. */
  202. public function setWorkingPath($path)
  203. {
  204. $this->workingPath = realpath($path);
  205. return $this;
  206. }
  207. /**
  208. * Get the version of Composer.
  209. *
  210. * @return string|null
  211. */
  212. public function getVersion()
  213. {
  214. $command = array_merge($this->findComposer(), ['-V', '--no-ansi']);
  215. $process = $this->getProcess($command);
  216. $process->run();
  217. $output = $process->getOutput();
  218. if (preg_match('/(\d+(\.\d+){2})/', $output, $version)) {
  219. return $version[1];
  220. }
  221. return explode(' ', $output)[2] ?? null;
  222. }
  223. }