Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Feature Flag Bundle Laravel Package

ajgarlag/feature-flag-bundle

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Symfony-Specific: The bundle is tightly coupled to Symfony’s ecosystem (e.g., #[AsFeature] attributes, ProviderInterface, and Symfony’s service container). While this ensures seamless integration with Symfony applications, it does not natively support Laravel or PHP standalone projects without abstraction layers (e.g., Symfony Bridge or custom adapters).
  • Feature Flag Pattern: The bundle implements a declarative, attribute-driven approach to feature flags, which aligns well with modern PHP frameworks’ dependency injection and metadata systems. However, Laravel’s service container and annotation/attribute systems differ from Symfony’s, requiring custom integration logic (e.g., mapping attributes to Laravel’s service providers or using traits/interfaces).
  • Extensibility: The ProviderInterface allows for custom backends (e.g., database, API-based, or hybrid providers), which is valuable for Laravel’s flexible architecture. However, Laravel’s event system and service container would need to be adapted to the bundle’s provider chain model.

Integration Feasibility

  • High-Level Abstraction Needed: To integrate this into Laravel, a wrapper layer would be required to:
    • Translate Symfony’s #[AsFeature] attributes to Laravel’s service bindings or annotations (e.g., using illuminate/support/Traits or custom annotations).
    • Adapt the ProviderInterface to Laravel’s service provider or container extensions (e.g., via Illuminate\Contracts\Container\Container).
    • Replace Symfony’s routing conditions (feature_is_enabled) with Laravel’s route middleware or service-based guards.
  • Twig Integration: Laravel uses Blade, so the Twig functions (feature_is_enabled) would need to be reimplemented as Blade directives or helper functions (e.g., feature_flag()).
  • Database/External Providers: Laravel’s Eloquent or query builder could replace Symfony’s Doctrine example, but the ProviderInterface would need to be adapted to Laravel’s DI container.

Technical Risk

  • Symfony Dependencies: The bundle relies on Symfony’s HttpKernel, DependencyInjection, and Attribute components, which are not natively available in Laravel. Risk mitigation requires:
    • Polyfills for missing Symfony classes (e.g., Attribute, ContainerInterface).
    • Custom service container integration to bridge Symfony’s ProviderInterface with Laravel’s ServiceProvider.
  • Attribute System: Laravel’s PHP 8+ attributes are supported, but Symfony’s #[AsFeature] attribute would need to be redefined or mapped to Laravel’s service registration (e.g., via register() in a service provider).
  • Routing Conditions: Laravel’s routing system (e.g., Route::where(), middleware) does not natively support dynamic conditions like Symfony’s condition attribute. Workarounds include:
    • Middleware-based feature checks (e.g., FeatureFlagMiddleware).
    • Service-based route filtering (e.g., Gate::for() or custom Closure guards).
  • Performance Overhead: The provider chain adds lookup latency. In Laravel, this could be optimized by:
    • Caching provider results (e.g., using Laravel’s cache system).
    • Prioritizing providers (e.g., database > API > config).

Key Questions

  1. Is Symfony Interoperability Required?

    • If the goal is to migrate from Symfony, this bundle could serve as a reference implementation for a Laravel-specific feature flag system.
    • If standalone Laravel integration is the priority, a custom implementation (e.g., using Laravel’s Config, Cache, or Database) may be simpler.
  2. What Are the Feature Flag Use Cases?

    • A/B Testing: Requires context-aware providers (e.g., user segments, cookies).
    • Kill Switches: Needs low-latency checks (e.g., Redis-backed providers).
    • Gradual Rollouts: May need percentage-based logic (not natively supported; would require custom providers).
  3. How Will Providers Be Implemented?

    • Database: Laravel’s Eloquent or Query Builder.
    • API: Guzzle or HTTP client with caching.
    • Environment: Laravel’s .env or config files.
  4. What’s the Fallback for Unsupported Features?

    • Routing Conditions: Middleware or Route::middleware().
    • Twig Functions: Blade directives or global helpers.
    • Attributes: Laravel’s #[Inject] or custom annotations.
  5. How Will Testing Be Handled?

    • Laravel’s testing tools (e.g., Mockery, Pest) would need to mock providers and service bindings.
    • Attribute-based features may require custom test traits to register test flags.

Integration Approach

Stack Fit

  • Laravel’s Compatibility:

    • Service Container: Laravel’s Illuminate\Container can replace Symfony’s ContainerInterface with adapters (e.g., Symfony\Component\DependencyInjection\ContainerInterface polyfill).
    • Attributes: Laravel supports PHP 8+ attributes, but Symfony’s #[AsFeature] must be redefined or mapped to Laravel’s service registration (e.g., via a FeatureFlagServiceProvider).
    • Routing: Laravel’s middleware or route filters can replace Symfony’s condition attribute.
    • Templating: Blade directives or global helper functions can replace Twig extensions.
  • Key Mappings:

    Symfony Feature Laravel Equivalent Implementation Notes
    #[AsFeature] Attribute Custom annotation or trait Use Illuminate\Support\Traits\Macroable or a service provider to register features.
    ProviderInterface Laravel Service Provider Implement a FeatureFlagProvider interface and bind it to the container.
    Routing conditions Middleware or Route::where() Create FeatureFlagMiddleware or use Gate::for().
    Twig functions Blade directives or helpers Register Blade::directive() or global feature_flag() helper.

Migration Path

  1. Phase 1: Core Integration

    • Step 1: Create a Laravel Service Provider (FeatureFlagServiceProvider) to:
      • Register feature flag services (e.g., FeatureFlagManager).
      • Bind the ProviderInterface to Laravel’s container.
    • Step 2: Implement a base FeatureFlagProvider class that adapts Symfony’s ProviderInterface to Laravel’s DI.
    • Step 3: Replace #[AsFeature] with a Laravel-compatible attribute or service registration macro.
  2. Phase 2: Backend Providers

    • Implement Laravel-specific providers:
      • Database Provider: Use Eloquent to fetch flags from a feature_flags table.
      • Cache Provider: Store flags in Laravel’s cache (e.g., Redis).
      • Config Provider: Load flags from config/feature-flags.php.
    • Example:
      // app/Providers/FeatureFlagDatabaseProvider.php
      class FeatureFlagDatabaseProvider implements FeatureFlagProvider
      {
          public function get(string $featureName): ?callable
          {
              return function () use ($featureName) {
                  return FeatureFlag::where('name', $featureName)->value('enabled');
              };
          }
      }
      
  3. Phase 3: Routing and Templating

    • Routing: Create middleware to check flags before route execution:
      // app/Http/Middleware/FeatureFlagMiddleware.php
      public function handle(Request $request, Closure $next, string $feature)
      {
          if (!feature_flag()->isEnabled($feature)) {
              abort(404); // or redirect
          }
          return $next($request);
      }
      
    • Blade: Register a directive:
      Blade::directive('feature', function ($flag) {
          return "<?php if (feature_flag()->isEnabled({$flag})): ?>";
      });
      
  4. Phase 4: Testing and Validation

    • Write Pest/Laravel tests to mock providers and verify flag behavior.
    • Test edge cases (e.g., missing flags, provider failures).

Compatibility

  • Laravel 10+: Fully compatible with PHP 8.1+ attributes and Symfony’s DI components.
  • Legacy Laravel: May require polyfills for older PHP versions (e.g., symfony/attribute for PHP 8.0).
  • Symfony Bridge: If partial Symfony integration is needed, use symfony/http-foundation or symfony/dependency-injection as a composer dependency.

Sequencing

  1. Start with a Minimal Viable Integration:
    • Implement config-based flags first (simplest provider).
    • Add database provider next for persistence.
  2. Gradually Add Complexity:
    • Context-aware providers (e.g.,
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity