File: Logs.php

package info (click to toggle)
php-composer-xdebug-handler 3.0.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: php: 2,501; makefile: 18
file content (156 lines) | stat: -rw-r--r-- 3,548 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

declare(strict_types=1);

/*
 * This file is part of composer/xdebug-handler.
 *
 * (c) Composer <https://github.com/composer>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Composer\XdebugHandler\Tests\App\Framework;

class Logs
{
    public const LOGGER_NAME = 'logger';

    /** @var array<list<LogItem>> */
    private $items = [];

    /**
     *
     * @param list<string> $outputLines
     */
    public function __construct(array $outputLines)
    {
        $pids = [];

        foreach ($outputLines as $line) {
            $line = trim($line);

            if (!(bool) preg_match('/^(.+)\\[(\\d+)\\](.+)$/', $line, $matches)) {
                continue;
            }

            $pid = (int) $matches[2];
            $item = new LogItem($pid, $matches[1], $matches[3]);

            if (!isset($pids[$pid])) {
                $this->items[] = [];
                $pids[$pid] = count($this->items) - 1;
            }

            $index = $pids[$pid];
            $this->items[$index][] = $item;
        }
    }

    /**
     *
     * @return list<LogItem>
     */
    public function getOutputForProcess(int $seqNo): array
    {
        $index = $seqNo - 1;

        if (isset($this->items[$index])) {
           return $this->items[$index];
        }

        return [];
    }

    /**
     *
     * @return list<LogItem>
     */
    public function getValuesForProcess(int $seqNo, bool $forLogger = true): array
    {
        $items = $this->getOutputForProcess($seqNo);

        if ($forLogger) {
            $result = $this->filterValuesForLogger($items);
        } else {
            $result = $this->filterValuesForScript($items);
        }

        return $result;
    }

    /**
     *
     * @return list<LogItem>
     */
    public function getValuesForProcessName(int $seqNo, string $name): array
    {
        $items = $this->getOutputForProcess($seqNo);

        return $this->filterValuesForScriptName($items, $name);
    }

    /**
     *
     * @param list<LogItem> $items
     */
    public function getItemFromList(array $items, int $seqNo, bool $complete = false): string
    {
        $index = $seqNo - 1;

        if (!isset($items[$index])) {
            throw new \LogicException('Log item not found at index: '.$index);
        }

        $item = $items[$index];

        if (!$complete) {
            return $item->value;
        }

        return sprintf('%s[%d] %s', $item->name, $item->pid, $item->value);
    }

    /**
     *
     * @param list<LogItem> $items
     * @return list<LogItem>
     */
    private function filterValuesForScript(array $items): array
    {
        $result =  array_filter($items, function($item) {
            return $item->name !== self::LOGGER_NAME;
        });

        return array_values($result);
    }

    /**
     *
     * @param list<LogItem> $items
     * @return list<LogItem>
     */
    private function filterValuesForScriptName(array $items, string $name): array
    {
        $result =  array_filter($items, function($item) use ($name) {
            return $item->name === $name;
        });

        return array_values($result);
    }

    /**
     *
     * @param list<LogItem> $items
     * @return list<LogItem>
     */
    private function filterValuesForLogger(array $items): array
    {
        $result = array_filter($items, function($item) {
            return $item->name === 'logger';
        });

        return array_values($result);
    }
}