* * @deprecated Use oat\taoItems\model\preview\ItemPreviewerRegistryServiceInterface instead */ class ItemPreviewerService extends ConfigurableService { const SERVICE_ID = 'taoItems/ItemPreviewer'; private const REGISTRY_ENTRY_KEY = 'taoItems/previewer/factory'; private const PREVIEWERS_KEY = 'previewers'; private const PLUGINS_KEY = 'plugins'; private $registry; /** * Sets the registry that contains the list of adapters * @param AbstractRegistry $registry */ public function setRegistry(AbstractRegistry $registry) { $this->registry = $registry; } /** * Gets the registry that contains the list of adapters * @return AbstractRegistry */ public function getRegistry() { if (!$this->registry) { $this->registry = ClientLibConfigRegistry::getRegistry(); } return $this->registry; } /** * Gets the list of adapters * @return array */ public function getAdapters() { $registry = $this->getRegistry(); $config = []; if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { $config = $registry->get(self::REGISTRY_ENTRY_KEY); } return $config[self::PREVIEWERS_KEY] ?? []; } /** * Gets the list of plugins * @return array */ public function getPlugins() { $registry = $this->getRegistry(); $config = []; if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { $config = $registry->get(self::REGISTRY_ENTRY_KEY); } return $config[self::PLUGINS_KEY] ?? []; } /** * Registers a previewer adapter * @param DynamicModule $module the adapter to register * @return boolean true if registered */ public function registerAdapter(DynamicModule $module): bool { if (null === $module || empty($module->getModule())) { return false; } $registry = $this->getRegistry(); $config = []; if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { $config = $registry->get(self::REGISTRY_ENTRY_KEY); } $config[self::PREVIEWERS_KEY][$module->getModule()] = $module->toArray(); $registry->set(self::REGISTRY_ENTRY_KEY, $config); return true; } /** * Unregisters a previewer adapter * @param string $moduleId * @return boolean true if unregistered */ public function unregisterAdapter($moduleId): bool { $registry = $this->getRegistry(); $config = []; if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { $config = $registry->get(self::REGISTRY_ENTRY_KEY); } if (isset($config[self::PREVIEWERS_KEY]) && isset($config[self::PREVIEWERS_KEY][$moduleId])) { unset($config[self::PREVIEWERS_KEY][$moduleId]); $registry->set(self::REGISTRY_ENTRY_KEY, $config); return true; } return false; } /** * Registers a previewer plugin * @param DynamicModule $module the plugin to register * @return boolean true if registered */ public function registerPlugin(DynamicModule $module): bool { if (null === $module || empty($module->getModule())) { return false; } $this->unregisterPlugin($module->getModule()); $registry = $this->getRegistry(); $config = []; if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { $config = $registry->get(self::REGISTRY_ENTRY_KEY); } $config[self::PLUGINS_KEY][] = $module->toArray(); $registry->set(self::REGISTRY_ENTRY_KEY, $config); return true; } /** * Unregisters a previewer plugin * @param string $moduleId * @return boolean true if unregistered */ public function unregisterPlugin($moduleId): bool { $registry = $this->getRegistry(); $config = []; if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { $config = $registry->get(self::REGISTRY_ENTRY_KEY); } $result = false; if (isset($config[self::PLUGINS_KEY])) { $config[self::PLUGINS_KEY] = array_filter( $config[self::PLUGINS_KEY], static function (array $plugin) use ($moduleId, &$result): bool { $result = $plugin['module'] == $moduleId; return !$result; } ); $registry->set(self::REGISTRY_ENTRY_KEY, $config); } return $result; } }