### Old Configuration Example Source: https://github.com/bestit/flagception-bundle/blob/master/UPGRADE-3.0.md This is an example of the configuration structure used in previous versions of the bundle. ```yaml best_it_feature_toggle: features: feature_123: active: true ``` -------------------------------- ### New Configuration Example Source: https://github.com/bestit/flagception-bundle/blob/master/UPGRADE-3.0.md This is an example of the updated configuration structure for version 3.0, where 'active' is replaced by 'default'. ```yaml flagception: features: feature_123: default: true ``` -------------------------------- ### Install Database Activator via Composer Source: https://github.com/bestit/flagception-bundle/blob/master/docs/database.md Install the Database Activator package using Composer. This command requires Composer to be installed globally. ```bash $ composer require flagception/database-activator ``` -------------------------------- ### Install Database Activator Package Source: https://context7.com/bestit/flagception-bundle/llms.txt Install the `flagception/database-activator` package to enable storing feature flag states in a database for runtime management. ```bash composer require flagception/database-activator ``` -------------------------------- ### Install Flagception Bundle Source: https://github.com/bestit/flagception-bundle/blob/master/README.md Use Composer to install the Flagception bundle. This command adds the bundle to your project's dependencies. ```console composer require flagception/flagception-bundle ``` -------------------------------- ### Feature Configuration Example Source: https://github.com/bestit/flagception-bundle/blob/master/docs/activator.md Define features in your config.yml file. The default value is used by the ConfigActivator if no other activator returns true. ```yaml # config.yml flagception: features: feature_123: default: false ``` -------------------------------- ### Feature Configuration Example Source: https://github.com/bestit/flagception-bundle/blob/master/docs/profiler.md This YAML snippet demonstrates how to configure features within the Flagception Bundle. It shows the 'default', 'constraint', 'env', and 'cookie' properties for a feature, illustrating their purpose and expected values. ```yaml # config.yml flagception: features: feature_123: # The default property belongs to 'array' default: false # The constraint property belongs to 'constraint' constraint: 'user_id === 12' # The env property belongs to 'environment' env: 'FEATURE_123' # The cookie property belongs to 'cookie' cookie: true # Contentful fields are defined in Contentful and not in your config # The activator called "contentful" ``` -------------------------------- ### Download Flagception Bundle using Composer Source: https://github.com/bestit/flagception-bundle/blob/master/docs/install.md Execute this command in your project directory to download the latest stable version of the bundle. Requires Composer to be installed globally. ```bash $ composer require flagception/flagception-bundle ``` -------------------------------- ### Profiler Integration Example Source: https://context7.com/bestit/flagception-bundle/llms.txt The bundle automatically integrates with Symfony's web profiler. This configuration snippet shows how different activators (constraint, environment, cookie) are displayed in the profiler. ```yaml # config/packages/flagception.yaml flagception: features: feature_dashboard: default: false constraint: 'user_id === 12' # Shows as 'constraint' activator env: 'FEATURE_DASHBOARD' # Shows as 'environment' activator cookie: true # Shows as 'cookie' activator activators: cookie: enable: true database: enable: true # Shows as 'database' activator ``` -------------------------------- ### Define Global Variable with ContextDecorator Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Example of a ContextDecorator class to add global variables 'user_id' and 'user_role' to the context. Ensure the service is tagged with 'flagception.context_decorator'. ```php # UserContextDecorator.php class UserContextDecorator implements ContextDecoratorInterface { private $user; public function __construct(User $user) { $this->user = $user; } public function getName(): string { return 'user_context_decorator'; } public function decorate(Context $context): Context { $context->add('user_id', $this->user->getId); $context->add('user_role', $this->user->getRole()); return $context; } } ``` -------------------------------- ### Define Custom Expression Language Provider Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Example of creating a custom ExpressionFunctionProvider for the 'date' function. This allows custom functions to be used in constraints. Remember to tag the provider with 'flagception.expression_language_provider'. ```php // DateProvider.php class DateProvider implements ExpressionFunctionProviderInterface { /** * {@inheritdoc} */ public function getFunctions() { return [ new ExpressionFunction( 'date', function ($value) { return sprintf('date(%1$s, time())', $value); }, function ($arguments, $str) { return date($str, time()); } ), ]; } } ``` -------------------------------- ### Simple Feature Flag Check in Twig Source: https://github.com/bestit/flagception-bundle/blob/master/docs/twig.md Use the `feature` function to conditionally render content based on whether a feature flag is active. No additional setup is required beyond the bundle's installation. ```twig {% if feature('feature_123') %} {# ... #} {% endif %} ``` -------------------------------- ### Implement Custom Expression Functions Source: https://context7.com/bestit/flagception-bundle/llms.txt Implement ExpressionFunctionProviderInterface to define custom functions for the expression language. This example shows how to create 'ab_group' for A/B testing and 'in_region' for geographic checks. ```php sprintf('(crc32(%s) %% %s)', $userId, $groups), fn($args, $userId, $groups) => crc32((string) $userId) % $groups ), // Check geographic region new ExpressionFunction( 'in_region', fn($region, $allowed) => sprintf('in_array(%s, %s)', $region, $allowed), fn($args, $region, $allowed) => in_array($region, $allowed, true) ), ]; } } ``` -------------------------------- ### Define Local Variable in Constraint (YAML) Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Example of a feature constraint in YAML that uses a locally defined 'user_id' variable. ```yaml flagception: features: # This feature will only be active, if the current user has id 12 feature_123: default: false constraint: 'user_id == 12' ``` -------------------------------- ### Check Multiple Features in XML Routing Source: https://github.com/bestit/flagception-bundle/blob/master/docs/route.md Define multiple feature flags for a route using an array of strings within the '' element in XML routing. This example is for Symfony 3.4/4.0 and later. ```xml AppBundle:Blog:list feature_123 feature_456 AppBundle:Blog:list feature_123 feature_456 ``` -------------------------------- ### Define Feature in XML Routing Source: https://github.com/bestit/flagception-bundle/blob/master/docs/route.md Configure feature flags within XML routing definitions using the '' element. This example shows configuration for Symfony 3.4/4.0 and later. ```xml AppBundle:Blog:list feature_123 AppBundle:Blog:list feature_123 ``` -------------------------------- ### Use Custom Date Function in Constraint Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Example of using the custom 'date' function within a feature constraint in YAML, after defining it in a custom provider. ```yaml flagception: features: feature_abc: default: false constraint: 'date("H") > 8 and date("H") < 18' ``` -------------------------------- ### Enable Database Activator with Connection String Source: https://github.com/bestit/flagception-bundle/blob/master/docs/database.md Enable the database activator and configure the connection using a database URL. ```yaml # config.yml flagception: activators: database: # Enable database activator (default: false) enable: true # Connection string url: 'mysql://user:secret@localhost/mydb' ``` -------------------------------- ### Configure Database Activator Source: https://context7.com/bestit/flagception-bundle/llms.txt Set up the database activator in `config/packages/flagception.yaml`. Supports connection via URL, DBAL service, PDO service, or direct credentials. Caching can be enabled for performance. ```yaml # config/packages/flagception.yaml flagception: activators: database: enable: true # Option 1: Connection URL url: '%env(DATABASE_URL)%' # Option 2: DBAL service reference # dbal: 'doctrine.dbal.default_connection' # Option 3: PDO service reference # pdo: 'app.pdo_connection' # Option 4: Credential fields # credentials: # dbname: 'mydb' # user: '%env(DB_USER)%' # password: '%env(DB_PASSWORD)%' # host: 'localhost' # driver: 'pdo_mysql' # Custom table/column names (optional) options: db_table: 'flagception_features' db_column_feature: 'feature' db_column_state: 'state' # Enable caching for better performance cache: enable: true pool: 'cache.app' lifetime: 3600 ``` -------------------------------- ### Enable Database Activator with Credential Fields Source: https://github.com/bestit/flagception-bundle/blob/master/docs/database.md Enable the database activator and configure the connection using individual credential fields. ```yaml # config.yml flagception: activators: database: # Enable database activator (default: false) enable: true # By credentials field credentials: dbname: 'mydb', user: 'user', password: 'secret', # You can use env too (%env(MYSQL_DATABASE)%) host: 'localhost', driver: 'pdo_mysql' ``` -------------------------------- ### Implement Custom Feature Activator Source: https://github.com/bestit/flagception-bundle/blob/master/docs/activator.md Create a service class that implements FeatureActivatorInterface. Tag it with 'flagception.activator' and an optional priority. The isActive method determines the feature state based on the provided context. ```php # AdminActivator.php class AdminActivator implements FeatureActivatorInterface { /** * {@inheritdoc} */ public function getName() { # Return an unqiue name for this activator return 'admin'; } /** * @var string $name The requested feature name (eg. 'feature_123') * @var Context $context The context object which all key / values */ public function isActive($name, Context $context) { # Always return true if the user role contain 'ROLE_ADMIN' return in_array('ROLE_ADMIN', $context->get('user_roles'), true); } } ``` -------------------------------- ### Enable Database Activator with PDO Instance Source: https://github.com/bestit/flagception-bundle/blob/master/docs/database.md Enable the database activator and specify a PDO service ID for the database connection. ```yaml # config.yml flagception: activators: database: # Enable database activator (default: false) enable: true # By pdo instance pdo: 'pdo.service.id' ``` -------------------------------- ### Implement Custom Feature Activator (PHP) Source: https://context7.com/bestit/flagception-bundle/llms.txt Create a custom feature activator by implementing `FeatureActivatorInterface`. This allows for custom logic to determine if a feature is active, chaining activators by priority. ```php get('user_roles') ?? []; return in_array('ROLE_ADMIN', $roles, true); } } ``` -------------------------------- ### Enable Route Metadata in Configuration Source: https://github.com/bestit/flagception-bundle/blob/master/docs/route.md Configure the Flagception bundle to enable route metadata checks. This setting is optional and defaults to true. ```yaml # config.yml flagception: features: feature_123: default: true # Use route attributes? (optional) routing_metadata: # Enable controller annotation (default: true) enable: true ``` -------------------------------- ### Enable Database Activator with DBAL Instance Source: https://github.com/bestit/flagception-bundle/blob/master/docs/database.md Enable the database activator and specify a DBAL service ID for the database connection. ```yaml # config.yml flagception: activators: database: # Enable database activator (default: false) enable: true # By dbal instance dbal: 'dbal.service.id' ``` -------------------------------- ### Combine Default, Environment, and Constraint Checks Source: https://github.com/bestit/flagception-bundle/blob/master/docs/environment.md Define a feature flag with a default value, an environment variable check, and a constraint. The checks are performed in order: default, then environment variable, then constraint. ```yaml flagception: features: feature_123: default: false env: FEATURE_NAME_FROM_ENV constraint: 'user_role == ROLE_ADMIN' ``` -------------------------------- ### Configure Features in YAML Source: https://context7.com/bestit/flagception-bundle/llms.txt Define feature flags in the flagception.yaml configuration file. Supports static defaults, environment variable bindings, and constraint expressions. ```yaml # config/packages/flagception.yaml flagception: features: # Simple feature with static default feature_dark_mode: default: true # Feature controlled by environment variable feature_new_checkout: env: FEATURE_NEW_CHECKOUT # Feature with constraint expression feature_beta_access: default: false constraint: 'user_id == 12 or "ROLE_BETA" in user_roles' # Combined: default -> env -> constraint chain feature_premium: default: false env: FEATURE_PREMIUM_ENABLED constraint: '(date("H") > 8 and date("H") < 18) or "ROLE_ADMIN" in user_role' ``` -------------------------------- ### Define Local Variable in Service Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Shows how to add local context variables 'user_id' and 'user_role' in a PHP service using the FeatureManager. ```php # FooService.php class FooService { /** * @var FeatureManagerInterface */ private $manager; /** * @param FeatureManagerInterface $manager * Service id: flagception.manager.feature_manager */ public function __construct(FeatureManagerInterface $manager) { $this->manager = $manager; } public function do() { // ... $context = new Context(); $context->add('user_id', 12); $context->add('user_role', 'ROLE_ADMIN'); if ($this->manager->isActive('feature_123', $context)) { // ... } // ... } } ``` -------------------------------- ### Configure Database Table and Column Names Source: https://github.com/bestit/flagception-bundle/blob/master/docs/database.md Customize the database table name and column names used by the Database Activator. ```yaml # config.yml flagception: activators: database: # Enable database activator (default: false) enable: true # Connection string url: 'mysql://user:secret@localhost/mydb' # Rename table and columns options: db_table: 'my_table' db_column_feature: 'my_cool_feature_name' db_column_state: 'my_current_feature_state' ``` -------------------------------- ### Enable Features via Cookie for Testing Source: https://context7.com/bestit/flagception-bundle/llms.txt Configure cookie-based activation to test features without changing deployment configuration. Users can enable features by setting a specific browser cookie. ```yaml # config/packages/flagception.yaml flagception: features: feature_new_design: default: false feature_secret: default: false cookie: false # Explicitly disable cookie activation for this feature activators: cookie: enable: true name: 'flagception_dev' # Cookie name (keep secret) separator: ',' # Multiple features: "feature_a,feature_b" mode: 'whitelist' # Only allow features defined in config (default) # mode: 'blacklist' # Allow any feature unless cookie: false ``` ```javascript // Enable features via browser console for testing document.cookie = "flagception_dev=feature_new_design,feature_beta_ui; path=/"; ``` -------------------------------- ### Enable Caching for Database Activator Source: https://github.com/bestit/flagception-bundle/blob/master/docs/database.md Configure caching for the Database Activator to improve performance by loading feature statuses from cache. ```yaml # config.yml flagception: activators: database: enable: true # ... cache: # Enable the cache option (default: false) enable: true # Set cache pool (default: cache.app) pool: cache.app # Set lifetime for cache in seconds (default: 3600) lifetime: 3600 ``` -------------------------------- ### Enable Controller Annotations in Configuration Source: https://github.com/bestit/flagception-bundle/blob/master/docs/annotation.md Configure the Flagception bundle to enable controller annotations by setting 'annotation.enable' to true in your config.yml. This allows feature checks directly within controller classes and methods. ```yaml flagception: features: feature_123: default: true # Use annotation? (optional) annotation: # Enable controller annotation (default: false) enable: true ``` -------------------------------- ### Use %env()% Syntax for Default Value Source: https://github.com/bestit/flagception-bundle/blob/master/docs/environment.md Utilize the %env()% syntax within the 'default' field to directly use the value of an environment variable as the initial state for a feature flag. ```yaml flagception: features: feature_123: default: '%env(FEATURE_NAME_FROM_ENV)%' ``` -------------------------------- ### Expression Language Method: match Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Demonstrates the 'match' method for performing regular expression matching within a constraint. It returns true or false. ```text match("/foo/i", "FOO") == true ``` -------------------------------- ### Enable Cookie Activator in Blacklist Mode Source: https://github.com/bestit/flagception-bundle/blob/master/docs/cookie.md Enable the cookie activator and switch to blacklist mode. In this mode, all features are activatable by cookie unless explicitly disabled in the configuration. ```yaml # config.yml flagception: features: # Activatable via cookie feature_123: default: false # Not activatable via cookie feature_456: cookie: false # Feature "feature_wyz" isn't defined in this config.yml but is activatable by cookie # Cookie settings cookie: enable: true mode: 'blacklist' ``` -------------------------------- ### Enable Annotations for Feature Flags (YAML) Source: https://context7.com/bestit/flagception-bundle/llms.txt Configure the Flagception bundle to enable annotation support for feature flags. This is a prerequisite for using the `@Feature` annotation on controllers and methods. ```yaml # config/packages/flagception.yaml flagception: features: feature_admin_v2: default: true feature_reports: default: false annotation: enable: true ``` -------------------------------- ### Control Features with Environment Variables Source: https://context7.com/bestit/flagception-bundle/llms.txt Manage feature flags using environment variables for deployment-time configuration. Supports direct binding and Symfony's env() syntax for boolean values. ```yaml # config/packages/flagception.yaml flagception: features: # Direct environment variable binding feature_maintenance_mode: env: FEATURE_MAINTENANCE_MODE # Using Symfony env() syntax feature_debug_panel: default: '%env(bool:FEATURE_DEBUG_PANEL)%' # Fallback chain: default -> env -> constraint feature_api_v2: default: false env: FEATURE_API_V2 constraint: 'user_id in [1, 2, 3]' ``` ```dotenv # .env FEATURE_MAINTENANCE_MODE=false FEATURE_DEBUG_PANEL=false FEATURE_API_V2=true ``` -------------------------------- ### Configure Features for Cookie Activation Source: https://github.com/bestit/flagception-bundle/blob/master/docs/cookie.md Configure individual features to be activatable or not activatable via cookies. Features not defined in config.yml are not activatable by default unless a blacklist mode is used. ```yaml # config.yml flagception: features: # Activatable via cookie feature_123: default: false # Not activatable via cookie feature_456: cookie: false # Feature "feature_wyz" isn't activatable via cookie because the feature isn't defined in your config.yml # Cookie settings cookie: # Enable cookie activator (default: false) enable: true # Cookie name - should be a secret key (default: 'flagception') name: 'flagception' # Cookie value separator for using with mutiple features (default: ',') separator: ',' ``` -------------------------------- ### Expression Language Method: ratio Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Shows the 'ratio' method, which returns a random true/false value based on a provided ratio between 0 and 1. ```text ratio(0.5) == true ``` -------------------------------- ### Protect Routes with Feature Flags (PHP) Source: https://context7.com/bestit/flagception-bundle/llms.txt Define routes with a `_feature` default to automatically return a 404 if the specified feature is inactive. Supports single or multiple feature requirements. ```php 'feature_new_blog'])] public function list(int $page): Response { // ... } /** * Multiple features required - returns 404 if ANY feature is inactive */ #[Route('/blog/beta/{slug}', name: 'blog_beta', defaults: ['_feature' => ['feature_blog_v2', 'feature_beta_access']])] public function betaPost(string $slug): Response { // ... } } ``` -------------------------------- ### Define Feature Constraints in YAML Source: https://context7.com/bestit/flagception-bundle/llms.txt Configure feature flags with various constraints like time-based activation, role-based access, percentage rollouts, and combined conditions. Ensure constraints are valid expressions. ```yaml flagception: features: # Time-based activation (8am to 6pm) feature_business_hours: default: false constraint: 'date("H") >= 8 and date("H") < 18' # Role-based activation feature_premium: default: false constraint: '"ROLE_PREMIUM" in user_roles or "ROLE_ADMIN" in user_roles' # Percentage rollout using ratio() feature_new_ui: default: false constraint: 'ratio(0.25) == true' # Combined conditions feature_beta_testing: default: false constraint: '(user_id > 1000 and match("/^beta/", user_email)) or date("N") <= 5' ``` -------------------------------- ### Define Features in Configuration Source: https://github.com/bestit/flagception-bundle/blob/master/docs/usage.md Define features and their default active/inactive states in your application's configuration files. Feature names are normalized to snake_case. ```yaml flagception: # Your Features (optional you left it empty) features: # Feature name as key feature_123: # Default flag if inactive or active (default: false) default: true feature_abc: default: false ``` -------------------------------- ### Configure Features in Symfony Source: https://github.com/bestit/flagception-bundle/blob/master/README.md Define features in your Symfony configuration file. Features can have default states, be controlled by environment variables, or use custom constraints. ```yaml flagception: # Your Features (optional you left it empty) features: # Feature name as key feature_123: # Default flag if inactive or active (default: false) default: true # Feature state from an environment variable feature_abc: env: FEATURE_ENV_ABC # Feature with constraint (active if user id is 12 OR it is between 8 am and 6 pm) feature_def: constraint: 'user_id == 12 or (date("H") > 8 and date("H") < 18)' # All togther (chain) feature_def: default: false env: FEATURE_ENV_ABC constraint: 'user_id == 12 or (date("H") > 8 and date("H") < 18)' ``` -------------------------------- ### Protect Controllers with Feature Annotations (PHP) Source: https://context7.com/bestit/flagception-bundle/llms.txt Use the `#[Feature]` annotation on controller classes or methods to control access based on feature flag status. Annotations must be enabled in the bundle configuration. ```php 8 and date("H") < 18) or "ROLE_ADMIN" in user_role' ``` -------------------------------- ### Register Custom Activators (YAML) Source: https://context7.com/bestit/flagception-bundle/llms.txt Register custom feature activators as services in your Symfony configuration, assigning them a priority. The bundle chains activators based on this priority. ```yaml # config/services.yaml services: App\Activator\AdminActivator: tags: - { name: flagception.activator, priority: 255 } App\Activator\PercentageRolloutActivator: tags: - { name: flagception.activator, priority: 100 } ``` -------------------------------- ### Service Declaration for ConfigActivator Source: https://github.com/bestit/flagception-bundle/blob/master/docs/activator.md Declare the ConfigActivator as a service in your Symfony application. It requires the 'flagception.constraint.constraint_resolver' argument and should be tagged with 'flagception.activator' and a priority. ```yaml flagception.activator.config_activator: class: Flagception\Bundle\FlagceptionBundle\Activator\ConfigActivator arguments: - '@flagception.constraint.constraint_resolver' tags: - { name: flagception.activator, priority: 100 } ``` -------------------------------- ### Protect Routes with Feature Flags (YAML) Source: https://context7.com/bestit/flagception-bundle/llms.txt Configure routes in YAML to include a `_feature` default, ensuring a 404 response when the feature is not active. This method supports both single and multiple feature flags. ```yaml # config/routes.yaml blog_list: path: /blog/{page} controller: App\Controller\BlogController::list defaults: _feature: 'feature_new_blog' blog_beta: path: /blog/beta/{slug} controller: App\Controller\BlogController::betaPost defaults: _feature: ['feature_blog_v2', 'feature_beta_access'] ``` -------------------------------- ### Database Table Schema for Feature Flags Source: https://context7.com/bestit/flagception-bundle/llms.txt Define the `flagception_features` table in your database to store feature flag states. The `feature` column is the primary key, and `state` is a boolean indicating activation. ```sql -- Table created automatically, or create manually: CREATE TABLE flagception_features ( feature VARCHAR(255) PRIMARY KEY, state BOOLEAN NOT NULL DEFAULT FALSE ); INSERT INTO flagception_features (feature, state) VALUES ('feature_premium', true); INSERT INTO flagception_features (feature, state) VALUES ('feature_beta', false); ``` -------------------------------- ### Enable Feature via Route Annotation Source: https://github.com/bestit/flagception-bundle/blob/master/docs/usage.md Use the `@Route` annotation with the `_feature` option to associate a route with a specific feature flag. Accessing the route will be restricted if the feature is inactive. ```php // src/AppBundle/Controller/BlogController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller { /** * @Route("/blog/{page}", name="blog_list", defaults={"_feature": "feature_123"}) */ public function listAction($page) { // ... } /** * @Route("/blog/{slug}", name="blog_show") */ public function showAction($slug) { // ... } } ``` -------------------------------- ### Enable Cookie Activator Configuration Source: https://github.com/bestit/flagception-bundle/blob/master/docs/cookie.md Enable the cookie activator and set a custom cookie name and separator in your config.yml. This configuration allows features to be activated by setting specific cookies. ```yaml # config.yml flagception: features: feature_123: default: false activators: # Cookie settings cookie: # Enable cookie activator (default: false) enable: true # Cookie name - should be a secret key (default: 'flagception') name: 'flagception' # Cookie value separator for using with mutiple features (default: ',') separator: ',' ``` -------------------------------- ### Define Local Variable in Twig Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Demonstrates how to pass local variables 'user_id' and 'user_role' to the feature function in Twig. ```twig {% if feature('feature_123', {'user_id': '12', 'user_role': 'ROLE_ADMIN'}) %} {# ... #} {% endif %} ``` -------------------------------- ### Check Features in Services Source: https://context7.com/bestit/flagception-bundle/llms.txt Inject FeatureManagerInterface into your services to programmatically check feature states. The isActive() method can take an optional context object for constraint evaluation. ```php manager = $manager; } public function processCheckout(User $user, Cart $cart): void { // Simple feature check if ($this->manager->isActive('feature_new_checkout')) { $this->processNewCheckout($cart); return; } // Feature check with context for constraint evaluation $context = new Context(); $context->add('user_id', $user->getId()); $context->add('user_roles', $user->getRoles()); $context->add('cart_total', $cart->getTotal()); if ($this->manager->isActive('feature_premium_shipping', $context)) { $this->applyPremiumShipping($cart); } $this->processLegacyCheckout($cart); } } ``` -------------------------------- ### Register Custom Expression Function Provider Source: https://context7.com/bestit/flagception-bundle/llms.txt Register the custom ExpressionFunctionProvider as a service in Symfony's services.yaml. This makes the custom functions available to the Flagception bundle. ```yaml # config/services.yaml services: App\ExpressionLanguage\FeatureFunctionProvider: tags: - { name: flagception.expression_language_provider } ``` -------------------------------- ### User Context Decorator for Security Data Source: https://context7.com/bestit/flagception-bundle/llms.txt Implement a custom context decorator to add user-specific information like ID, email, and roles to the feature flag context. This requires the Symfony Security component. ```php security = $security; } public function getName(): string { return 'user_context'; } public function decorate(Context $context): Context { $user = $this->security->getUser(); if ($user) { $context->add('user_id', $user->getId()); $context->add('user_email', $user->getEmail()); $context->add('user_roles', $user->getRoles()); } return $context; } } ``` -------------------------------- ### Expression Language Method: date Source: https://github.com/bestit/flagception-bundle/blob/master/docs/constraint.md Illustrates using the 'date' method within a constraint expression to check the current hour. This method utilizes PHP's `date` function with the current timestamp. ```text date("H") > 8 and date("H") < 18 ``` -------------------------------- ### Check Multiple Features in Route Annotation Source: https://github.com/bestit/flagception-bundle/blob/master/docs/route.md Define multiple feature flags for a route by passing an array to the '_feature' key in the @Route annotation. This ensures all specified features are active. ```php // src/AppBundle/Controller/BlogController.php // src/Controller/BlogController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller { /** * @Route("/blog/{page}", defaults={"_feature": {"feature_123", "feature_456"}}) */ public function listAction($page) { // ... } /** * @Route("/blog/{slug}") */ public function showAction($slug) { // ... } } ``` -------------------------------- ### Enable Flagception Bundle in Symfony 4.x Source: https://github.com/bestit/flagception-bundle/blob/master/docs/install.md Register the FlagceptionBundle in your config/bundles.php file for Symfony 4.x and later versions. ```php // config/bundles.php ['all' => true], ]; ``` -------------------------------- ### Use Custom Expression Functions in Feature Constraints Source: https://context7.com/bestit/flagception-bundle/llms.txt Utilize the custom 'ab_group' and 'in_region' functions within the flagception.yaml configuration to define feature constraints based on A/B testing groups or geographic regions. ```yaml # config/packages/flagception.yaml flagception: features: # Use custom ab_group function: users in group 0 get the feature feature_checkout_v2: default: false constraint: 'ab_group(user_id, 2) == 0' # Use custom in_region function feature_eu_compliance: default: false constraint: 'in_region(user_region, ["DE", "FR", "IT", "ES"])' ``` -------------------------------- ### Set Feature Flag from Environment Variable Source: https://github.com/bestit/flagception-bundle/blob/master/docs/environment.md Configure a feature flag to check the status of an environment variable named 'FEATURE_NAME_FROM_ENV'. This is useful for dynamically controlling features based on deployment settings. ```yaml flagception: features: # This feature check the env var 'FEATURE_NAME_FROM_ENV' # setenv('FEATURE_NAME_FROM_ENV=false') feature_123: env: FEATURE_NAME_FROM_ENV ``` -------------------------------- ### Check Multiple Features in YAML Routing Source: https://github.com/bestit/flagception-bundle/blob/master/docs/route.md Configure multiple feature flags for a route by providing an array to the '_feature' key in YAML routing. This applies to Symfony 3.4/4.0 and later. ```yaml # app/config/routing.yml blog_list: path: /blog/{page} defaults: { _controller: AppBundle:Blog:list, _feature: ['feature_456', 'feature_789'] } # Symfony 3.4 / 4.0 blog_list: path: /blog/{page} controller: AppBundle:Blog:list defaults: { _feature: ['feature_456', 'feature_789'] } blog_show: ``` -------------------------------- ### Register Flagception Bundle in Symfony Source: https://context7.com/bestit/flagception-bundle/llms.txt Register the FlagceptionBundle in your Symfony application's configuration file. This is required for Symfony 4+. ```php // config/bundles.php (Symfony 4+) return [ // ... Flagception\Bundle\FlagceptionBundle\FlagceptionBundle::class => ['all' => true], ]; ``` -------------------------------- ### Check Features in Twig Templates Source: https://context7.com/bestit/flagception-bundle/llms.txt Use the `feature()` function or `is active feature` test in Twig templates for conditional rendering. Context data can be passed for constraint evaluation. ```twig {# Simple feature check #} {% if feature('feature_dark_mode') %} {% endif %} {# Alternative syntax using test #} {% if 'feature_new_header' is active feature %} {% include 'components/new_header.html.twig' %} {% else %} {% include 'components/legacy_header.html.twig' %} {% endif %} {# Feature check with context data for constraint evaluation #} {% if feature('feature_beta_access', {'user_id': app.user.id, 'user_roles': app.user.roles}) %}
You have access to beta features!
{% endif %} {# Multiple feature checks #} {% if feature('feature_recommendations') and feature('feature_ai_powered') %} {% include 'components/ai_recommendations.html.twig' %} {% endif %} ``` -------------------------------- ### Check Feature in Service Source: https://github.com/bestit/flagception-bundle/blob/master/docs/usage.md Inject `FeatureManagerInterface` into your services to programmatically check if a feature is active using the `isActive()` method. ```php # FooService.php class FooService { /** * @var FeatureManagerInterface */ private $manager; /** * @param FeatureManagerInterface $manager * Service id: flagception.manager.feature_manager */ public function __construct(FeatureManagerInterface $manager) { $this->manager = $manager; } public function do() { // ... if ($this->manager->isActive('feature_123')) { // ... } // ... } } ``` -------------------------------- ### Applying Feature Annotations to Controller Actions Source: https://github.com/bestit/flagception-bundle/blob/master/docs/annotation.md Use the @Feature annotation to specify which feature flags must be active for a controller class or action to be accessible. A NotFoundHttpException is thrown if a requested action or class has an inactive feature flag. This approach is not recommended due to performance concerns; consider using route attributes instead. ```php # FooController.php use Flagception\Bundle\FlagceptionBundle\Annotations\Feature; /** * @Feature("feature_123") */ class FooController { /** * @Feature("feature_789") */ public function barAction() { } public function fooAction() { } } ``` -------------------------------- ### Feature Flag Check with Context Data in Twig Source: https://github.com/bestit/flagception-bundle/blob/master/docs/twig.md Check feature flag states while providing context data, such as user roles, to influence the flag's evaluation. Refer to the constraint documentation for details on available context options. ```twig {% if feature('feature_123', {'role': 'ROLE_ADMIN'}) %} {# ... #} {% endif %} ``` -------------------------------- ### Define Feature in Route Annotation Source: https://github.com/bestit/flagception-bundle/blob/master/docs/route.md Use the @Route annotation in your controllers to specify a feature flag. A NotFoundHttpException is thrown if the feature is inactive. ```php // src/AppBundle/Controller/BlogController.php // src/Controller/BlogController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller { /** * @Route("/blog/{page}", defaults={"_feature": "feature_123"}) */ public function listAction($page) { // ... } /** * @Route("/blog/{slug}") */ public function showAction($slug) { // ... } } ``` -------------------------------- ### Check Feature Status in Twig Source: https://github.com/bestit/flagception-bundle/blob/master/README.md Use the `feature` Twig function to conditionally render content based on the status of a feature toggle. ```twig {% if feature('feature_123') %} {# Execute if feature is active ... #} {% endif %} ``` -------------------------------- ### Define Feature in YAML Routing Source: https://github.com/bestit/flagception-bundle/blob/master/docs/route.md Specify a feature flag using the '_feature' key in your YAML routing configuration. This applies to Symfony 3.4/4.0 and later versions. ```yaml # app/config/routing.yml blog_list: path: /blog/{page} defaults: { _controller: AppBundle:Blog:list, _feature: 'feature_789' } # Symfony 3.4 / 4.0 blog_list: path: /blog/{page} controller: AppBundle:Blog:list defaults: { _feature: 'feature_789' } blog_show: ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.