View source
<?php
namespace Drupal\imce;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\imce\Entity\ImceProfile;
class ImcePluginManager extends DefaultPluginManager {
protected $hooks;
public $instances;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/ImcePlugin', $namespaces, $module_handler, 'Drupal\\imce\\ImcePluginInterface', 'Drupal\\imce\\Annotation\\ImcePlugin');
$this
->alterInfo('imce_plugin_info');
$this
->setCacheBackend($cache_backend, 'imce_plugins');
}
protected function findDefinitions() {
$definitions = parent::findDefinitions();
uasort($definitions, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
return $definitions;
}
public function getInstance(array $options) {
if (isset($options['id']) && ($id = $options['id'])) {
return isset($this->instances[$id]) ? $this->instances[$id] : $this
->createInstance($id);
}
}
public function getInstances() {
if (!isset($this->instances)) {
$this->instances = [];
foreach ($this
->getDefinitions() as $id => $def) {
$this->instances[$id] = $this
->createInstance($id);
}
}
return $this->instances;
}
public function getHooks() {
if (!isset($this->hooks)) {
$this->hooks = get_class_methods('Drupal\\imce\\ImcePluginInterface');
}
return $this->hooks;
}
public function invokeAll($hook, &$a = NULL, $b = NULL, $c = NULL) {
$ret = [];
if (in_array($hook, $this
->getHooks())) {
foreach ($this
->getInstances() as $plugin => $instance) {
$ret[$plugin] = $instance
->{$hook}($a, $b, $c);
}
}
return $ret;
}
public function permissionInfo() {
return call_user_func_array('array_merge', array_filter($this
->invokeAll('permissionInfo')));
}
public function alterProfileForm(array &$form, FormStateInterface $form_state, ImceProfile $imce_profile) {
return $this
->invokeAll('alterProfileForm', $form, $form_state, $imce_profile);
}
public function validateProfileForm(array &$form, FormStateInterface $form_state, ImceProfile $imce_profile) {
return $this
->invokeAll('validateProfileForm', $form, $form_state, $imce_profile);
}
public function processUserConf(array &$conf, AccountProxyInterface $user) {
return $this
->invokeAll('processUserConf', $conf, $user);
}
public function buildPage(array &$page, ImceFM $fm) {
return $this
->invokeAll('buildPage', $page, $fm);
}
public function handleOperation($op, ImceFM $fm) {
$plugin = $method = FALSE;
foreach ($this
->getDefinitions() as $p => $def) {
if (!empty($def['operations'][$op])) {
$plugin = $p;
$method = $def['operations'][$op];
}
}
if ($method && ($instance = $this
->getInstance([
'id' => $plugin,
]))) {
return $instance
->{$method}($fm);
}
return FALSE;
}
}