Skip to content

Commit

Permalink
Context condition matching
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 25, 2022
1 parent b0e6928 commit aa92c0d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Packaged\Context;

interface Condition
{
public function isSatisfied(Context $ctx);
}
55 changes: 55 additions & 0 deletions src/Conditions/ExpectEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Packaged\Context\Conditions;

use Packaged\Context\Condition;
use Packaged\Context\Context;

class ExpectEnvironment implements Condition
{
protected $_environment;

public function __construct($environment)
{
$this->_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;
}
}
25 changes: 21 additions & 4 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 [
Expand Down

0 comments on commit aa92c0d

Please sign in to comment.