Skip to content
This repository has been archived by the owner on Aug 25, 2019. It is now read-only.

Commit

Permalink
Ability to force environments in CLI, HTTP, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
dietrichm committed Jul 22, 2012
1 parent 428d5d4 commit 015bfd2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Core.php
Expand Up @@ -30,8 +30,8 @@ public function __construct($envName, array $dbConfig) {
/**
* Handle the current request.
*/
public function handleRequest($doEnvCheck = TRUE) {
if ($doEnvCheck && $this->getEnvironmentName() == "testing") {
public function handleRequest() {
if (defined("USEBB_UNIT_TESTS")) {
return;
}

Expand Down
12 changes: 12 additions & 0 deletions System/Context/AbstractContext.php
Expand Up @@ -136,4 +136,16 @@ public function handleException(\Exception $e) {
"exception" => $e
));
}

/**
* Get the forced environment name.
*
* Any environment name returned here will override the one passed to
* UseBB\Core::__construct.
*
* \returns Environment name
*/
public function getForcedEnvironmentName() {
return NULL;
}
}
18 changes: 18 additions & 0 deletions System/Context/CLI.php
Expand Up @@ -29,6 +29,7 @@ public function handleRequest() {
break;
}
}
echo "Done.\n";
}
}

Expand Down Expand Up @@ -95,4 +96,21 @@ public function handleException(\Exception $e) {
printf("Exception '%s' at %s:%d.\n%s\n",
$name, $e->getFile(), $e->getLine(), $e->getMessage());
}

/**
* Get the forced environment name.
*
* Any environment name returned here will override the one passed to
* UseBB\Core::__construct.
*
* \returns Environment name
*/
public function getForcedEnvironmentName() {
$opts = getopt("", array("env::"));
if (is_array($opts) && isset($opts["env"])) {
return $opts["env"];
}

return NULL;
}
}
13 changes: 11 additions & 2 deletions System/ServiceRegistry.php
Expand Up @@ -40,8 +40,13 @@ class ServiceRegistry {
public function __construct($envName, array $dbConfig) {
if (defined("USEBB_UNIT_TESTS")) {
$envName = "testing";
} elseif (!in_array($envName, array("production", "development"))) {
throw new \InvalidArgumentException("Unknown environment name.");
} elseif (($forced = $this->getForcedEnvironmentName()) !== NULL) {
$envName = $forced;
}

if (!in_array($envName, array("production", "development", "testing"))) {
throw new \InvalidArgumentException(
"Unknown environment name '" . $envName . "'.");
}

if (!isset($dbConfig[$envName]) || !is_array($dbConfig[$envName])) {
Expand All @@ -61,6 +66,10 @@ public function getEnvironmentName() {
return $this->envName;
}

private function getForcedEnvironmentName() {
return $this->get("context")->getForcedEnvironmentName();
}

/**
* Explicitly set a service to a given instance.
*
Expand Down

0 comments on commit 015bfd2

Please sign in to comment.