Composer.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Illuminate\Support;
  3. use Illuminate\Filesystem\Filesystem;
  4. use Symfony\Component\Process\Process;
  5. use Symfony\Component\Process\PhpExecutableFinder;
  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
  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 $extra
  36. * @return void
  37. */
  38. public function dumpAutoloads($extra = '')
  39. {
  40. $process = $this->getProcess();
  41. $process->setCommandLine(trim($this->findComposer().' dump-autoload '.$extra));
  42. $process->run();
  43. }
  44. /**
  45. * Regenerate the optimized Composer autoloader files.
  46. *
  47. * @return void
  48. */
  49. public function dumpOptimized()
  50. {
  51. $this->dumpAutoloads('--optimize');
  52. }
  53. /**
  54. * Get the composer command for the environment.
  55. *
  56. * @return string
  57. */
  58. protected function findComposer()
  59. {
  60. if ($this->files->exists($this->workingPath.'/composer.phar')) {
  61. return ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)).' composer.phar';
  62. }
  63. return 'composer';
  64. }
  65. /**
  66. * Get a new Symfony process instance.
  67. *
  68. * @return \Symfony\Component\Process\Process
  69. */
  70. protected function getProcess()
  71. {
  72. return (new Process('', $this->workingPath))->setTimeout(null);
  73. }
  74. /**
  75. * Set the working path used by the class.
  76. *
  77. * @param string $path
  78. * @return $this
  79. */
  80. public function setWorkingPath($path)
  81. {
  82. $this->workingPath = realpath($path);
  83. return $this;
  84. }
  85. }