Composer.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Illuminate\Support;
  3. use Illuminate\Filesystem\Filesystem;
  4. use Symfony\Component\Process\PhpExecutableFinder;
  5. use Symfony\Component\Process\Process;
  6. class Composer
  7. {
  8. /**
  9. * The filesystem instance.
  10. *
  11. * @var \Illuminate\Filesystem\Filesystem
  12. */
  13. protected $files;
  14. /**
  15. * The working path to regenerate from.
  16. *
  17. * @var string|null
  18. */
  19. protected $workingPath;
  20. /**
  21. * Create a new Composer manager instance.
  22. *
  23. * @param \Illuminate\Filesystem\Filesystem $files
  24. * @param string|null $workingPath
  25. * @return void
  26. */
  27. public function __construct(Filesystem $files, $workingPath = null)
  28. {
  29. $this->files = $files;
  30. $this->workingPath = $workingPath;
  31. }
  32. /**
  33. * Regenerate the Composer autoloader files.
  34. *
  35. * @param string|array $extra
  36. * @return int
  37. */
  38. public function dumpAutoloads($extra = '')
  39. {
  40. $extra = $extra ? (array) $extra : [];
  41. $command = array_merge($this->findComposer(), ['dump-autoload'], $extra);
  42. return $this->getProcess($command)->run();
  43. }
  44. /**
  45. * Regenerate the optimized Composer autoloader files.
  46. *
  47. * @return int
  48. */
  49. public function dumpOptimized()
  50. {
  51. return $this->dumpAutoloads('--optimize');
  52. }
  53. /**
  54. * Get the composer command for the environment.
  55. *
  56. * @return array
  57. */
  58. protected function findComposer()
  59. {
  60. if ($this->files->exists($this->workingPath.'/composer.phar')) {
  61. return [$this->phpBinary(), 'composer.phar'];
  62. }
  63. return ['composer'];
  64. }
  65. /**
  66. * Get the PHP binary.
  67. *
  68. * @return string
  69. */
  70. protected function phpBinary()
  71. {
  72. return ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
  73. }
  74. /**
  75. * Get a new Symfony process instance.
  76. *
  77. * @param array $command
  78. * @return \Symfony\Component\Process\Process
  79. */
  80. protected function getProcess(array $command)
  81. {
  82. return (new Process($command, $this->workingPath))->setTimeout(null);
  83. }
  84. /**
  85. * Set the working path used by the class.
  86. *
  87. * @param string $path
  88. * @return $this
  89. */
  90. public function setWorkingPath($path)
  91. {
  92. $this->workingPath = realpath($path);
  93. return $this;
  94. }
  95. }