Verified Commit 4b1c68c1 authored by Lee Rowlands's avatar Lee Rowlands Committed by Lee Rowlands
Browse files

Issue #3278318 by larowlan, mstrelan: Add a cache context for feature flags

parent 2a91da09
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -3,3 +3,9 @@ services:
    class: Drupal\featureflags\FlagManager
    arguments:
      - '@keyvalue'
  cache_context.featureflags:
    class: Drupal\featureflags\FeatureFlagContext
    arguments:
      - '@featureflags.manager'
    tags:
      - { name: cache.context }
+58 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\featureflags;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a class for a feature flags cache context.
 *
 * Use as follows:
 *   - featureflags:{id} where id is the feature flag ID,
 *     e.g featureflags:new_site.
 */
class FeatureFlagContext implements CacheContextInterface {

  /**
   * Feature flag manager.
   *
   * @var \Drupal\featureflags\FlagManager
   */
  protected FlagManager $flagManager;

  /**
   * Constructs a new FeatureFlagContext.
   *
   * @param \Drupal\featureflags\FlagManager $flagManager
   *   Feature flag manager.
   */
  public function __construct(FlagManager $flagManager) {
    $this->flagManager = $flagManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return new TranslatableMarkup('Feature flags');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext(string $flag_id = NULL): string {
    return $this->flagManager->get($flag_id, FALSE) ? '1' : '0';
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata(string $flag_id = NULL): CacheableMetadata {
    return (new CacheableMetadata())->addCacheTags(['config:feature_flag.flag.' . $flag_id]);
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -91,7 +91,7 @@ class FeatureFlagAdminTest extends BrowserTestBase {
    $assert->statusCodeEquals(200);
    $assert->linkExists('Add feature flag');
    $this->clickLink('Add feature flag');
    $this->assertContains(Url::fromRoute('entity.featureflag.add_form')->toString(), $this->getSession()->getCurrentUrl());
    $this->assertStringContainsString(Url::fromRoute('entity.featureflag.add_form')->toString(), $this->getSession()->getCurrentUrl());
    $name = $this->randomMachineName();
    $id = mb_strtolower($this->randomMachineName());
    $this->submitForm([
+4 −0
Original line number Diff line number Diff line
@@ -49,9 +49,13 @@ class FeatureFlagKernelTest extends KernelTestBase {

    $flag->setState(TRUE);
    $this->assertTrue($flag->getState());
    $context = \Drupal::service('cache_context.featureflags');
    $this->assertEquals('1', $context->getContext($id));
    $this->assertContains('config:feature_flag.flag.' . $id, $context->getCacheableMetadata($id)->getCacheTags());

    $flag->setState(FALSE);
    $this->assertFalse($flag->getState());
    $this->assertEquals('0', $context->getContext($id));

    $this->assertTrue(\Drupal::keyValue('featureflags')->has($id));
    $flag->delete();