-
Notifications
You must be signed in to change notification settings - Fork 11
feat: support isolated API instances #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
toddbaert
merged 3 commits into
open-feature:main
from
marcozabel:feat/isolated-api-instances
Jun 10, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenFeature\isolated; | ||
|
|
||
| use OpenFeature\OpenFeatureAPI; | ||
| use OpenFeature\interfaces\flags\API; | ||
|
|
||
| /** | ||
| * Factory for creating isolated OpenFeature API instances. | ||
| * | ||
| * ----------------- | ||
| * Requirement 1.8.1 | ||
| * ----------------- | ||
| * The API MUST expose a factory function which creates and returns a new, | ||
| * independent instance of the API. | ||
| * | ||
| * Each instance returned by this factory function maintains its own state, | ||
| * including providers, evaluation context, hooks, and event handlers. | ||
| * Instances created by the factory function do not share state with the | ||
| * "default" global singleton or with each other. | ||
| * | ||
| * ----------------- | ||
| * Requirement 1.8.3 | ||
| * ----------------- | ||
| * The factory function for creating isolated instances SHOULD be housed in a | ||
| * distinct module, import path, package, or namespace from the global | ||
| * singleton API. | ||
| * | ||
| * @see https://openfeature.dev/specification/sections/flag-evaluation#18-isolated-api-instances | ||
| * | ||
| * @experimental Section 1.8 of the OpenFeature specification is experimental | ||
| * and subject to change. | ||
| */ | ||
| final class OpenFeatureAPIFactory | ||
| { | ||
| /** | ||
| * Creates a new, independent API instance with fully isolated state. | ||
| * | ||
| * Usage: | ||
| * $api = OpenFeatureAPIFactory::createAPI(); | ||
| * $api->setProvider(new MyProvider()); | ||
| * $client = $api->getClient(); | ||
| * | ||
| * @experimental Section 1.8 of the OpenFeature specification is experimental | ||
| * and subject to change. | ||
| */ | ||
| public static function createAPI(): API | ||
| { | ||
| return new OpenFeatureAPI(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenFeature\Test\unit; | ||
|
|
||
| use OpenFeature\OpenFeatureAPI; | ||
| use OpenFeature\Test\APITestHelper; | ||
| use OpenFeature\Test\TestCase; | ||
| use OpenFeature\Test\TestHook; | ||
| use OpenFeature\Test\TestProvider; | ||
| use OpenFeature\implementation\flags\EvaluationContext; | ||
| use OpenFeature\implementation\provider\NoOpProvider; | ||
| use OpenFeature\interfaces\flags\API; | ||
| use OpenFeature\isolated\OpenFeatureAPIFactory; | ||
|
|
||
| class IsolatedAPITest extends TestCase | ||
| { | ||
| /** | ||
| * Requirement 1.8.1 | ||
| * | ||
| * The API MUST expose a factory function which creates and returns a new, | ||
| * independent instance of the API. | ||
| */ | ||
| public function testFactoryCreatesDistinctInstances(): void | ||
| { | ||
| $api1 = OpenFeatureAPIFactory::createAPI(); | ||
| $api2 = OpenFeatureAPIFactory::createAPI(); | ||
|
|
||
| $this->assertInstanceOf(API::class, $api1); | ||
| $this->assertInstanceOf(OpenFeatureAPI::class, $api1); | ||
| $this->assertNotSame($api1, $api2); | ||
| } | ||
|
|
||
| /** | ||
| * Requirement 1.8.1 | ||
| * | ||
| * Isolated instances do not share state with the global singleton and | ||
| * mutating an isolated instance does not affect the singleton's state. | ||
| */ | ||
| public function testIsolatedInstanceDoesNotInterfereWithSingleton(): void | ||
| { | ||
| $singleton = APITestHelper::new(); | ||
| $isolated = OpenFeatureAPIFactory::createAPI(); | ||
|
|
||
| $this->assertNotSame($singleton, $isolated); | ||
|
|
||
| // Mutate the isolated instance | ||
| $isolated->setProvider(new TestProvider()); | ||
| $isolated->addHooks(new TestHook()); | ||
| $isolated->setEvaluationContext(new EvaluationContext('isolated-key')); | ||
|
|
||
| // Singleton state remains unchanged | ||
| $this->assertInstanceOf(NoOpProvider::class, $singleton->getProvider()); | ||
| $this->assertEmpty($singleton->getHooks()); | ||
| } | ||
|
|
||
| /** | ||
| * Requirement 1.8.2 | ||
| * | ||
| * Instances returned by the factory function MUST conform to the same API | ||
| * contract as the global singleton, including flag evaluation, provider | ||
| * management, context, hooks, events, and shutdown functionality. | ||
| */ | ||
| public function testIsolatedInstanceConformsToAPIContract(): void | ||
| { | ||
| $api = OpenFeatureAPIFactory::createAPI(); | ||
|
|
||
| // Provider management | ||
| $provider = new TestProvider(); | ||
| $api->setProvider($provider); | ||
| $this->assertSame($provider, $api->getProvider()); | ||
| $this->assertEquals($provider->getMetadata(), $api->getProviderMetadata()); | ||
|
|
||
| // Hooks | ||
| $hook = new TestHook(); | ||
| $api->addHooks($hook); | ||
| $this->assertEquals([$hook], $api->getHooks()); | ||
|
|
||
| // Evaluation context | ||
| $context = new EvaluationContext('targeting-key'); | ||
| $api->setEvaluationContext($context); | ||
| $this->assertSame($context, $api->getEvaluationContext()); | ||
|
|
||
| // Client creation | ||
| $client = $api->getClient('test-domain', '1.0.0'); | ||
| $this->assertEquals('test-domain', $client->getMetadata()->getName()); | ||
| } | ||
|
|
||
| /** | ||
| * Requirement 1.8.1 | ||
| * | ||
| * Providers are isolated between instances. | ||
| */ | ||
| public function testProviderIsolation(): void | ||
| { | ||
| $api1 = OpenFeatureAPIFactory::createAPI(); | ||
| $api2 = OpenFeatureAPIFactory::createAPI(); | ||
|
|
||
| $api1->setProvider(new TestProvider()); | ||
|
|
||
| $this->assertInstanceOf(TestProvider::class, $api1->getProvider()); | ||
| $this->assertInstanceOf(NoOpProvider::class, $api2->getProvider()); | ||
| } | ||
|
marcozabel marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Requirement 1.8.1 | ||
| * | ||
| * Hooks are isolated between instances. | ||
| */ | ||
| public function testHookIsolation(): void | ||
| { | ||
| $api1 = OpenFeatureAPIFactory::createAPI(); | ||
| $api2 = OpenFeatureAPIFactory::createAPI(); | ||
|
|
||
| $hook = new TestHook(); | ||
| $api1->addHooks($hook); | ||
|
|
||
| $this->assertCount(1, $api1->getHooks()); | ||
| $this->assertEmpty($api2->getHooks()); | ||
| } | ||
|
marcozabel marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Requirement 1.8.1 | ||
| * | ||
| * Evaluation context is isolated between instances. | ||
| */ | ||
| public function testEvaluationContextIsolation(): void | ||
| { | ||
| $api1 = OpenFeatureAPIFactory::createAPI(); | ||
| $api2 = OpenFeatureAPIFactory::createAPI(); | ||
|
|
||
| $api1->setEvaluationContext(new EvaluationContext('key-1')); | ||
| $api2->setEvaluationContext(new EvaluationContext('key-2')); | ||
|
|
||
| $ctx1 = $api1->getEvaluationContext(); | ||
| $ctx2 = $api2->getEvaluationContext(); | ||
|
|
||
| $this->assertNotNull($ctx1); | ||
| $this->assertNotNull($ctx2); | ||
| $this->assertEquals('key-1', $ctx1->getTargetingKey()); | ||
| $this->assertEquals('key-2', $ctx2->getTargetingKey()); | ||
| } | ||
|
marcozabel marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Requirement 1.8.2 | ||
| * | ||
| * A client obtained from an isolated instance uses that instance's provider. | ||
| */ | ||
| public function testClientUsesItsOwnInstanceProvider(): void | ||
| { | ||
| $api1 = OpenFeatureAPIFactory::createAPI(); | ||
| $api2 = OpenFeatureAPIFactory::createAPI(); | ||
|
|
||
| $api1->setProvider(new TestProvider()); | ||
|
|
||
| $client1 = $api1->getClient('test', '1.0'); | ||
| $client2 = $api2->getClient('test', '1.0'); | ||
|
|
||
| $this->assertFalse($client1->getBooleanValue('flag-key', false)); | ||
| $this->assertFalse($client2->getBooleanValue('flag-key', false)); | ||
| } | ||
|
|
||
| /** | ||
| * Requirement 1.8.1 | ||
| * | ||
| * clearHooks on one instance does not affect another. | ||
| */ | ||
| public function testClearHooksDoesNotAffectOtherInstances(): void | ||
| { | ||
| $api1 = OpenFeatureAPIFactory::createAPI(); | ||
| $api2 = OpenFeatureAPIFactory::createAPI(); | ||
|
|
||
| $hook = new TestHook(); | ||
| $api1->addHooks($hook); | ||
| $api2->addHooks($hook); | ||
|
|
||
| $api1->clearHooks(); | ||
|
|
||
| $this->assertEmpty($api1->getHooks()); | ||
| $this->assertCount(1, $api2->getHooks()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.