View source
<?php
namespace Drupal\breakpoint;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Core\Plugin\Factory\ContainerFactory;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class BreakpointManager extends DefaultPluginManager implements BreakpointManagerInterface {
use StringTranslationTrait;
protected $defaults = [
'label' => '',
'mediaQuery' => '',
'weight' => 0,
'multipliers' => [],
'group' => '',
'class' => 'Drupal\\breakpoint\\Breakpoint',
'id' => '',
];
protected $themeHandler;
protected $breakpointsByGroup;
protected $instances = [];
public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend, TranslationInterface $string_translation) {
$this->factory = new ContainerFactory($this);
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this
->setStringTranslation($string_translation);
$this
->alterInfo('breakpoints');
$this
->setCacheBackend($cache_backend, 'breakpoints', [
'breakpoints',
]);
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('breakpoints', $this->moduleHandler
->getModuleDirectories() + $this->themeHandler
->getThemeDirectories());
$this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
}
return $this->discovery;
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if (empty($definition['group'])) {
$definition['group'] = $definition['provider'];
}
if (!in_array('1x', $definition['multipliers'])) {
$definition['multipliers'][] = '1x';
}
sort($definition['multipliers']);
}
protected function providerExists($provider) {
return $this->moduleHandler
->moduleExists($provider) || $this->themeHandler
->themeExists($provider);
}
public function getBreakpointsByGroup($group) {
if (!isset($this->breakpointsByGroup[$group])) {
if ($cache = $this->cacheBackend
->get($this->cacheKey . ':' . $group)) {
$this->breakpointsByGroup[$group] = $cache->data;
}
else {
$breakpoints = [];
foreach ($this
->getDefinitions() as $plugin_id => $plugin_definition) {
if ($plugin_definition['group'] == $group) {
$breakpoints[$plugin_id] = $plugin_definition;
}
}
uasort($breakpoints, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
$this->cacheBackend
->set($this->cacheKey . ':' . $group, $breakpoints, Cache::PERMANENT, [
'breakpoints',
]);
$this->breakpointsByGroup[$group] = $breakpoints;
}
}
$instances = [];
foreach ($this->breakpointsByGroup[$group] as $plugin_id => $definition) {
if (!isset($this->instances[$plugin_id])) {
$this->instances[$plugin_id] = $this
->createInstance($plugin_id);
}
$instances[$plugin_id] = $this->instances[$plugin_id];
}
return $instances;
}
public function getGroups() {
if ($cache = $this->cacheBackend
->get($this->cacheKey . '::groups')) {
$groups = $cache->data;
}
else {
$groups = [];
foreach ($this
->getDefinitions() as $plugin_definition) {
if (!isset($groups[$plugin_definition['group']])) {
$groups[$plugin_definition['group']] = $plugin_definition['group'];
}
}
$this->cacheBackend
->set($this->cacheKey . '::groups', $groups, Cache::PERMANENT, [
'breakpoints',
]);
}
$group_labels = [];
foreach ($groups as $group) {
$group_labels[$group] = $this
->getGroupLabel($group);
}
asort($group_labels);
return $group_labels;
}
public function getGroupProviders($group) {
$providers = [];
$breakpoints = $this
->getBreakpointsByGroup($group);
foreach ($breakpoints as $breakpoint) {
$provider = $breakpoint
->getProvider();
$extension = FALSE;
if ($this->moduleHandler
->moduleExists($provider)) {
$extension = $this->moduleHandler
->getModule($provider);
}
elseif ($this->themeHandler
->themeExists($provider)) {
$extension = $this->themeHandler
->getTheme($provider);
}
if ($extension) {
$providers[$extension
->getName()] = $extension
->getType();
}
}
return $providers;
}
public function clearCachedDefinitions() {
parent::clearCachedDefinitions();
$this->breakpointsByGroup = NULL;
$this->instances = [];
}
protected function getGroupLabel($group) {
if ($this->moduleHandler
->moduleExists($group)) {
$label = $this->moduleHandler
->getName($group);
}
elseif ($this->themeHandler
->themeExists($group)) {
$label = $this->themeHandler
->getName($group);
}
else {
$label = $this
->t($group, [], [
'context' => 'breakpoint',
]);
}
return $label;
}
}