BaseExtensibleParser.php in Markdown 8.2
File
src/Plugin/Markdown/BaseExtensibleParser.php
View source
<?php
namespace Drupal\markdown\Plugin\Markdown;
use Drupal\Component\Plugin\LazyPluginCollection;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\markdown\PluginManager\ExtensionCollection;
use Drupal\markdown\Util\SortArray;
abstract class BaseExtensibleParser extends BaseParser implements ExtensibleParserInterface {
protected $extensions = [];
protected $extensionCollection;
public function defaultConfiguration() {
return [
'extensions' => [],
] + parent::defaultConfiguration();
}
public function extension($extensionId) {
return $this
->extensions()
->get($extensionId);
}
public function extensionInterfaces() {
return isset($this->pluginDefinition['extensionInterfaces']) ? $this->pluginDefinition['extensionInterfaces'] : [];
}
public function extensions() {
if (!isset($this->extensionCollection)) {
$this->extensionCollection = new ExtensionCollection($this
->getContainer()
->get('plugin.manager.markdown.extension'), $this);
}
return $this->extensionCollection;
}
public function getBundledExtensionIds() {
return isset($this->pluginDefinition['bundledExtensions']) ? $this->pluginDefinition['bundledExtensions'] : [];
}
public function getConfiguration() {
$configuration = parent::getConfiguration();
$extensions = [];
$extensionCollection = $this
->extensions();
foreach ($extensionCollection as $extensionId => $extension) {
if ($overrides = $extension
->getConfigurationOverrides()) {
$extensionConfiguration = $extension
->getSortedConfiguration();
unset($extensionConfiguration['dependencies']);
$extensions[] = $extensionConfiguration;
}
}
uasort($extensions, function ($a, $b) {
return SortArray::sortByKeyString($a, $b, 'id');
});
$configuration['extensions'] = array_values($extensions);
return $configuration;
}
protected function getConfigurationSortOrder() {
return [
'extensions' => 100,
] + parent::getConfigurationSortOrder();
}
protected function isExtensionRequired(ExtensionInterface $extension) {
if ($requiredBy = $extension
->requiredBy()) {
foreach ($requiredBy as $dependent) {
if ($this
->extension($dependent)
->isEnabled()) {
return TRUE;
}
}
}
return FALSE;
}
public function getPluginCollections() {
return [
'extensions' => $this
->extensions(),
];
}
protected function getPluginDependencies(PluginInspectionInterface $instance) {
if (!$instance instanceof ExtensionInterface || ($instance
->isEnabled() || $this
->isExtensionRequired($instance))) {
return parent::getPluginDependencies($instance);
}
return [];
}
public function setExtensionConfig($extensionId, array $configuration) {
$this->extensions[$extensionId] = $configuration;
if (isset($this->extensionCollection)) {
$this->extensionCollection
->setInstanceConfiguration($extensionId, $configuration);
}
return $this;
}
}