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')]); } } }
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'); }
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']); } } }
public function strategy($name, $voter) { $strategy = Strategy::create($name, $voter); $this->registry->registerStrategy($strategy); return $strategy; }