View source
<?php
namespace Drupal\markdown\PluginManager;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\filter\Entity\FilterFormat;
use Drupal\filter\FilterPluginManager;
use Drupal\markdown\Annotation\InstallablePlugin;
use Drupal\markdown\Annotation\MarkdownAllowedHtml;
use Drupal\markdown\Annotation\InstallableRequirement;
use Drupal\markdown\Plugin\Markdown\AllowedHtmlInterface;
use Drupal\markdown\Plugin\Markdown\ExtensibleParserInterface;
use Drupal\markdown\Plugin\Markdown\ExtensionInterface;
use Drupal\markdown\Plugin\Markdown\ParserInterface;
use Drupal\markdown\Util\FilterAwareInterface;
use Drupal\markdown\Util\FilterFormatAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AllowedHtmlManager extends InstallablePluginManager {
protected $extensionManager;
protected $filterManager;
protected $parserManager;
protected $themeHandler;
protected $themeManager;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ConfigFactoryInterface $configFactory, LoggerInterface $logger, ModuleHandlerInterface $module_handler, FilterPluginManager $filterManager, ThemeHandlerInterface $themeHandler, ThemeManagerInterface $themeManager, ParserManagerInterface $parserManager, ExtensionManagerInterface $extensionManager) {
$namespaces = iterator_to_array($namespaces);
foreach ($themeHandler
->listInfo() as $extension) {
$namespaces['Drupal\\' . $extension
->getName()] = [
DRUPAL_ROOT . '/' . $extension
->getPath() . '/src',
];
}
parent::__construct('Plugin/Markdown', new \ArrayObject($namespaces), $configFactory, $logger, $module_handler, AllowedHtmlInterface::class, MarkdownAllowedHtml::class);
$this
->setCacheBackend($cache_backend, 'markdown_allowed_html_info');
$this
->alterInfo($this->cacheKey);
$this->filterManager = $filterManager;
$this->themeHandler = $themeHandler;
$this->themeManager = $themeManager;
$this->parserManager = $parserManager;
$this->extensionManager = $extensionManager;
}
public static function create(ContainerInterface $container = NULL) {
if (!$container) {
$container = \Drupal::getContainer();
}
$instance = new static($container
->get('container.namespaces'), $container
->get('cache.discovery'), $container
->get('config.factory'), $container
->get('logger.channel.markdown'), $container
->get('module_handler'), $container
->get('plugin.manager.filter'), $container
->get('theme_handler'), $container
->get('theme.manager'), $container
->get('plugin.manager.markdown.parser'), $container
->get('plugin.manager.markdown.extension'));
$instance
->setContainer($container);
return $instance;
}
protected function alterDefinitions(&$definitions, $runtime = FALSE) {
foreach ($definitions as $definition) {
if ($definition instanceof InstallablePlugin) {
$this
->alterDefinition($definition, $runtime);
}
}
if ($hook = $this->alterHook) {
if ($runtime) {
$hook = "_runtime";
}
$this->moduleHandler
->alter($hook, $definitions);
$this->themeManager
->alter($hook, $definitions);
}
}
public function appliesTo(ParserInterface $parser, ActiveTheme $activeTheme = NULL, array $definitions = NULL) {
$instances = [];
foreach ($this
->getGroupedDefinitions($definitions) as $group => $groupDefinitions) {
if ($activeTheme) {
$groupDefinitions = array_intersect_key($groupDefinitions, array_filter($parser
->getAllowedHtmlPlugins()));
}
switch ($group) {
case 'extension':
$groupDefinitions = $this
->getExtensionDefinitions($parser, $groupDefinitions, $activeTheme);
break;
case 'filter':
$filter = $parser instanceof FilterAwareInterface ? $parser
->getFilter() : NULL;
$filterFormat = $filter instanceof FilterFormatAwareInterface ? $filter
->getFilterFormat() : NULL;
$groupDefinitions = $this
->getFilterDefinitions($filterFormat, $groupDefinitions, $activeTheme);
break;
case 'parser':
$groupDefinitions = $this
->getParserDefinitions($parser, $groupDefinitions, $activeTheme);
break;
case 'theme':
if ($activeTheme) {
$groupDefinitions = $this
->getThemeDefinitions($groupDefinitions, $activeTheme);
}
break;
}
foreach (array_keys($groupDefinitions) as $plugin_id) {
try {
$instances[$plugin_id] = $this
->createInstance($plugin_id, [
'activeTheme' => $activeTheme,
'parser' => $parser,
]);
} catch (PluginException $e) {
}
}
}
return $instances;
}
public function getFallbackPluginId($plugin_id = NULL, array $configuration = []) {
return $plugin_id;
}
public function getExtensionDefinitions(ParserInterface $parser, array $definitions = NULL, ActiveTheme $activeTheme = NULL) {
if (!$parser instanceof ExtensibleParserInterface) {
return [];
}
$definitions = isset($definitions) ? $definitions : $this
->getType('extension');
foreach ($definitions as $plugin_id => $definition) {
$class = static::normalizeClassName($definition
->getClass());
foreach ($parser
->extensionInterfaces() as $interface) {
if (is_subclass_of($class, static::normalizeClassName($interface))) {
continue 2;
}
}
unset($definitions[$plugin_id]);
}
return $definitions;
}
public function getFilterDefinitions(FilterFormat $filterFormat = NULL, array $definitions = NULL, ActiveTheme $activeTheme = NULL) {
if (!$filterFormat) {
return [];
}
$definitions = isset($definitions) ? $definitions : $this
->getType('filter', $definitions);
$filters = $filterFormat
->filters();
foreach ($definitions as $plugin_id => $definition) {
if (!$definition
->getRequirementsByType('filter', $plugin_id) || !$filters
->has($plugin_id) || $activeTheme && !$filters
->get($plugin_id)->status) {
unset($definitions[$plugin_id]);
}
}
return $definitions;
}
public function getGroupedDefinitions(array $definitions = NULL, $label_key = 'label') {
$definitions = $this
->getSortedDefinitions(isset($definitions) ? $definitions : $this
->installedDefinitions(), $label_key);
$grouped_definitions = [];
foreach ($definitions as $id => $definition) {
$grouped_definitions[(string) $definition['type']][$id] = $definition;
}
return $grouped_definitions;
}
public function getParserDefinitions(ParserInterface $parser, array $definitions = NULL, ActiveTheme $activeTheme = NULL) {
$definitions = isset($definitions) ? $definitions : $this
->getType('parser');
$parserClass = static::normalizeClassName(get_class($parser));
foreach ($definitions as $plugin_id => $definition) {
$class = static::normalizeClassName($definition
->getClass());
if ($parserClass !== $class && !is_subclass_of($parser, $class)) {
unset($definitions[$plugin_id]);
}
}
return $definitions;
}
public function getSortedDefinitions(array $definitions = NULL, $label_key = 'label') {
$definitions = isset($definitions) ? $definitions : $this
->installedDefinitions();
uasort($definitions, function ($a, $b) use ($label_key) {
if ($a['type'] != $b['type']) {
return strnatcasecmp($a['type'], $b['type']);
}
return strnatcasecmp($a[$label_key], $b[$label_key]);
});
return $definitions;
}
public function getThemeDefinitions(array $definitions = NULL, ActiveTheme $activeTheme = NULL) {
$definitions = isset($definitions) ? $definitions : $this
->getType('theme');
if ($activeTheme) {
$themeAncestry = array_merge(array_keys($activeTheme
->getBaseThemes()), [
$activeTheme
->getName(),
]);
foreach ($definitions as $plugin_id => $definition) {
if (($provider = $definition
->getProvider()) && $this->themeHandler
->themeExists($provider) && !in_array($provider, $themeAncestry, TRUE)) {
unset($definitions[$plugin_id]);
}
}
}
return $definitions;
}
protected function getType($type, array $definitions = NULL) {
$grouped_definitions = $this
->getGroupedDefinitions($definitions);
return isset($grouped_definitions[$type]) ? $grouped_definitions[$type] : [];
}
protected function getProviderName($provider) {
if ($this->moduleHandler
->moduleExists($provider)) {
return $this->moduleHandler
->getName($provider);
}
if ($this->themeHandler
->themeExists($provider)) {
return $this->themeHandler
->getName($provider);
}
}
protected function alterDefinition(InstallablePlugin $definition, $runtime = FALSE) {
if (!$runtime) {
return;
}
switch ($definition->type) {
case 'extension':
if (($extensionRequirement = current($definition
->getRequirementsByType('extension'))) && ($extensionDefinition = $this->extensionManager
->getDefinition($extensionRequirement
->getTypeId()))) {
$definition
->merge($extensionDefinition, [
'ui',
]);
}
break;
case 'filter':
if (($filterRequirement = current($definition
->getRequirementsByType('filter'))) && ($filterDefinition = $this->filterManager
->getDefinition($filterRequirement
->getTypeId()))) {
$definition
->merge($filterDefinition, [
'ui',
]);
if (empty($definition->label) && !empty($filterDefinition['title'])) {
$definition->label = $filterDefinition['title'];
}
}
break;
case 'parser':
if (($parserRequirement = current($definition
->getRequirementsByType('parser'))) && ($parserDefinition = $this->parserManager
->getDefinition($parserRequirement
->getTypeId()))) {
$definition
->merge($parserDefinition, [
'ui',
]);
}
break;
}
if (empty($definition->label)) {
if ($definition->id === $definition->provider) {
$definition['label'] = $this
->getProviderName($definition->provider);
}
elseif ($definition->type !== 'extension') {
$definition['label'] = ucwords(trim(str_replace([
'_',
'-',
], ' ', $definition->id)));
}
}
if (in_array($definition->type, [
'filter',
'module',
'theme',
]) && $definition->id !== $definition->provider) {
$definition['label'] = $this
->getProviderName($definition->provider) . ': ' . $definition['label'];
}
}
public function processDefinition(&$definition, $plugin_id) {
if (!$definition instanceof MarkdownAllowedHtml || !($class = $definition
->getClass()) || $definition->requirementViolations) {
return;
}
if ($filter = $definition->requiresFilter) {
if (!$definition
->getRequirementsByType('filter', $filter)) {
$requirement = InstallableRequirement::create();
$requirement->id = "filter:{$filter}";
$definition->requirements[] = $requirement;
}
unset($definition->requiresFilter);
}
if (!isset($definition->type)) {
$definition->type = 'other';
if ($definition
->getRequirementsByType('filter')) {
$definition->type = 'filter';
}
elseif (is_subclass_of($class, ParserInterface::class)) {
$definition->type = 'parser';
if ($parserDefinition = $this->parserManager
->getDefinitionByClassName($class)) {
$requirement = InstallableRequirement::create();
$requirement->id = 'parser:' . $parserDefinition->id;
$definition->requirements[] = $requirement;
}
}
elseif (is_subclass_of($class, ExtensionInterface::class)) {
$definition->type = 'extension';
if ($extensionDefinition = $this->extensionManager
->getDefinitionByClassName($class)) {
$requirement = InstallableRequirement::create();
$requirement->id = 'extension:' . $extensionDefinition->id;
$definition->requirements[] = $requirement;
}
}
elseif ($this->moduleHandler
->moduleExists($definition->provider)) {
$definition->type = 'module';
}
elseif ($this->themeHandler
->themeExists($plugin_id)) {
$definition->type = 'theme';
}
}
parent::processDefinition($definition, $plugin_id);
}
protected function providerExists($provider) {
return $this->moduleHandler
->moduleExists($provider) || $this->themeHandler
->themeExists($provider);
}
}