View source
<?php
namespace Drupal\markdown\PluginManager;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\markdown\Annotation\MarkdownParser;
use Drupal\markdown\Plugin\Markdown\ExtensibleParserInterface;
use Drupal\markdown\Plugin\Markdown\ExtensionInterface;
use Drupal\markdown\Plugin\Markdown\ParserInterface;
use Drupal\markdown\Traits\EnableAwarePluginManagerTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParserManager extends InstallablePluginManager implements ParserManagerInterface {
use EnableAwarePluginManagerTrait;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ConfigFactoryInterface $configFactory, LoggerInterface $logger, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Markdown', $namespaces, $configFactory, $logger, $module_handler, ParserInterface::class, MarkdownParser::class);
$this
->setCacheBackend($cache_backend, 'markdown_parser_info');
$this
->alterInfo($this->cacheKey);
}
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'));
$instance
->setContainer($container);
return $instance;
}
public function createInstance($plugin_id, array $configuration = []) {
$parser = parent::createInstance($plugin_id, $configuration);
if ($parser
->getPluginId() === $this
->getFallbackPluginId()) {
return $parser;
}
$parser
->addCacheTags([
"markdown.parser.{$plugin_id}",
]);
return $parser;
}
public function getDefaultParser(array $configuration = []) {
$settings = $this->configFactory
->get('markdown.settings');
if (!($defaultParser = $settings
->get('default_parser'))) {
$defaultParser = current(array_keys($this
->installed()));
$this->logger
->warning($this
->t('No default markdown parser set, using first available installed parser "@default_parser".', [
'@default_parser' => $defaultParser,
]));
}
return $this
->createInstance($defaultParser, $configuration);
}
public function getFallbackPluginId($plugin_id = NULL, array $configuration = []) {
return '_missing_parser';
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if (!$definition instanceof MarkdownParser || !$definition
->isInstalled() || !($class = $definition
->getClass())) {
return;
}
if (is_subclass_of($class, ExtensibleParserInterface::class)) {
if (!$definition->extensionInterfaces) {
throw new InvalidPluginDefinitionException($plugin_id, sprintf('Markdown parser "%s" implements %s but is missing "extensionInterfaces" in the definition.', $plugin_id, ExtensibleParserInterface::class));
}
foreach (array_map('\\Drupal\\markdown\\PluginManager\\InstallablePluginManager::normalizeClassName', $definition->extensionInterfaces) as $interface) {
if ($interface === ExtensionInterface::class) {
throw new InvalidPluginDefinitionException($plugin_id, sprintf('Markdown parser "%s" cannot specify %s as the extension interface. It must create its own unique interface that extend from it.', $plugin_id, ExtensionInterface::class));
}
if (!interface_exists($interface)) {
throw new InvalidPluginDefinitionException($plugin_id, sprintf('Markdown parser "%s" indicates that it supports the extension interface "%s", but this interface does not exist.', $plugin_id, $interface));
}
if (!is_subclass_of($interface, ExtensionInterface::class)) {
throw new InvalidPluginDefinitionException($plugin_id, sprintf('Markdown parser "%s" indicates that it supports the extension interface "%s", but this interface does not extend %s.', $plugin_id, $interface, ExtensionInterface::class));
}
}
}
}
protected function providerExists($provider) {
if ($provider === 'markdown') {
return TRUE;
}
return parent::providerExists($provider);
}
}