diff --git a/src/Condition.php b/src/Condition.php new file mode 100644 index 0000000..a6ca2f9 --- /dev/null +++ b/src/Condition.php @@ -0,0 +1,7 @@ +_environment = $environment; + } + + public static function phpunit() + { + return new static(Context::ENV_PHPUNIT); + } + + public static function local() + { + return new static(Context::ENV_LOCAL); + } + + public static function dev() + { + return new static(Context::ENV_DEV); + } + + public static function qa() + { + return new static(Context::ENV_QA); + } + + public static function uat() + { + return new static(Context::ENV_UAT); + } + + public static function stage() + { + return new static(Context::ENV_STAGE); + } + + public static function prod() + { + return new static(Context::ENV_PROD); + } + + public function isSatisfied(Context $ctx) + { + return $ctx->getEnvironment() == $this->_environment; + } +} diff --git a/src/Context.php b/src/Context.php index 7bfb923..85b19d4 100644 --- a/src/Context.php +++ b/src/Context.php @@ -3,6 +3,7 @@ use Packaged\Config\ConfigProviderInterface; use Packaged\Config\Provider\ConfigProvider; +use Packaged\Context\Conditions\ExpectEnvironment; use Packaged\Event\Channel\Channel; use Packaged\Helpers\System; use Packaged\Http\Cookies\CookieJar; @@ -93,24 +94,28 @@ public function setProjectRoot(string $root) return $this; } + /** @deprecated user matches(new ExpectEnvironment($env)) */ public function isEnv(string $env) { - return $this->getEnvironment() === $env; + return $this->matches(new ExpectEnvironment($env)); } + /** @deprecated user matches(ExpectEnvironment::local()) */ public function isLocal() { - return $this->isEnv(static::ENV_LOCAL); + return $this->matches(ExpectEnvironment::local()); } + /** @deprecated user matches(ExpectEnvironment::prod()) */ public function isProd() { - return $this->isEnv(static::ENV_PROD); + return $this->matches(ExpectEnvironment::prod()); } + /** @deprecated user matches(ExpectEnvironment::phpunit()) */ public function isUnitTest() { - return $this->isEnv(static::ENV_PHPUNIT); + return $this->matches(ExpectEnvironment::phpunit()); } public function getEnvironment(): string @@ -303,6 +308,18 @@ public function parent($root = false): ?Context return $this->_parent; } + public function matches(Condition ...$condition): bool + { + foreach($condition as $cond) + { + if(!$cond->isSatisfied($this)) + { + return false; + } + } + return true; + } + public function __debugInfo() { return [