Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CommandOutputProcessor
93.33% covered (success)
93.33%
14 / 15
50.00% covered (danger)
50.00%
1 / 2
9.02
0.00% covered (danger)
0.00%
0 / 1
 process
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
7.03
 extractBufferedOutput
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Console\Logger\Processor;
21
22use Symfony\Component\Console\Output\BufferedOutput;
23use Symfony\Component\Console\Output\ConsoleOutputInterface;
24use Symfony\Component\Console\Output\OutputInterface;
25
26/**
27 * Converts buffered command output objects into serializable context entries.
28 */
29 class CommandOutputProcessor implements ContextProcessorInterface
30{
31    /**
32     * @param array<string, mixed> $context
33     *
34     * @return array<string, mixed>
35     */
36    public function process(array $context): array
37    {
38        foreach ($context as $key => $value) {
39            if (! $value instanceof OutputInterface) {
40                continue;
41            }
42
43            unset($context[$key]);
44
45            $outputContent = $this->extractBufferedOutput($value);
46
47            if (null !== $outputContent) {
48                $context[$key] = $outputContent;
49            }
50
51            if ($value instanceof ConsoleOutputInterface) {
52                $errorOutput = $this->extractBufferedOutput($value->getErrorOutput());
53
54                if (null !== $errorOutput && ! \array_key_exists('error_output', $context)) {
55                    $context['error_output'] = $errorOutput;
56                }
57            }
58        }
59
60        return $context;
61    }
62
63    /**
64     * @param OutputInterface $output
65     *
66     * @return ?string
67     */
68    private function extractBufferedOutput(OutputInterface $output): ?string
69    {
70        if (! $output instanceof BufferedOutput) {
71            return null;
72        }
73
74        return $output->fetch();
75    }
76}