Passed
Pull Request — master (#31)
by Shinji
04:07 queued 02:36
created

PhpReaderTraceLoop::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 28
rs 9.7
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Inspector\Daemon\Reader\Worker;
15
16
use Generator;
17
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\TraceMessage;
18
use PhpProfiler\Inspector\Settings\GetTraceSettings\GetTraceSettings;
19
use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettings;
20
use PhpProfiler\Inspector\Settings\TargetProcessSettings\TargetProcessSettings;
21
use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettings;
22
use PhpProfiler\Lib\PhpProcessReader\PhpGlobalsFinder;
23
use PhpProfiler\Lib\PhpProcessReader\PhpMemoryReader\ExecutorGlobalsReader;
24
25
final class PhpReaderTraceLoop implements PhpReaderTraceLoopInterface
26
{
27
    private PhpGlobalsFinder $php_globals_finder;
28
    private ExecutorGlobalsReader $executor_globals_reader;
29
    private ReaderLoopProvider $reader_loop_provider;
30
31
    public function __construct(
32
        PhpGlobalsFinder $php_globals_finder,
33
        ExecutorGlobalsReader $executor_globals_reader,
34
        ReaderLoopProvider $reader_loop_provider
35
    ) {
36
        $this->php_globals_finder = $php_globals_finder;
37
        $this->executor_globals_reader = $executor_globals_reader;
38
        $this->reader_loop_provider = $reader_loop_provider;
39
    }
40
41
    /**
42
     * @param TargetProcessSettings $target_process_settings
43
     * @param TraceLoopSettings $loop_settings
44
     * @param TargetPhpSettings $target_php_settings
45
     * @param GetTraceSettings $get_trace_settings
46
     * @return Generator<TraceMessage>
47
     * @throws \PhpProfiler\Lib\Elf\Parser\ElfParserException
48
     * @throws \PhpProfiler\Lib\Elf\Process\ProcessSymbolReaderException
49
     * @throws \PhpProfiler\Lib\Elf\Tls\TlsFinderException
50
     * @throws \PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException
51
     */
52
    public function run(
53
        TargetProcessSettings $target_process_settings,
54
        TraceLoopSettings $loop_settings,
55
        TargetPhpSettings $target_php_settings,
56
        GetTraceSettings $get_trace_settings
57
    ): Generator {
58
        $eg_address = $this->php_globals_finder->findExecutorGlobals($target_process_settings, $target_php_settings);
59
60
        $loop = $this->reader_loop_provider->getMainLoop(
61
            function () use (
62
                $get_trace_settings,
63
                $target_process_settings,
64
                $target_php_settings,
65
                $eg_address
66
            ): \Generator {
67
                $call_trace = $this->executor_globals_reader->readCallTrace(
68
                    $target_process_settings->pid,
69
                    $target_php_settings->php_version,
70
                    $eg_address,
71
                    $get_trace_settings->depth
72
                );
73
                yield new TraceMessage($call_trace);
74
            },
75
            $loop_settings
76
        );
77
        /** @var Generator<TraceMessage> */
78
        $loop_process = $loop->invoke();
79
        yield from $loop_process;
80
    }
81
}
82