1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | /* (c) Anton Medvedev <[email protected]> |
||||
6 | * |
||||
7 | * For the full copyright and license information, please view the LICENSE |
||||
8 | * file that was distributed with this source code. |
||||
9 | */ |
||||
10 | |||||
11 | namespace Deployer\Utility; |
||||
12 | |||||
13 | use Deployer\ProcessRunner\Printer; |
||||
14 | use Deployer\Exception\RunException; |
||||
15 | use Deployer\Host\Host; |
||||
16 | use Symfony\Component\Console\Helper\ProgressBar; |
||||
17 | use Symfony\Component\Console\Output\OutputInterface; |
||||
18 | use Symfony\Component\Process\Exception\ProcessFailedException; |
||||
19 | use Symfony\Component\Process\Process; |
||||
20 | |||||
21 | use function Deployer\writeln; |
||||
22 | |||||
23 | class Rsync |
||||
24 | { |
||||
25 | /** |
||||
26 | * @var Printer |
||||
27 | */ |
||||
28 | private $pop; |
||||
29 | /** |
||||
30 | * @var OutputInterface |
||||
31 | */ |
||||
32 | private $output; |
||||
33 | |||||
34 | public function __construct(Printer $pop, OutputInterface $output) |
||||
35 | { |
||||
36 | $this->pop = $pop; |
||||
37 | $this->output = $output; |
||||
38 | } |
||||
39 | |||||
40 | /** |
||||
41 | * Start rsync process. |
||||
42 | * |
||||
43 | * @param string|string[] $source |
||||
44 | * @phpstan-param array{flags?: string, options?: array, timeout?: int|null, progress_bar?: bool, display_stats?: bool} $config |
||||
45 | * @throws RunException |
||||
46 | */ |
||||
47 | public function call(Host $host, $source, string $destination, array $config = []): void |
||||
48 | { |
||||
49 | $defaults = [ |
||||
50 | 'timeout' => null, |
||||
51 | 'options' => [], |
||||
52 | 'flags' => '-azP', |
||||
53 | 'progress_bar' => true, |
||||
54 | 'display_stats' => false, |
||||
55 | ]; |
||||
56 | $config = array_merge($defaults, $config); |
||||
57 | |||||
58 | $options = $config['options']; |
||||
59 | $flags = $config['flags']; |
||||
60 | $displayStats = $config['display_stats'] || in_array('--stats', $options, true); |
||||
61 | |||||
62 | if ($displayStats && !in_array('--stats', $options, true)) { |
||||
63 | $options[] = '--stats'; |
||||
64 | } |
||||
65 | |||||
66 | $connectionOptions = $host->connectionOptionsString(); |
||||
67 | if ($connectionOptions !== '') { |
||||
68 | $options = array_merge($options, ['-e', "ssh $connectionOptions"]); |
||||
69 | } |
||||
70 | if ($host->has('become') && !empty($host->get('become'))) { |
||||
71 | $options = array_merge($options, ['--rsync-path', "sudo -H -u {$host->get('become')} rsync"]); |
||||
72 | } |
||||
73 | if (!is_array($source)) { |
||||
74 | $source = [$source]; |
||||
75 | } |
||||
76 | $command = array_values(array_filter( |
||||
77 | array_merge(['rsync', $flags], $options, $source, [$destination]), |
||||
78 | function (string $value) { |
||||
79 | return $value !== ''; |
||||
80 | }, |
||||
81 | )); |
||||
82 | |||||
83 | $commandString = $command[0]; |
||||
84 | for ($i = 1; $i < count($command); $i++) { |
||||
0 ignored issues
–
show
|
|||||
85 | $commandString .= ' ' . escapeshellarg($command[$i]); |
||||
86 | } |
||||
87 | if ($this->output->isVerbose()) { |
||||
88 | $this->output->writeln("[$host] $commandString"); |
||||
89 | } |
||||
90 | |||||
91 | $progressBar = null; |
||||
92 | if ($this->output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL && $config['progress_bar']) { |
||||
93 | $progressBar = new ProgressBar($this->output); |
||||
94 | $progressBar->setBarCharacter('<info>≡</info>'); |
||||
95 | $progressBar->setProgressCharacter('>'); |
||||
96 | $progressBar->setEmptyBarCharacter('-'); |
||||
97 | } |
||||
98 | |||||
99 | $fullOutput = ''; |
||||
100 | |||||
101 | $callback = function ($type, $buffer) use ($host, $progressBar, &$fullOutput) { |
||||
102 | $fullOutput .= $buffer; |
||||
103 | if ($progressBar) { |
||||
104 | foreach (explode("\n", $buffer) as $line) { |
||||
105 | if (preg_match('/(to-chk|to-check)=(\d+?)\/(\d+)/', $line, $match)) { |
||||
106 | $max = intval($match[3]); |
||||
107 | $step = $max - intval($match[2]); |
||||
108 | $progressBar->setMaxSteps($max); |
||||
109 | $progressBar->setFormat("[$host] %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%"); |
||||
110 | $progressBar->setProgress($step); |
||||
111 | } |
||||
112 | } |
||||
113 | return; |
||||
114 | } |
||||
115 | if ($this->output->isVerbose()) { |
||||
116 | $this->pop->printBuffer($type, $host, $buffer); |
||||
117 | } |
||||
118 | }; |
||||
119 | |||||
120 | $process = new Process($command); |
||||
121 | $process->setTimeout($config['timeout']); |
||||
122 | try { |
||||
123 | $process->mustRun($callback); |
||||
124 | |||||
125 | if ($displayStats) { |
||||
126 | $stats = []; |
||||
127 | |||||
128 | $statsStarted = false; |
||||
129 | foreach (explode("\n", $fullOutput) as $line) { |
||||
130 | if (strpos($line, 'Number of files') === 0) { |
||||
131 | $statsStarted = true; |
||||
132 | } |
||||
133 | |||||
134 | if ($statsStarted) { |
||||
135 | if (empty($line)) { |
||||
136 | break; |
||||
137 | } |
||||
138 | $stats[] = $line; |
||||
139 | } |
||||
140 | } |
||||
141 | |||||
142 | writeln("Rsync operation stats\n" . '<comment>' . implode("\n", $stats) . '</comment>'); |
||||
143 | } |
||||
144 | |||||
145 | } catch (ProcessFailedException $exception) { |
||||
146 | throw new RunException( |
||||
147 | $host, |
||||
148 | $commandString, |
||||
149 | $process->getExitCode(), |
||||
0 ignored issues
–
show
It seems like
$process->getExitCode() can also be of type null ; however, parameter $exitCode of Deployer\Exception\RunException::__construct() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
150 | $process->getOutput(), |
||||
151 | $process->getErrorOutput(), |
||||
152 | ); |
||||
153 | } finally { |
||||
154 | if ($progressBar) { |
||||
155 | $progressBar->clear(); |
||||
156 | } |
||||
157 | } |
||||
158 | } |
||||
159 | } |
||||
160 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: