|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeIgniter\Commands\Utilities; |
| 4 | + |
| 5 | +use CodeIgniter\CLI\BaseCommand; |
| 6 | +use CodeIgniter\CLI\CLI; |
| 7 | +use CodeIgniter\Config\DotEnv; |
| 8 | + |
| 9 | +/** |
| 10 | + * Command to display the current environment, |
| 11 | + * or set a new one in the `.env` file. |
| 12 | + */ |
| 13 | +final class Environment extends BaseCommand |
| 14 | +{ |
| 15 | + /** |
| 16 | + * The group the command is lumped under |
| 17 | + * when listing commands. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $group = 'CodeIgniter'; |
| 22 | + |
| 23 | + /** |
| 24 | + * The Command's name |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $name = 'env'; |
| 29 | + |
| 30 | + /** |
| 31 | + * The Command's short description |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + protected $description = 'Retrieves the current environment, or set a new one.'; |
| 36 | + |
| 37 | + /** |
| 38 | + * The Command's usage |
| 39 | + * |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + protected $usage = 'env [<environment>]'; |
| 43 | + |
| 44 | + /** |
| 45 | + * The Command's arguments |
| 46 | + * |
| 47 | + * @var array<string, string> |
| 48 | + */ |
| 49 | + protected $arguments = [ |
| 50 | + 'environment' => '[Optional] The new environment to set. If none is provided, this will print the current environment.', |
| 51 | + ]; |
| 52 | + |
| 53 | + /** |
| 54 | + * The Command's options |
| 55 | + * |
| 56 | + * @var array |
| 57 | + */ |
| 58 | + protected $options = []; |
| 59 | + |
| 60 | + /** |
| 61 | + * Allowed values for environment. `testing` is excluded |
| 62 | + * since spark won't work on it. |
| 63 | + * |
| 64 | + * @var array<int, string> |
| 65 | + */ |
| 66 | + private static $knownTypes = [ |
| 67 | + 'production', |
| 68 | + 'development', |
| 69 | + ]; |
| 70 | + |
| 71 | + /** |
| 72 | + * @inheritDoc |
| 73 | + * |
| 74 | + * @param array<string, mixed> $params |
| 75 | + * |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + public function run(array $params) |
| 79 | + { |
| 80 | + if ($params === []) |
| 81 | + { |
| 82 | + CLI::write(sprintf('Your environment is currently set as %s.', CLI::color(ENVIRONMENT, 'green'))); |
| 83 | + CLI::newLine(); |
| 84 | + |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + $env = strtolower(array_shift($params)); |
| 89 | + |
| 90 | + if ($env === 'testing') |
| 91 | + { |
| 92 | + CLI::error('The "testing" environment is reserved for PHPUnit testing.', 'light_gray', 'red'); |
| 93 | + CLI::error('You will not be able to run spark under a "testing" environment.', 'light_gray', 'red'); |
| 94 | + CLI::newLine(); |
| 95 | + |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (! in_array($env, self::$knownTypes, true)) |
| 100 | + { |
| 101 | + CLI::error(sprintf('Invalid environment type "%s". Expected one of "%s".', $env, implode('" and "', self::$knownTypes)), 'light_gray', 'red'); |
| 102 | + CLI::newLine(); |
| 103 | + |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (! $this->writeNewEnvironmentToEnvFile($env)) |
| 108 | + { |
| 109 | + CLI::error('Error in writing new environment to .env file.', 'light_gray', 'red'); |
| 110 | + CLI::newLine(); |
| 111 | + |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + // force DotEnv to reload the new environment |
| 116 | + // however we cannot redefine the ENVIRONMENT constant |
| 117 | + putenv('CI_ENVIRONMENT'); |
| 118 | + unset($_ENV['CI_ENVIRONMENT'], $_SERVER['CI_ENVIRONMENT']); |
| 119 | + (new DotEnv(ROOTPATH))->load(); |
| 120 | + |
| 121 | + CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green'); |
| 122 | + CLI::write('The ENVIRONMENT constant will be changed on the next script execution.'); |
| 123 | + CLI::newLine(); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @see https://regex101.com/r/4sSORp/1 for the regex in action |
| 128 | + * |
| 129 | + * @param string $newEnv |
| 130 | + * |
| 131 | + * @return boolean |
| 132 | + */ |
| 133 | + private function writeNewEnvironmentToEnvFile(string $newEnv): bool |
| 134 | + { |
| 135 | + $baseEnv = ROOTPATH . 'env'; |
| 136 | + $envFile = ROOTPATH . '.env'; |
| 137 | + |
| 138 | + if (! is_file($envFile)) |
| 139 | + { |
| 140 | + if (! is_file($baseEnv)) |
| 141 | + { |
| 142 | + CLI::write('Both default shipped `env` file and custom `.env` are missing.', 'yellow'); |
| 143 | + CLI::write('It is impossible to write the new environment type.', 'yellow'); |
| 144 | + CLI::newLine(); |
| 145 | + |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + copy($baseEnv, $envFile); |
| 150 | + } |
| 151 | + |
| 152 | + $pattern = preg_quote($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, '/'); |
| 153 | + $pattern = sprintf('/^[#\s]*CI_ENVIRONMENT[=\s]+%s$/m', $pattern); |
| 154 | + |
| 155 | + return file_put_contents( |
| 156 | + $envFile, |
| 157 | + preg_replace($pattern, "\nCI_ENVIRONMENT = {$newEnv}", file_get_contents($envFile), -1, $count) |
| 158 | + ) !== false && $count > 0; |
| 159 | + } |
| 160 | +} |
0 commit comments