Skip to content

Commit aa92c0d

Browse files
committed
Context condition matching
1 parent b0e6928 commit aa92c0d

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

src/Condition.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Packaged\Context;
3+
4+
interface Condition
5+
{
6+
public function isSatisfied(Context $ctx);
7+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
namespace Packaged\Context\Conditions;
3+
4+
use Packaged\Context\Condition;
5+
use Packaged\Context\Context;
6+
7+
class ExpectEnvironment implements Condition
8+
{
9+
protected $_environment;
10+
11+
public function __construct($environment)
12+
{
13+
$this->_environment = $environment;
14+
}
15+
16+
public static function phpunit()
17+
{
18+
return new static(Context::ENV_PHPUNIT);
19+
}
20+
21+
public static function local()
22+
{
23+
return new static(Context::ENV_LOCAL);
24+
}
25+
26+
public static function dev()
27+
{
28+
return new static(Context::ENV_DEV);
29+
}
30+
31+
public static function qa()
32+
{
33+
return new static(Context::ENV_QA);
34+
}
35+
36+
public static function uat()
37+
{
38+
return new static(Context::ENV_UAT);
39+
}
40+
41+
public static function stage()
42+
{
43+
return new static(Context::ENV_STAGE);
44+
}
45+
46+
public static function prod()
47+
{
48+
return new static(Context::ENV_PROD);
49+
}
50+
51+
public function isSatisfied(Context $ctx)
52+
{
53+
return $ctx->getEnvironment() == $this->_environment;
54+
}
55+
}

src/Context.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Packaged\Config\ConfigProviderInterface;
55
use Packaged\Config\Provider\ConfigProvider;
6+
use Packaged\Context\Conditions\ExpectEnvironment;
67
use Packaged\Event\Channel\Channel;
78
use Packaged\Helpers\System;
89
use Packaged\Http\Cookies\CookieJar;
@@ -93,24 +94,28 @@ public function setProjectRoot(string $root)
9394
return $this;
9495
}
9596

97+
/** @deprecated user matches(new ExpectEnvironment($env)) */
9698
public function isEnv(string $env)
9799
{
98-
return $this->getEnvironment() === $env;
100+
return $this->matches(new ExpectEnvironment($env));
99101
}
100102

103+
/** @deprecated user matches(ExpectEnvironment::local()) */
101104
public function isLocal()
102105
{
103-
return $this->isEnv(static::ENV_LOCAL);
106+
return $this->matches(ExpectEnvironment::local());
104107
}
105108

109+
/** @deprecated user matches(ExpectEnvironment::prod()) */
106110
public function isProd()
107111
{
108-
return $this->isEnv(static::ENV_PROD);
112+
return $this->matches(ExpectEnvironment::prod());
109113
}
110114

115+
/** @deprecated user matches(ExpectEnvironment::phpunit()) */
111116
public function isUnitTest()
112117
{
113-
return $this->isEnv(static::ENV_PHPUNIT);
118+
return $this->matches(ExpectEnvironment::phpunit());
114119
}
115120

116121
public function getEnvironment(): string
@@ -303,6 +308,18 @@ public function parent($root = false): ?Context
303308
return $this->_parent;
304309
}
305310

311+
public function matches(Condition ...$condition): bool
312+
{
313+
foreach($condition as $cond)
314+
{
315+
if(!$cond->isSatisfied($this))
316+
{
317+
return false;
318+
}
319+
}
320+
return true;
321+
}
322+
306323
public function __debugInfo()
307324
{
308325
return [

0 commit comments

Comments
 (0)