ProcessFactory.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. public function create(
  53. LoopInterface $loop,
  54. InputInterface $input,
  55. RunnerConfig $runnerConfig,
  56. ProcessIdentifier $identifier,
  57. int $serverPort
  58. ): Process {
  59. $commandArgs = $this->getCommandArgs($serverPort, $identifier, $input, $runnerConfig);
  60. return new Process(
  61. implode(' ', $commandArgs),
  62. $loop,
  63. $runnerConfig->getParallelConfig()->getProcessTimeout()
  64. );
  65. }
  66. /**
  67. * @private
  68. *
  69. * @return list<string>
  70. */
  71. public function getCommandArgs(int $serverPort, ProcessIdentifier $identifier, InputInterface $input, RunnerConfig $runnerConfig): array
  72. {
  73. $phpBinary = (new PhpExecutableFinder)->find(false);
  74. if ($phpBinary === false) {
  75. throw new ParallelisationException('Cannot find PHP executable.');
  76. }
  77. $mainScript = $_SERVER['argv'][0];
  78. $commandArgs = [
  79. ProcessUtils::escapeArgument($phpBinary),
  80. ProcessUtils::escapeArgument($mainScript),
  81. 'worker',
  82. '--port',
  83. (string) $serverPort,
  84. '--identifier',
  85. ProcessUtils::escapeArgument($identifier->toString()),
  86. ];
  87. if ($runnerConfig->isDryRun()) {
  88. $commandArgs[] = '--dry-run';
  89. }
  90. if (filter_var($input->getOption('diff'), FILTER_VALIDATE_BOOLEAN)) {
  91. $commandArgs[] = '--diff';
  92. }
  93. if (filter_var($input->getOption('stop-on-violation'), FILTER_VALIDATE_BOOLEAN)) {
  94. $commandArgs[] = '--stop-on-violation';
  95. }
  96. foreach (['allow-risky', 'config', 'rules', 'using-cache', 'cache-file'] as $option) {
  97. $optionValue = $input->getOption($option);
  98. if ($optionValue !== null) {
  99. $commandArgs[] = "--{$option}";
  100. $commandArgs[] = ProcessUtils::escapeArgument($optionValue);
  101. }
  102. }
  103. return $commandArgs;
  104. }
  105. }