Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 31 additions & 63 deletions system/Commands/Utilities/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,20 @@

namespace CodeIgniter\Commands\Utilities;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Argument;
use CodeIgniter\Config\DotEnv;
use Config\Paths;

/**
* Command to display the current environment,
* or set a new one in the `.env` file.
*/
final class Environment extends BaseCommand
#[Command(name: 'env', description: 'Retrieves the current environment, or set a new one.', group: 'CodeIgniter')]
class Environment extends AbstractCommand
{
/**
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
protected $group = 'CodeIgniter';

/**
* The Command's name
*
* @var string
*/
protected $name = 'env';

/**
* The Command's short description
*
* @var string
*/
protected $description = 'Retrieves the current environment, or set a new one.';

/**
* The Command's usage
*
* @var string
*/
protected $usage = 'env [<environment>]';

/**
* The Command's arguments
*
* @var array<string, string>
*/
protected $arguments = [
'environment' => '[Optional] The new environment to set. If none is provided, this will print the current environment.',
];

/**
* The Command's options
*
* @var array<string, string>
*/
protected $options = [];

/**
* Allowed values for environment. `testing` is excluded
* since spark won't work on it.
Expand All @@ -80,52 +38,63 @@ final class Environment extends BaseCommand
'development',
];

/**
* @return int
*/
public function run(array $params)
protected function configure(): void
{
if (! isset($params[0])) {
CLI::write(sprintf('Your environment is currently set as %s.', CLI::color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green')));
CLI::newLine();
$this->addArgument(new Argument(
name: 'environment',
description: 'The new environment to set. If none is provided, the current environment is printed.',
default: '',
));
}

protected function execute(array $arguments, array $options): int
{
$env = $arguments['environment'];
assert(is_string($env));

if ($env === '') {
CLI::write(sprintf(
'Your environment is currently set as %s.',
CLI::color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green'),
));

return EXIT_SUCCESS;
}

$env = strtolower(array_shift($params));
$env = strtolower($env);

if ($env === 'testing') {
CLI::error('The "testing" environment is reserved for PHPUnit testing.', 'light_gray', 'red');
CLI::error('You will not be able to run spark under a "testing" environment.', 'light_gray', 'red');
CLI::newLine();

return EXIT_ERROR;
}

if (! in_array($env, self::$knownTypes, true)) {
CLI::error(sprintf('Invalid environment type "%s". Expected one of "%s".', $env, implode('" and "', self::$knownTypes)), 'light_gray', 'red');
CLI::newLine();
CLI::error(sprintf(
'Invalid environment type "%s". Expected one of "%s".',
$env,
implode('" and "', self::$knownTypes),
), 'light_gray', 'red');

return EXIT_ERROR;
}

if (! $this->writeNewEnvironmentToEnvFile($env)) {
CLI::error('Error in writing new environment to .env file.', 'light_gray', 'red');
CLI::newLine();

return EXIT_ERROR;
}

// force DotEnv to reload the new environment
// however we cannot redefine the ENVIRONMENT constant
// Reload DotEnv with the new environment. The ENVIRONMENT constant
// only takes the new value on the next script execution.
putenv('CI_ENVIRONMENT');
unset($_ENV['CI_ENVIRONMENT']);
service('superglobals')->unsetServer('CI_ENVIRONMENT');
(new DotEnv((new Paths())->envDirectory ?? ROOTPATH))->load(); // @phpstan-ignore nullCoalesce.property

CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green');
CLI::write('The ENVIRONMENT constant will be changed in the next script execution.');
CLI::newLine();

return EXIT_SUCCESS;
}
Expand All @@ -142,7 +111,6 @@ private function writeNewEnvironmentToEnvFile(string $newEnv): bool
if (! is_file($baseEnv)) {
CLI::write('Both default shipped `env` file and custom `.env` are missing.', 'yellow');
CLI::write('It is impossible to write the new environment type.', 'yellow');
CLI::newLine();

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/system/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testHelpCommandUsingHelpOption(): void
$this->initializeConsole('env', '--help');
(new Console())->run();

$this->assertStringContainsString('env [<environment>]', $this->getStreamFilterBuffer());
$this->assertStringContainsString('env [options] [--] [<environment>]', $this->getStreamFilterBuffer());
$this->assertStringContainsString(
'Retrieves the current environment, or set a new one.',
$this->getStreamFilterBuffer(),
Expand Down
7 changes: 0 additions & 7 deletions tests/system/Commands/Utilities/EnvironmentCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ public function testUsingCommandWithNoArgumentsGivesCurrentEnvironment(): void
$this->assertStringContainsString(ENVIRONMENT, $this->getStreamFilterBuffer());
}

public function testUsingCommandWithOptionsOnlyGivesCurrentEnvironment(): void
{
command('env --foo');
$this->assertStringContainsString('testing', $this->getStreamFilterBuffer());
$this->assertStringContainsString(ENVIRONMENT, $this->getStreamFilterBuffer());
}

public function testProvidingTestingAsEnvGivesErrorMessage(): void
{
command('env testing');
Expand Down
Loading