Skip to content

Commit cd5e2a6

Browse files
committed
feat: Enhance global state directory determination for Windows
Use a workaround solution to get the APPDATA path (Windows)
1 parent b5f1d41 commit cd5e2a6

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

app.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Butschster\ContextGenerator\Application\Application;
88
use Butschster\ContextGenerator\Application\ExceptionHandler;
9+
use Butschster\ContextGenerator\Application\FSPath;
910
use Butschster\ContextGenerator\Application\Kernel;
1011
use Spiral\Core\Container;
1112
use Spiral\Core\Options;
@@ -93,11 +94,31 @@
9394
// -----------------------------------------------------------------------------
9495

9596
// Determine appropriate location for global state based on OS
96-
$globalStateDir = match (PHP_OS_FAMILY) {
97-
'Windows' => \getenv('APPDATA') . '/CTX',
97+
$globalStateDir = (string) FSPath::create(match (PHP_OS_FAMILY) {
98+
'Windows' => (function (): string {
99+
$result = $_SERVER['APPDATA'] ?? null;
100+
if (\is_string($result)) {
101+
return $result;
102+
}
103+
104+
/*
105+
* In some cases, the APPDATA environment variable may not be set by an MCP client (f.e. Claude)
106+
* In this case we need to use workaround.
107+
*/
108+
/** @psalm-suppress ForbiddenCode */
109+
$output = `reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "AppData"`;
110+
// Check if the command was successful
111+
$pos = \strpos($output, $_SERVER['USERPROFILE']);
112+
if ($pos === false) {
113+
// If the command failed, we can use the default APPDATA path
114+
return $_SERVER['USERPROFILE'] . '\AppData\Roaming';
115+
}
116+
117+
return \trim(\explode("\n", \substr($output, $pos))[0]);
118+
})() . '/CTX',
98119
'Darwin' => $_SERVER['HOME'] . '/Library/Application Support/CTX',
99120
default => $_SERVER['HOME'] . '/.config/ctx',
100-
};
121+
});
101122

102123
$app = Kernel::create(
103124
directories: [

0 commit comments

Comments
 (0)