Skip to content
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;
}
}
5 changes: 5 additions & 0 deletions plugins/CoreConsole/FeatureFlags/SystemSignals.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function getName(): string
{
return 'SystemSignals';
}

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

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 @@ -46,17 +46,15 @@ public function isFeatureActive(string $featureFlag): bool
return $featureFlagObj->getForcedFeatureFlagState();
}

$featureActive = false;

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

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

return $featureActive;
return false;
}

/**
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;
}
}
63 changes: 63 additions & 0 deletions plugins/FeatureFlags/Storage/CookieFeatureFlagStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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
*/
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
*/
public function disableFeatureFlag(FeatureFlagInterface $feature): void
{
// do nothing, as cookie values should only be set in frontend
Comment thread
sgiehl marked this conversation as resolved.
}

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

/**
* @internal
*/
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;
}
}
5 changes: 5 additions & 0 deletions plugins/FeatureFlags/tests/Unit/FeatureFlagManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
$logger
);

$this->assertEquals($expectedOutcome, $sut->isFeatureActive(get_class($mockFeature)));

Check failure on line 64 in plugins/FeatureFlags/tests/Unit/FeatureFlagManagerTest.php

View workflow job for this annotation

GitHub Actions / PHP (UnitTests, 7.2, PDO_MYSQL, Mysql, 5.7)

Failed asserting that true matches expected false.

Check failure on line 64 in plugins/FeatureFlags/tests/Unit/FeatureFlagManagerTest.php

View workflow job for this annotation

GitHub Actions / PHP (UnitTests, 8.2, PDO_MYSQL, Mariadb, 11.4)

Failed asserting that true matches expected false.

Check failure on line 64 in plugins/FeatureFlags/tests/Unit/FeatureFlagManagerTest.php

View workflow job for this annotation

GitHub Actions / PHP (UnitTests, 8.5, MYSQLI, Mysql, 8.0)

Failed asserting that true matches expected false.
}

public function testIsFeatureActiveReturnsForcedStateWithoutConsultingStorages(): void
Expand All @@ -82,6 +82,11 @@
{
return true;
}

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

$this->assertTrue($sut->isFeatureActive(get_class($featureFlag)));
Expand Down
Loading
Loading