/**
* Constructor
*/
public function __construct(\Scalr\Service\Aws $aws)
{
$this->plugins = new \ArrayObject(array());
$this->aws = $aws;
$cfEnabled = $this->aws->getContainer()->config('scalr.aws.plugins.enabled');
if (!is_array($cfEnabled)) {
$cfEnabled = (array) $cfEnabled;
}
foreach (EventType::getAllowedValues() as $eventName) {
$this->subscriptions[$eventName] = array();
}
$iterator = new \RegexIterator(new \DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . 'Handlers'), '/^(.+)Plugin\\.php$/');
foreach ($iterator as $di) {
/* @var $di \DirectoryIterator */
$plugin = substr($di->getFilename(), 0, -4);
$class = __NAMESPACE__ . '\\Handlers\\' . $plugin;
$p = new $class();
if (!$p instanceof PluginInterface) {
trigger_error(sprintf('AWS client plugin "%s" must implement PluginInterface.', $class), E_USER_WARNING);
continue;
}
$p->setAws($this->aws);
$p->setContainer($this->aws->getContainer());
$subscribed = $p->getSubscribedEvents();
if (empty($subscribed)) {
continue;
}
$lcPlugin = strtolower(substr($plugin, 0, -6));
foreach ($subscribed as $eventName) {
if (isset($this->subscriptions[$eventName])) {
$this->subscriptions[$eventName][$lcPlugin] = $p->priority;
} else {
throw new Exception\PluginException(sprintf('AWS client plugin "%s" is subscribed to unknown event "%s".', $class, $eventName));
}
}
$this->plugins[$lcPlugin] = $p;
$this->enabled[$lcPlugin] = in_array($lcPlugin, $cfEnabled) ? true : null;
}
//Sorts subscriptions by plugin priority
foreach ($this->subscriptions as $eventName => $v) {
if (!empty($v)) {
arsort($this->subscriptions[$eventName]);
}
}
}