Skip to content
Snippets Groups Projects
Commit 8a131798 authored by jurgenhaas's avatar jurgenhaas
Browse files

#1 Coding standard compliance

parent e84b79d8
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -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.
}
}
......
......@@ -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"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment