Skip to content

Commit 9881e8e

Browse files
authored
Merge pull request #23 from bestit/feature/optimize-env-handling
Feature/optimize env handling
2 parents b2d067f + 3fc9d53 commit 9881e8e

File tree

7 files changed

+150
-12
lines changed

7 files changed

+150
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
## Unreleased
2+
### Changed
3+
- The [EnvironmentActivator](docs/activator/environment.md) looks for values by `$_ENV` and `getenv()` @migo315
4+
- The [ArrayActivator](docs/activator/array.md) accept associative array in addition to numeric arrays @migo315
5+
26
### Added
37
- \#19 Add cookie extractor callable for [CookieActivator](docs/activator/cookie.md) @migo315
8+
- \#15 Support PHP7.3 in travics for tests @migo315
9+
- \#13 Add "match all" strategy for [ChainActivator](docs/activator/chain.md) @migo315
410

511
## [1.4.0]
612
### Added

docs/activator/array.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,34 @@ class MyClass
3030
}
3131
}
3232
```
33+
34+
In addition, you can pass an associative array with the feature name as key and the feature state as value. Value should
35+
be a `boolean` or a type which can be cast to `boolean` by the `filter_var` method.
36+
37+
```php
38+
// MyClass.php
39+
class MyClass
40+
{
41+
public function doSomething()
42+
{
43+
$activator = new ArrayActivator([
44+
'feature_abc' => true, // Activate the feature
45+
'feature_def' => 'false', // Disable the feature
46+
'feature_ghi' => 'yes' // Activate the feature
47+
'feature_qwe' => 0 // Disable the feature
48+
]);
49+
50+
$manager = new FeatureManager($activator);
51+
52+
// Will return true
53+
if ($manager->isActive('feature_ghi')) {
54+
// do something
55+
}
56+
57+
// Will return false
58+
if ($manager->isActive('feature_wxy')) {
59+
// do something
60+
}
61+
}
62+
}
63+
```

docs/activator/environment.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ EnvironmentActivator
33
The `EnvironmentActivator` will use environment variables for enable or disable a feature. In some cases your environment
44
variable names does not match your feature names. So you can define a simple map (array) as first constructor argument.
55

6+
This activator use `$_ENV` first to get the environment value and `getenv()` as fallback.
7+
68
Simple example: You have a feature `foo_bar` but your environment variables looks like `FEATURE_FOO_BAR`. Your class should
79
be something like this:
810

src/Activator/ArrayActivator.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
class ArrayActivator implements FeatureActivatorInterface
1414
{
1515
/**
16-
* Active features
16+
* Array of features
1717
*
1818
* @var array
1919
*/
20-
private $activeFeatures;
20+
private $features;
2121

2222
/**
2323
* ArrayActivator constructor.
2424
*
25-
* @param array $activeFeatures
25+
* @param array $features
2626
*/
27-
public function __construct(array $activeFeatures = [])
27+
public function __construct(array $features = [])
2828
{
29-
$this->activeFeatures = $activeFeatures;
29+
$this->features = $features;
3030
}
3131

3232
/**
@@ -42,6 +42,10 @@ public function getName()
4242
*/
4343
public function isActive($name, Context $context)
4444
{
45-
return in_array($name, $this->activeFeatures, true);
45+
if (array_key_exists($name, $this->features)) {
46+
return filter_var($this->features[$name], FILTER_VALIDATE_BOOLEAN);
47+
}
48+
49+
return in_array($name, $this->features, true);
4650
}
4751
}

src/Activator/EnvironmentActivator.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,24 @@ public function isActive($name, Context $context)
5656
}
5757

5858
if ($this->forceRequest === true && !array_key_exists($name, $this->variables)) {
59-
return filter_var(getenv($name), FILTER_VALIDATE_BOOLEAN);
59+
return $this->getEnv($name);
6060
}
6161

62-
return filter_var(getenv($this->variables[$name]), FILTER_VALIDATE_BOOLEAN);
62+
return $this->getEnv($this->variables[$name]);
63+
}
64+
65+
/**
66+
* Get enviroment value by $_ENV or getenv()
67+
*
68+
* @param $name
69+
*
70+
* @return string|null
71+
*/
72+
private function getEnv($name)
73+
{
74+
return filter_var(
75+
array_key_exists($name, $_ENV) ? $_ENV[$name] : getenv($name),
76+
FILTER_VALIDATE_BOOLEAN
77+
);
6378
}
6479
}

tests/Activator/ArrayActivatorTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,68 @@ public function testIsInArray()
7676

7777
static::assertTrue($activator->isActive('feature_abc', new Context()));
7878
}
79+
80+
/**
81+
* Test feature is in array (key => value)
82+
*
83+
* @return void
84+
*/
85+
public function testIsInArrayKeyValue()
86+
{
87+
$activator = new ArrayActivator([
88+
'feature_def',
89+
'feature_abc' => true
90+
]);
91+
92+
static::assertTrue($activator->isActive('feature_abc', new Context()));
93+
}
94+
95+
/**
96+
* Test feature is in array but false
97+
*
98+
* @return void
99+
*/
100+
public function testIsInArrayButFalse()
101+
{
102+
$activator = new ArrayActivator([
103+
'feature_def',
104+
'feature_abc' => false
105+
]);
106+
107+
static::assertFalse($activator->isActive('feature_abc', new Context()));
108+
}
109+
110+
/**
111+
* Test feature is in array / cast from integer
112+
*
113+
* @return void
114+
*/
115+
public function testIsInArrayCastFromInteger()
116+
{
117+
$activator = new ArrayActivator([
118+
'feature_def',
119+
'feature_abc' => 1,
120+
'feature_ghi' => 0
121+
]);
122+
123+
static::assertTrue($activator->isActive('feature_abc', new Context()));
124+
static::assertFalse($activator->isActive('feature_ghi', new Context()));
125+
}
126+
127+
/**
128+
* Test feature is in array / cast from string
129+
*
130+
* @return void
131+
*/
132+
public function testIsInArrayCastFromString()
133+
{
134+
$activator = new ArrayActivator([
135+
'feature_def',
136+
'feature_abc' => 'true',
137+
'feature_ghi' => 'false',
138+
]);
139+
140+
static::assertTrue($activator->isActive('feature_abc', new Context()));
141+
static::assertFalse($activator->isActive('feature_ghi', new Context()));
142+
}
79143
}

tests/Activator/EnvironmentActivatorTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public function testName()
4444
public function testUnmappedVariableWithoutForce()
4545
{
4646
putenv('FOOBAR_TEST_FLAGCEPTION=true');
47+
$_ENV['BAZZ_TEST_FLAGCEPTION'] = true;
4748

4849
$activator = new EnvironmentActivator();
4950
static::assertFalse($activator->isActive('FOOBAR_TEST_FLAGCEPTION', new Context()));
51+
static::assertFalse($activator->isActive('BAZZ_TEST_FLAGCEPTION', new Context()));
5052
}
5153

5254
/**
@@ -57,9 +59,11 @@ public function testUnmappedVariableWithoutForce()
5759
public function testUnmappedVariableWithForce()
5860
{
5961
putenv('FOOBAR_TEST_FLAGCEPTION=true');
62+
$_ENV['BAZZ_TEST_FLAGCEPTION'] = true;
6063

6164
$activator = new EnvironmentActivator([], true);
6265
static::assertTrue($activator->isActive('FOOBAR_TEST_FLAGCEPTION', new Context()));
66+
static::assertTrue($activator->isActive('BAZZ_TEST_FLAGCEPTION', new Context()));
6367
}
6468

6569
/**
@@ -70,6 +74,7 @@ public function testUnmappedVariableWithForce()
7074
public function testUnknownVariableWithForce()
7175
{
7276
putenv('FOOBAR_TEST_FLAGCEPTION=true');
77+
$_ENV['BAZZ_TEST_FLAGCEPTION'] = true;
7378

7479
$activator = new EnvironmentActivator([], true);
7580
static::assertFalse($activator->isActive('FOOBAR_FEATURE', new Context()));
@@ -83,11 +88,14 @@ public function testUnknownVariableWithForce()
8388
public function testMappedVariableWithoutForce()
8489
{
8590
putenv('FOOBAR_TEST_FLAGCEPTION=true');
91+
$_ENV['BAZZ_TEST_FLAGCEPTION'] = true;
8692

8793
$activator = new EnvironmentActivator([
88-
'feature_test' => 'FOOBAR_TEST_FLAGCEPTION'
94+
'feature_test' => 'FOOBAR_TEST_FLAGCEPTION',
95+
'feature_bazz' => 'BAZZ_TEST_FLAGCEPTION'
8996
]);
9097
static::assertTrue($activator->isActive('feature_test', new Context()));
98+
static::assertTrue($activator->isActive('feature_bazz', new Context()));
9199
}
92100

93101
/**
@@ -98,11 +106,14 @@ public function testMappedVariableWithoutForce()
98106
public function testMappedVariableWithForce()
99107
{
100108
putenv('FOOBAR_TEST_FLAGCEPTION=true');
109+
$_ENV['BAZZ_TEST_FLAGCEPTION'] = true;
101110

102111
$activator = new EnvironmentActivator([
103-
'feature_test' => 'FOOBAR_TEST_FLAGCEPTION'
112+
'feature_test' => 'FOOBAR_TEST_FLAGCEPTION',
113+
'feature_bazz' => 'BAZZ_TEST_FLAGCEPTION'
104114
], true);
105115
static::assertTrue($activator->isActive('feature_test', new Context()));
116+
static::assertTrue($activator->isActive('feature_bazz', new Context()));
106117
}
107118

108119
/**
@@ -113,9 +124,11 @@ public function testMappedVariableWithForce()
113124
public function testWrongMappedVariable()
114125
{
115126
putenv('FOOBAR_TEST_FLAGCEPTION=true');
127+
$_ENV['BAZZ_TEST_FLAGCEPTION'] = true;
116128

117129
$activator = new EnvironmentActivator([
118-
'feature_test' => 'FOOBAR_TEST_FLAGCEPTION'
130+
'feature_test' => 'FOOBAR_TEST_FLAGCEPTION',
131+
'feature_bazz' => 'BAZZ_TEST_FLAGCEPTION'
119132
]);
120133
static::assertFalse($activator->isActive('bazz_foo', new Context()));
121134
}
@@ -128,10 +141,13 @@ public function testWrongMappedVariable()
128141
public function testWrongEnvironmentMappedVariable()
129142
{
130143
putenv('FOOBAR_TEST_FLAGCEPTION=true');
144+
$_ENV['BAZZ_TEST_FLAGCEPTION'] = true;
131145

132146
$activator = new EnvironmentActivator([
133-
'feature_test' => 'FOOBAR_FAIL'
147+
'feature_test' => 'FOOBAR_FAIL',
148+
'feature_bazz' => 'BAZZ_FAIL'
134149
]);
135150
static::assertFalse($activator->isActive('feature_test', new Context()));
151+
static::assertFalse($activator->isActive('feature_bazz', new Context()));
136152
}
137153
}

0 commit comments

Comments
 (0)