Passed
Pull Request — 0.6.x (#198)
by Shinji
01:39
created

ProcessDescriptorRetriever::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
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\Searcher\Worker;
15
16
use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessDescriptor;
17
use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettings;
18
use PhpProfiler\Lib\Log\Log;
19
use PhpProfiler\Lib\PhpInternals\ZendTypeReader;
20
use PhpProfiler\Lib\PhpProcessReader\PhpGlobalsFinder;
21
use PhpProfiler\Lib\PhpProcessReader\PhpVersionDetector;
22
use PhpProfiler\Lib\Process\ProcessSpecifier;
23
24
class ProcessDescriptorRetriever
25
{
26
    public function __construct(
27
        private PhpGlobalsFinder $php_globals_finder,
28
        private PhpVersionDetector $php_version_detector,
29
    ) {
30
    }
31
32
    /** @param TargetPhpSettings<value-of<ZendTypeReader::ALL_SUPPORTED_VERSIONS>|'auto'> $target_php_settings */
33
    public function getProcessDescriptor(
34
        int $pid,
35
        TargetPhpSettings $target_php_settings,
36
        ProcessDescriptorCache $process_descriptor_cache,
37
    ): TargetProcessDescriptor {
38
        $cache = $process_descriptor_cache->get($pid);
39
        if (!is_null($cache)) {
40
            return $cache;
41
        }
42
43
        try {
44
            $process_specifier = new ProcessSpecifier($pid);
45
            Log::debug('deciding php version', ['target_pid' => $pid]);
46
            $target_php_settings_decided = $this->php_version_detector->decidePhpVersion(
47
                $process_specifier,
48
                $target_php_settings
49
            );
50
            Log::debug('php version decided', ['target_pid' => $pid]);
51
            $eg_address = $this->php_globals_finder->findExecutorGlobals(
52
                new ProcessSpecifier($pid),
53
                $target_php_settings_decided,
54
            );
55
        } catch (\Throwable $e) {
56
            Log::debug(
57
                'error on analyzing php binary',
58
                [
59
                    'message' => $e->getMessage(),
60
                    'trace' => $e->getTrace()
61
                ]
62
            );
63
            $process_descriptor_cache->setInvalid($pid);
64
            return TargetProcessDescriptor::getInvalid();
65
        }
66
67
        Log::debug('successfully analyzed php binary', [$pid]);
68
        /** @psalm-var TargetPhpSettings<value-of<ZendTypeReader::ALL_SUPPORTED_VERSIONS>> $target_php_settings_decided */
69
70
        $result = new TargetProcessDescriptor(
71
            $pid,
72
            $eg_address,
73
            $target_php_settings_decided->php_version,
74
        );
75
        $process_descriptor_cache->set($result);
76
        return $result;
77
    }
78
}
79