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; }