Skip to content

Commit 9a86ad2

Browse files
committed
ProcessFinger 0.1.2
1 parent b8d5e59 commit 9a86ad2

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Includes the following helpers:
1414
- [Levex](https://github.com/nabeghe/levex-php) <small>v1.0.1</small>
1515
- [Matcher](https://github.com/nabeghe/matcher-php)<small> v1.0.0</small>
1616
- [Mem](https://github.com/nabeghe/mem-php) <small>v1.2.0</small>
17-
- [ProcessFinger](https://github.com/nabeghe/process-finger-php) <small>v0.1.1</small>
17+
- [ProcessFinger](https://github.com/nabeghe/process-finger-php) <small>v0.1.2</small>
1818
- [Reflecty](https://github.com/nabeghe/reflecty-php) <small>v0.5.2</small>
1919
- [Servery](https://github.com/nabeghe/servery-php) <small>v0.3.0</small>
2020
- [Shortnum](https://github.com/nabeghe/shortnum-php) <small>v1.0.0</small>

src/ProcessFinger/ProcessFinger.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,41 @@ public static function getScriptPath(?int $pid = null): ?string
106106
}
107107

108108
if ($os === 'WIN') {
109-
$command = "powershell -NoProfile -Command \"(Get-CimInstance Win32_Process -Filter \\\"ProcessId='$pid'\\\").CommandLine\" 2>&1";
110-
$output = trim((string) shell_exec($command));
109+
$command = 'powershell -NoProfile -Command '
110+
. '"(Get-CimInstance Win32_Process -Filter \"ProcessId=' . $pid . '\").CommandLine" 2>&1';
111111

112-
if ($output === '') {
112+
$commandLine = trim((string) shell_exec($command));
113+
if ($commandLine === '') {
113114
return null;
114115
}
115116

116-
preg_match_all('/"([^"]+)"|(\S+)/', $output, $matches);
117-
$args = array_values(array_filter(array_merge($matches[1], $matches[2])));
117+
// Parse Windows command line (handles quotes properly)
118+
preg_match_all('/"([^"]+)"|(\S+)/u', $commandLine, $m);
119+
$args = array_values(array_filter(array_merge($m[1], $m[2])));
118120

119-
foreach ($args as $arg) {
120-
if ($arg[0] === '-') {
121-
continue;
122-
}
121+
// Remove php.exe
122+
array_shift($args);
123123

124-
if (substr($arg, -4) !== '.php') {
124+
// Get process working directory
125+
$cwd = trim((string) shell_exec(
126+
'powershell -NoProfile -Command '
127+
. '"(Get-CimInstance Win32_Process -Filter \"ProcessId=' . $pid . '\").WorkingDirectory"'
128+
));
129+
130+
foreach ($args as $arg) {
131+
if ($arg === '' || $arg[0] === '-') {
125132
continue;
126133
}
127134

135+
// Direct path
128136
if (is_file($arg)) {
129137
return realpath($arg);
130138
}
139+
140+
/// Relative path
141+
if ($cwd && is_file($cwd . DIRECTORY_SEPARATOR . $arg)) {
142+
return realpath($cwd . DIRECTORY_SEPARATOR . $arg);
143+
}
131144
}
132145

133146
return null;

0 commit comments

Comments
 (0)