Skip to content
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

[POC] Allow activating feature flags using cookies #22939

Draft
wants to merge 2 commits into
base: 5.x-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/CoreConsole/FeatureFlags/CliMultiProcessSymfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function getName(): string
{
return 'CliMultiProcessSymfony';
}

public function allowsCookieOverwrite(): bool
{
return false;
}
}
2 changes: 2 additions & 0 deletions plugins/FeatureFlags/FeatureFlagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
interface FeatureFlagInterface
{
public function getName(): string;

public function allowsCookieOverwrite(): bool;
}
6 changes: 2 additions & 4 deletions plugins/FeatureFlags/FeatureFlagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ public function isFeatureActive(string $featureFlag): bool
return false;
}

$featureActive = false;

foreach ($this->storages as $storage) {
$isActive = $storage->isFeatureActive($featureFlagObj);

if ($isActive !== null) {
$featureActive = $isActive;
return $isActive;
}
}

return $featureActive;
return false;
}

private function createFeatureFlagObjFromString(string $featureFlag): ?FeatureFlagInterface
Expand Down
5 changes: 5 additions & 0 deletions plugins/FeatureFlags/FeatureFlags/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function getName(): string
{
return 'Example';
}

public function allowsCookieOverwrite(): bool
{
return false;
}
}
71 changes: 71 additions & 0 deletions plugins/FeatureFlags/Storage/CookieFeatureFlagStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\FeatureFlags\Storage;

use Piwik\Plugins\FeatureFlags\FeatureFlagInterface;
use Piwik\Plugins\FeatureFlags\FeatureFlagStorageInterface;

class CookieFeatureFlagStorage implements FeatureFlagStorageInterface
{
/**
* @internal
* @param FeatureFlagInterface $feature
* @return bool|null
*/
public function isFeatureActive(FeatureFlagInterface $feature): ?bool
{
if (!$feature->allowsCookieOverwrite()) {
return false;
}

$cookieName = $this->getCookieNameForFeature($feature->getName());

if (!isset($_COOKIE[$cookieName])) {
return null;
}

return $_COOKIE[$cookieName] == '1';
}

/**
* @internal
* @param FeatureFlagInterface $feature
* @return void
*/
public function disableFeatureFlag(FeatureFlagInterface $feature): void
{
// do nothing, as cookie values should only be set in frontend
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we display some warning or a message in CLI about this not being supported?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or CLI will never get here?

}

/**
* @internal
* @param FeatureFlagInterface $feature
* @return void
*/
public function enableFeatureFlag(FeatureFlagInterface $feature): void
{
// do nothing as cookie values should only be set in frontend
}

/**
* @internal
* @param string $feature
* @return void
*/
public function deleteFeatureFlag(string $featureName): void
{
// do nothing as cookie values should only be set in frontend
}

private function getCookieNameForFeature(string $featureName): string
{
return 'feature_' . $featureName;
}
}
1 change: 1 addition & 0 deletions plugins/FeatureFlags/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* The first one will be overwritten by the second one (if set).
*/
'featureflag.storages' => [
DI::get('Piwik\Plugins\FeatureFlags\Storage\CookieFeatureFlagStorage'),
DI::get('Piwik\Plugins\FeatureFlags\Storage\ConfigFeatureFlagStorage'),
],
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function getName(): string
{
return 'NotReal';
}

public function allowsCookieOverwrite(): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function getName(): string
{
return 'SystemTest';
}

public function allowsCookieOverwrite(): bool
{
return false;
}
}
Loading
Loading