Skip to content

Commit

Permalink
PHPCS fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Dec 30, 2013
1 parent 1bf9cfd commit e6b9b6a
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 304 deletions.
8 changes: 2 additions & 6 deletions .editorconfig
Expand Up @@ -4,13 +4,9 @@
root = false

[*]
indent_style = tab
indent_size = 2
indent_style = space
indent_size = 4
charset = "utf-8"
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_style = space
indent_size = 2
260 changes: 130 additions & 130 deletions src/josegonzalez/Dotenv/Load.php
Expand Up @@ -7,181 +7,181 @@
use JsonSerializable;
use RuntimeException;

class Load implements JsonSerializable {
class Load implements JsonSerializable
{

protected $_filepath = null;
protected $_filepath = null;

protected $_environment = null;
protected $_environment = null;

public function __construct($filepath = null) {
$this->setFilepath($filepath);
return $this;
}

public function filepath() {
return $this->_filepath;
}

public function setFilepath($filepath = null) {
if ($filepath == null) {
$filepath = __DIR__ . DIRECTORY_SEPARATOR . '.env';
}
$this->_filepath = $filepath;
return $this;
}

public static function load($options = null) {
$filepath = null;
if (is_string($options)) {
$filepath = $options;
$options = array();
public function __construct($filepath = null) {
$this->setFilepath($filepath);
return $this;
}

$dotenv = new \josegonzalez\Dotenv\Load($filepath);
$dotenv->parse();
if (array_key_exists('expect', $options)) {
$dotenv->expect($options['expect']);
public function filepath() {
return $this->_filepath;
}

if (array_key_exists('define', $options)) {
$dotenv->define();
public function setFilepath($filepath = null) {
if ($filepath == null) {
$filepath = __DIR__ . DIRECTORY_SEPARATOR . '.env';
}
$this->_filepath = $filepath;
return $this;
}

if (array_key_exists('toEnv', $options)) {
$dotenv->toEnv($options['toEnv']);
}
public static function load($options = null) {
$filepath = null;
if (is_string($options)) {
$filepath = $options;
$options = array();
}

if (array_key_exists('toServer', $options)) {
$dotenv->toServer($options['toServer']);
}
$dotenv = new \josegonzalez\Dotenv\Load($filepath);
$dotenv->parse();
if (array_key_exists('expect', $options)) {
$dotenv->expect($options['expect']);
}

return $dotenv;
}
if (array_key_exists('define', $options)) {
$dotenv->define();
}

public function parse() {
if (!file_exists($this->_filepath)) {
throw new InvalidArgumentException(sprintf("Environment file '%s' is not found.", $this->_filepath));
}
if (array_key_exists('toEnv', $options)) {
$dotenv->toEnv($options['toEnv']);
}

if (!is_readable($this->_filepath)) {
throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath));
}
if (array_key_exists('toServer', $options)) {
$dotenv->toServer($options['toServer']);
}

$fc = file_get_contents($this->_filepath);
if ($fc === false) {
throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath));
return $dotenv;
}

$lines = explode(PHP_EOL, $fc);
public function parse() {
if (!file_exists($this->_filepath)) {
throw new InvalidArgumentException(sprintf("Environment file '%s' is not found.", $this->_filepath));
}

$this->_environment = array();
foreach ($lines as $line) {
if (empty($line)) {
continue;
}
if (!is_readable($this->_filepath)) {
throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath));
}

if (!preg_match('/(?:export )?([a-zA-Z_][a-zA-Z0-9_]*)=(.*)/', $line, $matches)) {
continue;
}
$fc = file_get_contents($this->_filepath);
if ($fc === false) {
throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath));
}

$key = $matches[1];
$value = $matches[2];
if (preg_match('/^\'(.*)\'$/', $value, $matches)) {
$value = $matches[1];
} elseif (preg_match('/^"(.*)"$/', $value, $matches)) {
$value = $matches[1];
}
$lines = explode(PHP_EOL, $fc);

$this->_environment[$key] = $value;
}
$this->_environment = array();
foreach ($lines as $line) {
if (empty($line)) {
continue;
}

return $this;
}
if (!preg_match('/(?:export )?([a-zA-Z_][a-zA-Z0-9_]*)=(.*)/', $line, $matches)) {
continue;
}

public function expect() {
$this->requireParse('expect');
$key = $matches[1];
$value = $matches[2];
if (preg_match('/^\'(.*)\'$/', $value, $matches)) {
$value = $matches[1];
} elseif (preg_match('/^"(.*)"$/', $value, $matches)) {
$value = $matches[1];
}

$args = func_get_args();
if (count($args) == 0) {
throw new InvalidArgumentException("No arguments were passed to expect()");
$this->_environment[$key] = $value;
}

return $this;
}

if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
public function expect() {
$this->requireParse('expect');

$keys = (array) $args;
$missingEnvs = array();
$args = func_get_args();
if (count($args) == 0) {
throw new InvalidArgumentException("No arguments were passed to expect()");
}

foreach ($keys as $key) {
if (!isset($this->_environment[$key])) {
$missingEnvs[] = $key;
}
}
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}

if (!empty($missingEnvs)) {
throw new RuntimeException(sprintf("Required ENV vars missing: ['%s']", implode("', '", $missingEnvs)));
}
$keys = (array) $args;
$missingEnvs = array();

return $this;
}
foreach ($keys as $key) {
if (!isset($this->_environment[$key])) {
$missingEnvs[] = $key;
}
}

public function define() {
$this->requireParse('define');
foreach ($this->_environment as $key => $value) {
if (defined($key)) {
throw new LogicException(sprintf('Key "%s" has already been defined', $key));
}
if (!empty($missingEnvs)) {
throw new RuntimeException(sprintf("Required ENV vars missing: ['%s']", implode("', '", $missingEnvs)));
}

define($key, $value);
return $this;
}

return $this;
}
public function define() {
$this->requireParse('define');
foreach ($this->_environment as $key => $value) {
if (defined($key)) {
throw new LogicException(sprintf('Key "%s" has already been defined', $key));
}

public function toEnv($overwrite = false) {
$this->requireParse('toEnv');
foreach ($this->_environment as $key => $value) {
if (isset($_ENV[$key]) && !$overwrite) {
throw new LogicException(sprintf('Key "%s" has already been defined in $_ENV', $key));
}
define($key, $value);
}

$_ENV[$key] = $value;
return $this;
}

return $this;
}
public function toEnv($overwrite = false) {
$this->requireParse('toEnv');
foreach ($this->_environment as $key => $value) {
if (isset($_ENV[$key]) && !$overwrite) {
throw new LogicException(sprintf('Key "%s" has already been defined in $_ENV', $key));
}

public function toServer($overwrite = false) {
$this->requireParse('toServer');
foreach ($this->_environment as $key => $value) {
if (isset($_SERVER[$key]) && !$overwrite) {
throw new LogicException(sprintf('Key "%s" has already been defined in $_SERVER', $key));
}
$_ENV[$key] = $value;
}

$_SERVER[$key] = $value;
return $this;
}

return $this;
}
public function toServer($overwrite = false) {
$this->requireParse('toServer');
foreach ($this->_environment as $key => $value) {
if (isset($_SERVER[$key]) && !$overwrite) {
throw new LogicException(sprintf('Key "%s" has already been defined in $_SERVER', $key));
}

$_SERVER[$key] = $value;
}

public function toArray() {
$this->requireParse('environment');
return $this->_environment;
}
return $this;
}

public function jsonSerialize() {
return $this->toArray();
}
public function toArray() {
$this->requireParse('environment');
return $this->_environment;
}

public function __toString() {
return json_encode($this, JSON_PRETTY_PRINT);
}
public function jsonSerialize() {
return $this->toArray();
}

protected function requireParse($method) {
if (!is_array($this->_environment)) {
throw new LogicException(sprintf('Environment must be parsed before calling %s', $method));
public function __toString() {
return json_encode($this, JSON_PRETTY_PRINT);
}
}

protected function requireParse($method) {
if (!is_array($this->_environment)) {
throw new LogicException(sprintf('Environment must be parsed before calling %s', $method));
}
}
}

0 comments on commit e6b9b6a

Please sign in to comment.