WebOrbiton
v1.0.0.1

StocketBase

112 lines · 3.2 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. final class Config
  6. {
  7. private static ?array $values = null;
  8. private static ?string $envPath = null;
  9. ​
  10. public static function envFilePath(): string
  11. {
  12. if (self::$envPath !== null) {
  13. return self::$envPath;
  14. }
  15. ​
  16. $envRoot = dirname(__DIR__, 2) . '/stocketbase-env';
  17. $pointerFile = $envRoot . '/active-env.txt';
  18. ​
  19. if (is_file($pointerFile)) {
  20. $projectName = trim((string) file_get_contents($pointerFile));
  21. $candidate = $envRoot . '/.env.project.' . $projectName;
  22. if ($projectName !== '' && is_file($candidate)) {
  23. self::$envPath = $candidate;
  24. return self::$envPath;
  25. }
  26. }
  27. ​
  28. self::$envPath = $envRoot . '/.env';
  29. return self::$envPath;
  30. }
  31. ​
  32. public static function isInstalled(): bool
  33. {
  34. return is_file(self::envFilePath());
  35. }
  36. ​
  37. public static function load(): array
  38. {
  39. if (self::$values !== null) {
  40. return self::$values;
  41. }
  42. ​
  43. $path = self::envFilePath();
  44. $values = [];
  45. ​
  46. if (is_file($path)) {
  47. $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  48. foreach ($lines as $line) {
  49. $line = trim($line);
  50. if ($line === '' || str_starts_with($line, '#')) {
  51. continue;
  52. }
  53. if (!str_contains($line, '=')) {
  54. continue;
  55. }
  56. [$key, $value] = explode('=', $line, 2);
  57. $key = trim($key);
  58. $value = trim($value);
  59. if (strlen($value) >= 2 && $value[0] === '"' && $value[-1] === '"') {
  60. $value = substr($value, 1, -1);
  61. }
  62. $values[$key] = $value;
  63. }
  64. }
  65. ​
  66. self::$values = $values;
  67. return self::$values;
  68. }
  69. ​
  70. public static function get(string $key, ?string $default = null): ?string
  71. {
  72. $values = self::load();
  73. return $values[$key] ?? $default;
  74. }
  75. ​
  76. public static function write(string $projectName, array $values): bool
  77. {
  78. $envRoot = dirname(__DIR__, 2) . '/stocketbase-env';
  79. ​
  80. if (!is_dir($envRoot)) {
  81. if (!mkdir($envRoot, 0750, true) && !is_dir($envRoot)) {
  82. return false;
  83. }
  84. }
  85. ​
  86. $safeName = preg_replace('/[^a-zA-Z0-9_-]/', '', $projectName);
  87. if ($safeName === '') {
  88. $safeName = 'default';
  89. }
  90. ​
  91. $targetFile = $envRoot . '/.env.project.' . $safeName;
  92. $lines = [];
  93. foreach ($values as $key => $value) {
  94. $needsQuotes = str_contains((string) $value, ' ') || $value === '';
  95. $lines[] = $needsQuotes ? sprintf('%s="%s"', $key, $value) : sprintf('%s=%s', $key, $value);
  96. }
  97. ​
  98. $written = file_put_contents($targetFile, implode(PHP_EOL, $lines) . PHP_EOL);
  99. if ($written === false) {
  100. return false;
  101. }
  102. ​
  103. file_put_contents($envRoot . '/active-env.txt', $safeName);
  104. chmod($targetFile, 0640);
  105. ​
  106. self::$values = null;
  107. self::$envPath = null;
  108. ​
  109. return true;
  110. }
  111. }
  112. ​