Example #1
0
 public function register(Feature $feature, $strategyName)
 {
     if (!$this->isStrategyRegistered($strategyName)) {
         throw new \InvalidArgumentException(sprintf("Strategy '%s' is not registered.", $strategyName));
     }
     $this->features[] = $feature;
     $this->featureStrategyMapping[$feature->getName()] = $strategyName;
 }
Example #2
0
 public function save(Feature $feature, $userKey, $variant)
 {
     $table = $this->tableName;
     $stmt = $this->pdo->prepare("INSERT INTO {$table} (id, user, feature, variant) VALUES (NULL, :user, :feature, :variant)");
     $stmt->bindParam('user', $userKey);
     $stmt->bindParam('feature', $feature->getName());
     $stmt->bindParam('variant', $variant);
     return $stmt->execute();
 }
Example #3
0
 public function decide(Strategy $strategy, Feature $feature, array $context = array())
 {
     $voter = $strategy->getVoter();
     $context = !empty($context) ? $context : $this->getContext();
     $context['_user'] = $this->get('_user');
     $options = array_merge($strategy->getOptions(), $feature->getOptions());
     if (is_callable($voter)) {
         return $this->callVoter($voter, $context, $options);
     } else {
         if (is_string($voter)) {
             $contextObj = $this->arrayToObject($context);
             $optionsObj = $this->arrayToObject($options);
             return $this->expressionLanguage->evaluate($voter, ['context' => $contextObj, 'options' => $optionsObj, 'user' => $this->get('_user')]);
         }
     }
 }
Example #4
0
 public function setUp()
 {
     $this->registry = new Registry();
     $this->feature = Feature::create('top_secret', array('enabled', 'disabled'), array('whitelist' => array('127.0.0.1', '10.0.0.5')));
     $this->strategy = Strategy::create('ip_whitelist', function ($context, $options) {
         return in_array($context['client_ip'], $options['whitelist']);
     });
     // $this->strategy = Strategy::create('ip_whitelist', 'context.client_ip in options.whitelist');
     $this->registry->registerStrategy($this->strategy);
     $this->registry->register($this->feature, 'ip_whitelist');
 }
Example #5
0
 private function loadFeatures($registry)
 {
     if (!isset($this->data['features'])) {
         return;
     }
     foreach ($this->data['features'] as $name => $config) {
         $strategyName = $config['strategy'];
         if (!$strategyName) {
             throw new \RuntimeException(sprintf("A strategy must be specified for feature '%s'.", $name));
         }
         $variants = isset($config['variants']) ? $config['variants'] : ['enabled', 'disabled'];
         $feature = Feature::create($name, $variants);
         if (isset($config['options'])) {
             $feature->setOptions($config['options']);
         }
         $registry->register($feature, $strategyName);
     }
 }
Example #6
0
 public function feature($name, $strategyName, array $variants = array())
 {
     $feature = Feature::create($name, $variants);
     $this->registry->register($feature, $strategyName);
     return $feature;
 }