Skip to content

Commit

Permalink
don't fail if config path is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
apreiml committed Nov 28, 2024
1 parent 202f2e8 commit 0582fa8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/ConfigPaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,16 @@ private function windowsHomeDir()
return null;
}

private function homeConfigDir()
private function homeConfigDir(): ?string
{
if ($homeConfigDir = $this->getEnv('XDG_CONFIG_HOME')) {
return $homeConfigDir;
}

$homeDir = $this->homeDir();
if ($homeDir === null) {
return null;
}

return $homeDir === '/' ? $homeDir.'.config' : $homeDir.'/.config';
}
Expand Down Expand Up @@ -130,7 +133,7 @@ public function configDirs(): array
*
* @see self::homeConfigDir
*/
public function currentConfigDir(): string
public function currentConfigDir(): ?string
{
if ($this->configDir !== null) {
return $this->configDir;
Expand All @@ -144,7 +147,7 @@ public function currentConfigDir(): string
}
}

return $configDirs[0];
return $configDirs[0] ?? null;
}

/**
Expand Down Expand Up @@ -259,6 +262,7 @@ public function which($command)
*/
private function allDirNames(array $baseDirs): array
{
$baseDirs = array_filter($baseDirs);
$dirs = \array_map(function ($dir) {
return \strtr($dir, '\\', '/').'/psysh';
}, $baseDirs);
Expand Down
6 changes: 5 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public function setHistoryFile(string $file)
* Defaults to `/history` inside the shell's base config dir unless
* explicitly overridden.
*/
public function getHistoryFile(): string
public function getHistoryFile(): ?string
{
if (isset($this->historyFile)) {
return $this->historyFile;
Expand All @@ -674,6 +674,10 @@ public function getHistoryFile(): string
$this->setHistoryFile($files[0]);
} else {
// fallback: create our own history file
$currentConfDir = $this->configPaths->currentConfigDir();
if ($currentConfDir === null) {
return null;
}
$this->setHistoryFile($this->configPaths->currentConfigDir().'/psysh_history');
}

Expand Down

0 comments on commit 0582fa8

Please sign in to comment.