Class Sealed
public sealed class FeatureGatePhase : IBuildPhase

Namespace: Moka.Docs.Engine.Phases

Filters pages from the build based on feature flags. Pages with a requires front matter field are excluded when the corresponding feature flag is disabled in the feature manager.

Inheritance

Inherits from: IBuildPhase

Constructors

NameDescription
FeatureGatePhase(IFeatureManager featureManager, FeatureGatePhase> logger) Filters pages from the build based on feature flags. Pages with a requires front matter field are excluded when the corresponding feature flag is disabled in the feature manager.

FeatureGatePhase(IFeatureManager featureManager, FeatureGatePhase> logger)

FeatureGatePhase.FeatureGatePhase(IFeatureManager featureManager, ILogger<FeatureGatePhase> logger)

Filters pages from the build based on feature flags. Pages with a requires front matter field are excluded when the corresponding feature flag is disabled in the feature manager.

Properties

NameDescription
Name
Order

Methods

Type Relationships
classDiagram
                    style FeatureGatePhase fill:#f9f,stroke:#333,stroke-width:2px
                    FeatureGatePhase --|> IBuildPhase : inherits
                
View Source
/// <summary>
///     Filters pages from the build based on feature flags.
///     Pages with a <c>requires</c> front matter field are excluded when
///     the corresponding feature flag is disabled in the feature manager.
/// </summary>
public sealed class FeatureGatePhase(IFeatureManager featureManager, ILogger<FeatureGatePhase> logger) : IBuildPhase
{
    /// <inheritdoc/>
    public string Name => "FeatureGate";
    /// <inheritdoc/>
    public int Order => 500;

    /// <inheritdoc/>
    public async Task ExecuteAsync(BuildContext context, CancellationToken ct = default)
    {
        int removed = 0;
        for (int i = context.Pages.Count - 1; i >= 0; i--)
        {
            ct.ThrowIfCancellationRequested();
            string? requiredFeature = context.Pages[i].FrontMatter.Requires;
            if (string.IsNullOrWhiteSpace(requiredFeature))
            {
                continue;
            }

            bool isEnabled = await featureManager.IsEnabledAsync(requiredFeature);
            if (!isEnabled)
            {
                logger.LogDebug("Excluding page {Route} — feature flag '{Feature}' is disabled", context.Pages[i].Route, requiredFeature);
                context.Pages.RemoveAt(i);
                removed++;
            }
        }

        if (removed > 0)
        {
            logger.LogInformation("Feature gating excluded {Count} page(s)", removed);
        }
    }
}
Was this page helpful?