ProcessFactory.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Runner\Parallel;
  13. /**
  14. * Copyright (c) 2012+ Fabien Potencier, Dariusz Rumiński
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the "Software"), to deal
  18. * in the Software without restriction, including without limitation the rights
  19. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. * copies of the Software, and to permit persons to whom the Software is furnished
  21. * to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in all
  24. * copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. * THE SOFTWARE.
  33. */
  34. use Illuminate\Support\ProcessUtils;
  35. use PhpCsFixer\Runner\RunnerConfig;
  36. use React\EventLoop\LoopInterface;
  37. use Symfony\Component\Console\Input\InputInterface;
  38. use Symfony\Component\Process\PhpExecutableFinder;
  39. /**
  40. * This overrides the default "ProcessFactory" to allow for
  41. * customization of the command-line arguments that better
  42. * suit the needs of the laravel pint package.
  43. *
  44. * @author Greg Korba <greg@codito.dev>
  45. *
  46. * @readonly
  47. *
  48. * @internal
  49. */
  50. final class ProcessFactory
  51. {
  52. private InputInterface $input;
  53. public function __construct(InputInterface $input)
  54. {
  55. $this->input = $input;
  56. }
  57. public function create(
  58. LoopInterface $loop,
  59. RunnerConfig $runnerConfig,
  60. ProcessIdentifier $identifier,
  61. int $serverPort
  62. ): Process {
  63. $commandArgs = $this->getCommandArgs($serverPort, $identifier, $runnerConfig);
  64. return new Process(
  65. implode(' ', $commandArgs),
  66. $loop,
  67. $runnerConfig->getParallelConfig()->getProcessTimeout()
  68. );
  69. }
  70. /**
  71. * @private
  72. *
  73. * @return list<string>
  74. */
  75. public function getCommandArgs(int $serverPort, ProcessIdentifier $identifier, RunnerConfig $runnerConfig): array
  76. {
  77. $phpBinary = (new PhpExecutableFinder)->find(false);
  78. if ($phpBinary === false) {
  79. throw new ParallelisationException('Cannot find PHP executable.');
  80. }
  81. $mainScript = $_SERVER['argv'][0];
  82. $commandArgs = [
  83. escapeshellarg($phpBinary),
  84. escapeshellarg($mainScript),
  85. 'worker',
  86. '--port',
  87. (string) $serverPort,
  88. '--identifier',
  89. escapeshellarg($identifier->toString()),
  90. ];
  91. if ($runnerConfig->isDryRun()) {
  92. $commandArgs[] = '--dry-run';
  93. }
  94. if (filter_var($this->input->getOption('diff'), FILTER_VALIDATE_BOOLEAN)) {
  95. $commandArgs[] = '--diff';
  96. }
  97. if (filter_var($this->input->getOption('stop-on-violation'), FILTER_VALIDATE_BOOLEAN)) {
  98. $commandArgs[] = '--stop-on-violation';
  99. }
  100. foreach (['allow-risky', 'config', 'rules', 'using-cache', 'cache-file'] as $option) {
  101. $optionValue = $this->input->getOption($option);
  102. if ($optionValue !== null) {
  103. $commandArgs[] = "--{$option}";
  104. $commandArgs[] = ProcessUtils::escapeArgument($optionValue);
  105. }
  106. }
  107. return $commandArgs;
  108. }
  109. }