View source
<?php
namespace Drupal\markdown\Plugin\Markdown;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Utility\DiffArray;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Url;
use Drupal\markdown\Annotation\InstallableLibrary;
use Drupal\markdown\BcSupport\ObjectWithPluginCollectionInterface;
use Drupal\markdown\BcSupport\PluginDependencyTrait;
use Drupal\markdown\Exception\MissingVersionException;
use Drupal\markdown\Traits\MoreInfoTrait;
use Drupal\markdown\Util\FilterAwareInterface;
use Drupal\markdown\Util\FilterFormatAwareInterface;
use Drupal\markdown\Util\ParserAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class InstallablePluginBase extends AnnotatedPluginBase implements InstallablePluginInterface {
use MoreInfoTrait;
use PluginDependencyTrait {
getPluginDependencies as getPluginDependenciesTrait;
}
protected $config;
public function buildLibrary(InstallableLibrary $library = NULL) {
}
public function buildStatus($all = FALSE) {
$build = [
'#theme_wrappers' => [
'container__installable_libraries',
],
'#attributes' => [
'class' => [
'installable-libraries',
],
],
];
$libraries = $all ? $this->pluginDefinition->libraries : [
$this
->getInstalledLibrary() ?: $this
->getPreferredLibrary(),
];
foreach ($libraries as $library) {
$build[] = [
'#theme' => 'installable_library',
'#plugin' => $this,
'#library' => $library,
];
}
return $build;
}
public function defaultConfiguration() {
$configuration['id'] = $this
->getPluginId();
$configuration['weight'] = $this->pluginDefinition->weight;
if ($this instanceof EnabledPluginInterface) {
$configuration['enabled'] = $this
->enabledByDefault();
}
if ($this instanceof SettingsInterface) {
$pluginDefinition = $this
->getPluginDefinition();
$settings = isset($pluginDefinition['settings']) ? $pluginDefinition['settings'] : [];
$configuration['settings'] = NestedArray::mergeDeep($settings, static::defaultSettings($pluginDefinition));
}
return $configuration;
}
public function calculateDependencies() {
$dependencies = [];
if ($this instanceof ObjectWithPluginCollectionInterface) {
foreach ($this
->getPluginCollections() as $pluginCollection) {
foreach ($pluginCollection as $instance) {
$dependencies = array_map('array_unique', NestedArray::mergeDeep($dependencies, $this
->getPluginDependencies($instance)));
}
}
}
return $dependencies;
}
public function config() {
return $this->config;
}
public function getAvailableInstalls() {
$availableInstalls = [];
foreach ($this->pluginDefinition->libraries as $library) {
$definition = clone $this->pluginDefinition;
$definition
->merge($library);
$definition->libraries = [];
$availableInstall = new static($this->configuration, $this->pluginId, $definition);
if ($this instanceof ParserAwareInterface && $availableInstall instanceof ParserAwareInterface) {
$availableInstall
->setParser($this
->getParser());
}
if ($this instanceof FilterAwareInterface && $availableInstall instanceof FilterAwareInterface) {
$availableInstall
->setFilter($this
->getFilter());
}
if ($this instanceof FilterFormatAwareInterface && $availableInstall instanceof FilterFormatAwareInterface) {
$availableInstall
->setFilterFormat($this
->getFilterFormat());
}
$availableInstalls[] = $availableInstall;
}
return $availableInstalls;
}
protected function getConfigurationName() {
return sprintf('installable.plugin.%s_%s', $this
->getProvider(), $this
->getPluginId());
}
public function getConfiguration() {
$configuration = parent::getConfiguration();
$configuration['dependencies'] = $this
->getPluginDependencies($this);
if ($this instanceof EnabledPluginInterface) {
$configuration['enabled'] = $this
->isEnabled();
}
if ($this instanceof SettingsInterface) {
$configuration['settings'] = $this
->getSettingOverrides();
}
return $configuration;
}
protected function getConfigurationSortOrder() {
$order = [
'dependencies' => -100,
'id' => -50,
'weight' => -30,
];
if ($this instanceof EnabledPluginInterface) {
$order['enabled'] = -20;
}
if ($this instanceof SettingsInterface) {
$order['settings'] = -10;
}
return $order;
}
public function getContainer() {
return $this->container instanceof ContainerInterface ? $this->container : \Drupal::getContainer();
}
public function getDeprecated() {
return $this->pluginDefinition->deprecated;
}
public function getExperimental() {
return $this->pluginDefinition->experimental;
}
public function getInstalledId() {
return $this->pluginDefinition
->getInstalledId();
}
public function getInstalledLibrary() {
return $this->pluginDefinition
->getInstalledLibrary();
}
public function getObject($args = NULL, $_ = NULL) {
if ($class = $this
->getObjectClass()) {
$ref = new \ReflectionClass($class);
return $ref
->newInstanceArgs(func_get_args());
}
}
public function getObjectClass() {
return $this->pluginDefinition->object;
}
public function getLabel($version = TRUE) {
$label = $this->pluginDefinition->label ?: $this->pluginDefinition
->getId();
try {
if ($version && ($version = $this
->getVersion())) {
$label .= " ({$version})";
}
} catch (MissingVersionException $exception) {
}
return $label;
}
public function getLink($label = NULL, array $options = [], $fallbackToLabel = TRUE) {
return $this->pluginDefinition
->getLink($label, $options, $fallbackToLabel);
}
protected function getPluginDependencies(PluginInspectionInterface $instance) {
return array_map('array_unique', $this
->getPluginDependenciesTrait($instance));
}
public function getPreferredLibrary() {
return $this->pluginDefinition
->getPreferredLibrary();
}
public function getSortedConfiguration() {
$configuration = $this
->getConfiguration();
$weights = $this
->getConfigurationSortOrder() + array_fill_keys(array_keys($configuration), 0);
uksort($configuration, function ($a, $b) use ($weights) {
$a = isset($weights[$a]) ? (int) $weights[$a] : 0;
$b = isset($weights[$b]) ? (int) $weights[$b] : 0;
if ($a === $b) {
return 0;
}
return $a < $b ? -1 : 1;
});
return $configuration;
}
public function getUrl(array $options = []) {
if ($url = $this->pluginDefinition->url) {
if (UrlHelper::isExternal($url)) {
$options['attributes']['target'] = '_blank';
return Url::fromUri($url)
->setOptions($options);
}
return Url::fromUserInput($url)
->setOptions($options);
}
}
public function getVersion() {
if (!$this->pluginDefinition->version) {
throw new MissingVersionException(sprintf('The library "%s" did not not specify a version. If the plugin has no version, it must be explicitly set to "0.0.0".', $this
->getPluginId()));
}
return $this->pluginDefinition->version;
}
public function getVersionConstraint() {
if ($versionRequirement = current($this->pluginDefinition
->getRequirementsByConstraint('Version'))) {
return $versionRequirement->constraints['Version'];
}
}
public function hasMultipleLibraries() {
return count($this->pluginDefinition->libraries) > 1;
}
public function isInstalled() {
return $this->pluginDefinition
->isInstalled();
}
public function isPreferred() {
return $this->pluginDefinition->preferred;
}
public function isPreferredLibraryInstalled() {
return $this->pluginDefinition
->isPreferredLibraryInstalled();
}
public function setConfiguration(array $configuration) {
$configuration = array_filter($configuration, function ($value) {
return $value !== NULL;
});
$defaultConfiguration = $this
->defaultConfiguration();
$this->config = static::createConfig($this
->getConfigurationName(), $defaultConfiguration, TRUE, $this
->getContainer());
if ($overrides = $this
->getConfigurationOverrides($configuration + $defaultConfiguration)) {
$this->config
->setModuleOverride($overrides);
}
parent::setConfiguration($this->config
->get());
}
protected static function createConfig($name, array $data = [], $immutable = TRUE, ContainerInterface $container = NULL) {
$class = $immutable ? ImmutableConfig::class : Config::class;
if (!$container) {
$container = \Drupal::getContainer();
}
$config = new $class($name, $container
->get('config.storage'), $container
->get('event_dispatcher'), $container
->get('config.typed'));
$config
->initWithData($data);
return $config;
}
public function showInUi() {
return $this->pluginDefinition->ui;
}
}