Skip to content

Commit 44fc93c

Browse files
authored
Release v3.28.0
2 parents ad2b40b + d3a0773 commit 44fc93c

12 files changed

Lines changed: 849 additions & 4 deletions

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
## 3.28.0 (2022-05-18)
5+
6+
### Enhancements
7+
8+
* New APIs to support feature flag and experiment functionality. For more information, please see https://docs.bugsnag.com/product/features-experiments.
9+
[#646](https://github.com/bugsnag/bugsnag-php/pull/646)
10+
411
## 3.27.0 (2022-02-07)
512

613
### Enhancements

src/Client.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Composer\CaBundle\CaBundle;
2323
use GuzzleHttp;
2424

25-
class Client
25+
class Client implements FeatureDataStore
2626
{
2727
/**
2828
* The default event notification endpoint.
@@ -806,6 +806,54 @@ public function getMetaData()
806806
return $this->config->getMetaData();
807807
}
808808

809+
/**
810+
* Add a single feature flag to all future reports.
811+
*
812+
* @param string $name
813+
* @param string|null $variant
814+
*
815+
* @return void
816+
*/
817+
public function addFeatureFlag($name, $variant = null)
818+
{
819+
$this->config->addFeatureFlag($name, $variant);
820+
}
821+
822+
/**
823+
* Add multiple feature flags to all future reports.
824+
*
825+
* @param FeatureFlag[] $featureFlags
826+
* @phpstan-param list<FeatureFlag> $featureFlags
827+
*
828+
* @return void
829+
*/
830+
public function addFeatureFlags(array $featureFlags)
831+
{
832+
$this->config->addFeatureFlags($featureFlags);
833+
}
834+
835+
/**
836+
* Remove the feature flag with the given name from all future reports.
837+
*
838+
* @param string $name
839+
*
840+
* @return void
841+
*/
842+
public function clearFeatureFlag($name)
843+
{
844+
$this->config->clearFeatureFlag($name);
845+
}
846+
847+
/**
848+
* Remove all feature flags from all future reports.
849+
*
850+
* @return void
851+
*/
852+
public function clearFeatureFlags()
853+
{
854+
$this->config->clearFeatureFlags();
855+
}
856+
809857
/**
810858
* Set Bugsnag's error reporting level.
811859
*

src/Configuration.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Bugsnag;
44

5+
use Bugsnag\Internal\FeatureFlagDelegate;
56
use InvalidArgumentException;
67

7-
class Configuration
8+
class Configuration implements FeatureDataStore
89
{
910
/**
1011
* The default endpoint for event notifications.
@@ -84,7 +85,7 @@ class Configuration
8485
*/
8586
protected $notifier = [
8687
'name' => 'Bugsnag PHP (Official)',
87-
'version' => '3.27.0',
88+
'version' => '3.28.0',
8889
'url' => 'https://bugsnag.com',
8990
];
9091

@@ -116,6 +117,13 @@ class Configuration
116117
*/
117118
protected $metaData = [];
118119

120+
/**
121+
* The associated feature flags.
122+
*
123+
* @var FeatureFlagDelegate
124+
*/
125+
private $featureFlags;
126+
119127
/**
120128
* The error reporting level.
121129
*
@@ -196,6 +204,7 @@ public function __construct($apiKey)
196204

197205
$this->apiKey = $apiKey;
198206
$this->fallbackType = php_sapi_name();
207+
$this->featureFlags = new FeatureFlagDelegate();
199208

200209
// Add PHP runtime version to device data
201210
$this->mergeDeviceData(['runtimeVersions' => ['php' => phpversion()]]);
@@ -588,6 +597,64 @@ public function getMetaData()
588597
return $this->metaData;
589598
}
590599

600+
/**
601+
* Add a single feature flag to all future reports.
602+
*
603+
* @param string $name
604+
* @param string|null $variant
605+
*
606+
* @return void
607+
*/
608+
public function addFeatureFlag($name, $variant = null)
609+
{
610+
$this->featureFlags->add($name, $variant);
611+
}
612+
613+
/**
614+
* Add multiple feature flags to all future reports.
615+
*
616+
* @param FeatureFlag[] $featureFlags
617+
* @phpstan-param list<FeatureFlag> $featureFlags
618+
*
619+
* @return void
620+
*/
621+
public function addFeatureFlags(array $featureFlags)
622+
{
623+
$this->featureFlags->merge($featureFlags);
624+
}
625+
626+
/**
627+
* Remove the feature flag with the given name from all future reports.
628+
*
629+
* @param string $name
630+
*
631+
* @return void
632+
*/
633+
public function clearFeatureFlag($name)
634+
{
635+
$this->featureFlags->remove($name);
636+
}
637+
638+
/**
639+
* Remove all feature flags from all future reports.
640+
*
641+
* @return void
642+
*/
643+
public function clearFeatureFlags()
644+
{
645+
$this->featureFlags->clear();
646+
}
647+
648+
/**
649+
* @internal
650+
*
651+
* @return FeatureFlagDelegate
652+
*/
653+
public function getFeatureFlagsCopy()
654+
{
655+
return clone $this->featureFlags;
656+
}
657+
591658
/**
592659
* Set Bugsnag's error reporting level.
593660
*

src/FeatureDataStore.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Bugsnag;
4+
5+
interface FeatureDataStore
6+
{
7+
/**
8+
* Add a single feature flag.
9+
*
10+
* @param string $name
11+
* @param string|null $variant
12+
*
13+
* @return void
14+
*/
15+
public function addFeatureFlag($name, $variant = null);
16+
17+
/**
18+
* Add multiple feature flags.
19+
*
20+
* The new flags will be merged with any existing feature flags, with the
21+
* newer variant values taking precedence
22+
*
23+
* @param array $featureFlags
24+
* @phpstan-param list<FeatureFlag> $featureFlags
25+
*
26+
* @return void
27+
*/
28+
public function addFeatureFlags(array $featureFlags);
29+
30+
/**
31+
* Remove a single feature flag by name.
32+
*
33+
* @param string $name
34+
*
35+
* @return void
36+
*/
37+
public function clearFeatureFlag($name);
38+
39+
/**
40+
* Remove all feature flags.
41+
*
42+
* @return void
43+
*/
44+
public function clearFeatureFlags();
45+
}

src/FeatureFlag.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Bugsnag;
4+
5+
final class FeatureFlag
6+
{
7+
/**
8+
* A name that identifies this feature flag.
9+
*
10+
* @var string
11+
*/
12+
private $name;
13+
14+
/**
15+
* An optional variant for this feature flag.
16+
*
17+
* @var string|null
18+
*/
19+
private $variant;
20+
21+
/**
22+
* @param string $name a name that identifies this feature flag
23+
* @param string|null $variant an optional variant for this feature flag.
24+
*/
25+
public function __construct($name, $variant = null)
26+
{
27+
$this->name = $name;
28+
29+
// ensure the variant can only be null or a string as the API only
30+
// accepts strings (null values will be omitted from the payload)
31+
if ($variant !== null && !is_string($variant)) {
32+
$json = json_encode($variant);
33+
34+
// if JSON encoding fails, omit the variant
35+
$variant = $json === false ? null : $json;
36+
}
37+
38+
$this->variant = $variant;
39+
}
40+
41+
/**
42+
* Get the feature flag's name.
43+
*
44+
* @return string
45+
*/
46+
public function getName()
47+
{
48+
return $this->name;
49+
}
50+
51+
/**
52+
* Get the feature flag's variant.
53+
*
54+
* @return string|null
55+
*/
56+
public function getVariant()
57+
{
58+
return $this->variant;
59+
}
60+
61+
/**
62+
* Convert this feature flag into the format used by the Bugsnag Event API.
63+
*
64+
* This has two forms, either with a variant:
65+
* { "featureFlag": "name", "variant": "variant" }
66+
*
67+
* or if the feature flag has no variant:
68+
* { "featureFlag": "no variant" }
69+
*
70+
* @return array[]
71+
* @phpstan-return array{featureFlag: string, variant?: string}
72+
*/
73+
public function toArray()
74+
{
75+
if (is_string($this->variant)) {
76+
return ['featureFlag' => $this->name, 'variant' => $this->variant];
77+
}
78+
79+
return ['featureFlag' => $this->name];
80+
}
81+
}

0 commit comments

Comments
 (0)