コード例 #1
0
ファイル: Decider.php プロジェクト: leoht/erlenmeyer
 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')]);
         }
     }
 }
コード例 #2
0
ファイル: RegistryTest.php プロジェクト: leoht/erlenmeyer
 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');
 }
コード例 #3
0
ファイル: YamlLoader.php プロジェクト: leoht/erlenmeyer
 private function loadStrategies($registry)
 {
     if (!isset($this->data['strategies'])) {
         return;
     }
     foreach ($this->data['strategies'] as $name => $config) {
         // Try to find an already registered strategy
         $strategy = $registry->getStrategy($name);
         if (!$strategy) {
             $voter = $config['voter'];
             if (!is_callable($voter) && !is_string($voter)) {
                 throw new \RuntimeException(sprintf("Voter for strategy '%s' must be either a callable or a valid expression string.", $name));
             }
             $strategy = Strategy::create($name, $voter);
             $registry->registerStrategy($strategy);
         }
         if (isset($config['options'])) {
             $strategy->setOptions($config['options']);
         }
     }
 }
コード例 #4
0
ファイル: Erlenmeyer.php プロジェクト: leoht/erlenmeyer
 public function strategy($name, $voter)
 {
     $strategy = Strategy::create($name, $voter);
     $this->registry->registerStrategy($strategy);
     return $strategy;
 }