humbug /
box
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the box project. |
||
| 7 | * |
||
| 8 | * (c) Kevin Herrera <[email protected]> |
||
| 9 | * Théo Fidry <[email protected]> |
||
| 10 | * |
||
| 11 | * This source file is subject to the MIT license that is bundled |
||
| 12 | * with this source code in the file LICENSE. |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace KevinGH\Box\Phar\Differ; |
||
| 16 | |||
| 17 | use Fidry\Console\IO; |
||
| 18 | use KevinGH\Box\Phar\PharInfo; |
||
| 19 | use Symfony\Component\Process\Process; |
||
| 20 | |||
| 21 | final readonly class ProcessCommandBasedDiffer implements Differ |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 22 | { |
||
| 23 | public function __construct(private string $command) |
||
| 24 | { |
||
| 25 | } |
||
| 26 | |||
| 27 | public function diff(PharInfo $pharInfoA, PharInfo $pharInfoB, IO $io): void |
||
| 28 | { |
||
| 29 | $result = self::getDiff( |
||
| 30 | $pharInfoA, |
||
| 31 | $pharInfoB, |
||
| 32 | $this->command, |
||
| 33 | ); |
||
| 34 | |||
| 35 | $io->writeln($result ?? Differ::NO_DIFF_MESSAGE); |
||
| 36 | } |
||
| 37 | |||
| 38 | public static function getDiff(PharInfo $pharInfoA, PharInfo $pharInfoB, string $command): ?string |
||
| 39 | { |
||
| 40 | $pharInfoATmp = $pharInfoA->getTmp(); |
||
| 41 | $pharInfoBTmp = $pharInfoB->getTmp(); |
||
| 42 | |||
| 43 | $pharInfoAFileName = $pharInfoA->getFileName(); |
||
| 44 | $pharInfoBFileName = $pharInfoB->getFileName(); |
||
| 45 | |||
| 46 | $diffCommand = implode( |
||
| 47 | ' ', |
||
| 48 | [ |
||
| 49 | $command, |
||
| 50 | $pharInfoATmp, |
||
| 51 | $pharInfoBTmp, |
||
| 52 | ], |
||
| 53 | ); |
||
| 54 | |||
| 55 | $diffProcess = Process::fromShellCommandline($diffCommand); |
||
| 56 | $diffProcess->run(); |
||
| 57 | |||
| 58 | // We do not check if the process is successful as if there |
||
| 59 | // is a difference between the two files then the process |
||
| 60 | // _will_ be unsuccessful. |
||
| 61 | $diff = trim($diffProcess->getOutput()); |
||
| 62 | |||
| 63 | if ('' === $diff) { |
||
| 64 | return null; |
||
| 65 | } |
||
| 66 | |||
| 67 | return str_replace( |
||
| 68 | [ |
||
| 69 | $pharInfoATmp, |
||
| 70 | $pharInfoBTmp, |
||
| 71 | ], |
||
| 72 | [ |
||
| 73 | $pharInfoAFileName, |
||
| 74 | $pharInfoBFileName, |
||
| 75 | ], |
||
| 76 | $diff, |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 |