-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15431 from craftcms/feature/env
`env` commands
- Loading branch information
Showing
4 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\console\controllers; | ||
|
||
use Craft; | ||
use craft\console\Controller; | ||
use craft\helpers\App; | ||
use craft\helpers\Console; | ||
use yii\base\Exception; | ||
use yii\console\ExitCode; | ||
|
||
/** | ||
* Sets or removes environment variables in the `.env` file. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.11.0 | ||
*/ | ||
class EnvController extends Controller | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public $defaultAction = 'show'; | ||
|
||
/** | ||
* Displays the value of an environment variable, or sets its value if $name contains `=`. | ||
* | ||
* php craft env CRAFT_DEV_MODE | ||
* php craft env CRAFT_DEV_MODE=true | ||
* | ||
* @param string $name | ||
* @return int | ||
*/ | ||
public function actionShow(string $name): int | ||
{ | ||
if (str_contains($name, '=')) { | ||
[$name, $value] = explode('=', $name, 2); | ||
return $this->runAction('set', [$name, $value]); | ||
} | ||
|
||
$value = App::env($name); | ||
$dump = Craft::dump($value, return: true); | ||
$this->stdout(trim($dump) . "\n"); | ||
return ExitCode::OK; | ||
} | ||
|
||
/** | ||
* Sets an environment variable in the `.env` file. | ||
* | ||
* php craft env/set CRAFT_DEV_MODE true | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* @return int | ||
*/ | ||
public function actionSet(string $name, string $value = ''): int | ||
{ | ||
if (str_contains($name, '=')) { | ||
[$name, $value] = explode('=', $name, 2); | ||
} | ||
|
||
try { | ||
Craft::$app->getConfig()->setDotEnvVar(trim($name), trim($value)); | ||
} catch (Exception $e) { | ||
$this->stderr($e->getMessage() . "\n", Console::FG_RED); | ||
return ExitCode::UNSPECIFIED_ERROR; | ||
} | ||
|
||
$dump = Craft::dump(App::env($name), return: true); | ||
$this->stdout(sprintf("%s %s.\n", $this->markdownToAnsi("`$name` is now"), trim($dump))); | ||
return ExitCode::OK; | ||
} | ||
|
||
/** | ||
* Removes an environment variable from the `.env` file. | ||
* | ||
* php craft env/remove CRAFT_DEV_MODE | ||
* | ||
* @param string $name | ||
* @return int | ||
*/ | ||
public function actionRemove(string $name): int | ||
{ | ||
try { | ||
Craft::$app->getConfig()->setDotEnvVar($name, false); | ||
} catch (Exception $e) { | ||
$this->stderr($e->getMessage() . "\n", Console::FG_RED); | ||
return ExitCode::UNSPECIFIED_ERROR; | ||
} | ||
|
||
$this->stdout($this->markdownToAnsi("`$name` has been removed.") . "\n"); | ||
return ExitCode::OK; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters