diff --git a/Dotenv.php b/Dotenv.php index 79b2b3849c2c971544429ee0e529d41ca6792e77..79ed239e10b34fe4fffd944f221ce0c6d25276db 100644 --- a/Dotenv.php +++ b/Dotenv.php @@ -2,6 +2,7 @@ namespace LakeDrops\Component\Dotenv; +use Composer\IO\IOInterface; use Symfony\Component\Dotenv\Dotenv as SymfonyDotenv; /** @@ -9,22 +10,36 @@ use Symfony\Component\Dotenv\Dotenv as SymfonyDotenv; */ final class Dotenv { - /** @var string */ + /** + * Name of the plugin "owning" this instance, used as a prefix. + * + * @var string + */ private $name; - /** @var \Composer\IO\IOInterface */ + /** + * The input-output object of the composer session. + * + * @var \Composer\IO\IOInterface + */ private $io; - /** @var \Symfony\Component\Dotenv\Dotenv */ + /** + * The Symfony Dotenv object. + * + * @var \Symfony\Component\Dotenv\Dotenv + */ private $dotenv; /** * Dotenv constructor. * * @param string $name + * Name of the plugin "owning" this instance, used as a prefix. * @param \Composer\IO\IOInterface $io + * The input-output object of the composer session. */ - public function __construct($name, $io) { + public function __construct($name, IOInterface $io) { $this->name = strtoupper($name); $this->io = $io; $this->dotenv = new SymfonyDotenv(); @@ -32,10 +47,23 @@ final class Dotenv { } /** + * Receive a value from the environment. + * + * First of all, it prefixes the key with the plugin name and converts the + * result into upper case. Then looking for the value with PHP's getenv and if + * nothing found, it prompts the user for input if the session is interactive. + * Finally, if the value is still not available, the default value is being + * used. + * * @param string $key + * The key of the environment variable. * @param string $prompt + * The text being displayed when prompting the user. + * @param string $default + * The default value. * * @return string + * The value. */ public function receive($key, $prompt, $default = '') { $key = $this->name . '_' . strtoupper($key); @@ -53,8 +81,16 @@ final class Dotenv { } /** + * Set an environment variable. + * + * Sets the environment variable into a .env file only if there isn't an + * environment variable present yet. The .env file will also be added to the + * .gitignore file. + * * @param string $key + * The name of the environment variable. * @param string $value + * The value of the environment variable. */ public function put($key, $value) { $key = strtoupper($key); @@ -68,12 +104,15 @@ final class Dotenv { passthru(escapeshellcmd('git -c "user.email=dotenv@lakedrops.com" ignore .env')); } + /** + * Load the local .env file. + */ private function load() { try { $this->dotenv->load('.env'); } catch (\Exception $ex) { - // Ignore + // Ignore. } } diff --git a/composer.json b/composer.json index 520c796fae603438f62698bf77c92aa47baf4fc2..92c51b3f466868e359f9823ae4ae9419f300aadc 100644 --- a/composer.json +++ b/composer.json @@ -27,11 +27,15 @@ "php": ">=5.6", "symfony/dotenv": "^3.3" }, + "require-dev": { + "composer/composer": "^1.5.0", + "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3", + "drupal/coder": "^8.2", + "phpunit/phpunit": "^4.8.0" + }, + "minimum-stability": "dev", + "prefer-stable": true, "autoload": { "psr-4": { "LakeDrops\\Component\\Dotenv\\": "" } - }, - "require-dev": { - "composer/composer": "dev-master", - "phpunit/phpunit": "^4.4.0" } }