annotate core/lib/Drupal/Core/Extension/ThemeHandler.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Extension;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@17 6 use Drupal\Core\Extension\Exception\UninstalledExtensionException;
Chris@17 7 use Drupal\Core\Extension\Exception\UnknownExtensionException;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Default theme handler using the config system to store installation statuses.
Chris@0 11 */
Chris@0 12 class ThemeHandler implements ThemeHandlerInterface {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * A list of all currently available themes.
Chris@0 16 *
Chris@0 17 * @var array
Chris@0 18 */
Chris@0 19 protected $list;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The config factory to get the installed themes.
Chris@0 23 *
Chris@0 24 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 25 */
Chris@0 26 protected $configFactory;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * An extension discovery instance.
Chris@0 30 *
Chris@18 31 * @var \Drupal\Core\Extension\ThemeExtensionList
Chris@0 32 */
Chris@18 33 protected $themeList;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The app root.
Chris@0 37 *
Chris@0 38 * @var string
Chris@0 39 */
Chris@0 40 protected $root;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Constructs a new ThemeHandler.
Chris@0 44 *
Chris@0 45 * @param string $root
Chris@0 46 * The app root.
Chris@0 47 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 48 * The config factory to get the installed themes.
Chris@18 49 * @param \Drupal\Core\Extension\ThemeExtensionList $theme_list
Chris@18 50 * A extension discovery instance.
Chris@0 51 */
Chris@18 52 public function __construct($root, ConfigFactoryInterface $config_factory, ThemeExtensionList $theme_list) {
Chris@0 53 $this->root = $root;
Chris@0 54 $this->configFactory = $config_factory;
Chris@18 55 $this->themeList = $theme_list;
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * {@inheritdoc}
Chris@0 60 */
Chris@0 61 public function getDefault() {
Chris@0 62 return $this->configFactory->get('system.theme')->get('default');
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * {@inheritdoc}
Chris@0 67 */
Chris@0 68 public function setDefault($name) {
Chris@0 69 $list = $this->listInfo();
Chris@0 70 if (!isset($list[$name])) {
Chris@17 71 throw new UninstalledExtensionException("$name theme is not installed.");
Chris@0 72 }
Chris@0 73 $this->configFactory->getEditable('system.theme')
Chris@0 74 ->set('default', $name)
Chris@0 75 ->save();
Chris@0 76 return $this;
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * {@inheritdoc}
Chris@0 81 */
Chris@0 82 public function install(array $theme_list, $install_dependencies = TRUE) {
Chris@0 83 // We keep the old install() method as BC layer but redirect directly to the
Chris@0 84 // theme installer.
Chris@0 85 return \Drupal::service('theme_installer')->install($theme_list, $install_dependencies);
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * {@inheritdoc}
Chris@0 90 */
Chris@0 91 public function uninstall(array $theme_list) {
Chris@0 92 // We keep the old uninstall() method as BC layer but redirect directly to
Chris@0 93 // the theme installer.
Chris@0 94 \Drupal::service('theme_installer')->uninstall($theme_list);
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * {@inheritdoc}
Chris@0 99 */
Chris@0 100 public function listInfo() {
Chris@0 101 if (!isset($this->list)) {
Chris@0 102 $this->list = [];
Chris@18 103 $installed_themes = $this->configFactory->get('core.extension')->get('theme');
Chris@18 104 if (!empty($installed_themes)) {
Chris@18 105 $installed_themes = array_intersect_key($this->themeList->getList(), $installed_themes);
Chris@18 106 array_map([$this, 'addTheme'], $installed_themes);
Chris@0 107 }
Chris@0 108 }
Chris@0 109 return $this->list;
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * {@inheritdoc}
Chris@0 114 */
Chris@0 115 public function addTheme(Extension $theme) {
Chris@18 116 // Register the namespaces of installed themes.
Chris@18 117 // @todo Implement proper theme registration
Chris@18 118 // https://www.drupal.org/project/drupal/issues/2941757
Chris@18 119 \Drupal::service('class_loader')->addPsr4('Drupal\\' . $theme->getName() . '\\', $this->root . '/' . $theme->getPath() . '/src');
Chris@18 120
Chris@0 121 if (!empty($theme->info['libraries'])) {
Chris@0 122 foreach ($theme->info['libraries'] as $library => $name) {
Chris@0 123 $theme->libraries[$library] = $name;
Chris@0 124 }
Chris@0 125 }
Chris@0 126 if (isset($theme->info['engine'])) {
Chris@0 127 $theme->engine = $theme->info['engine'];
Chris@0 128 }
Chris@0 129 if (isset($theme->info['base theme'])) {
Chris@0 130 $theme->base_theme = $theme->info['base theme'];
Chris@0 131 }
Chris@0 132 $this->list[$theme->getName()] = $theme;
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * {@inheritdoc}
Chris@0 137 */
Chris@0 138 public function refreshInfo() {
Chris@18 139 $installed = $this->configFactory->get('core.extension')->get('theme');
Chris@0 140 // Only refresh the info if a theme has been installed. Modules are
Chris@0 141 // installed before themes by the installer and this method is called during
Chris@0 142 // module installation.
Chris@0 143 if (empty($installed) && empty($this->list)) {
Chris@0 144 return;
Chris@0 145 }
Chris@0 146 $this->reset();
Chris@0 147 }
Chris@0 148
Chris@0 149 /**
Chris@0 150 * {@inheritdoc}
Chris@0 151 */
Chris@0 152 public function reset() {
Chris@18 153 $this->themeList->reset();
Chris@0 154 $this->list = NULL;
Chris@0 155 }
Chris@0 156
Chris@0 157 /**
Chris@0 158 * {@inheritdoc}
Chris@0 159 */
Chris@0 160 public function rebuildThemeData() {
Chris@18 161 return $this->themeList->reset()->getList();
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * {@inheritdoc}
Chris@0 166 */
Chris@0 167 public function getBaseThemes(array $themes, $theme) {
Chris@18 168 return $this->themeList->getBaseThemes($themes, $theme);
Chris@0 169 }
Chris@0 170
Chris@0 171 /**
Chris@0 172 * {@inheritdoc}
Chris@0 173 */
Chris@0 174 public function getName($theme) {
Chris@18 175 return $this->themeList->getName($theme);
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * {@inheritdoc}
Chris@0 180 */
Chris@0 181 public function getThemeDirectories() {
Chris@0 182 $dirs = [];
Chris@0 183 foreach ($this->listInfo() as $name => $theme) {
Chris@0 184 $dirs[$name] = $this->root . '/' . $theme->getPath();
Chris@0 185 }
Chris@0 186 return $dirs;
Chris@0 187 }
Chris@0 188
Chris@0 189 /**
Chris@0 190 * {@inheritdoc}
Chris@0 191 */
Chris@0 192 public function themeExists($theme) {
Chris@0 193 $themes = $this->listInfo();
Chris@0 194 return isset($themes[$theme]);
Chris@0 195 }
Chris@0 196
Chris@0 197 /**
Chris@0 198 * {@inheritdoc}
Chris@0 199 */
Chris@0 200 public function getTheme($name) {
Chris@0 201 $themes = $this->listInfo();
Chris@0 202 if (isset($themes[$name])) {
Chris@0 203 return $themes[$name];
Chris@0 204 }
Chris@17 205 throw new UnknownExtensionException(sprintf('The theme %s does not exist.', $name));
Chris@0 206 }
Chris@0 207
Chris@0 208 /**
Chris@0 209 * {@inheritdoc}
Chris@0 210 */
Chris@0 211 public function hasUi($name) {
Chris@0 212 $themes = $this->listInfo();
Chris@0 213 if (isset($themes[$name])) {
Chris@0 214 if (!empty($themes[$name]->info['hidden'])) {
Chris@0 215 $theme_config = $this->configFactory->get('system.theme');
Chris@0 216 return $name == $theme_config->get('default') || $name == $theme_config->get('admin');
Chris@0 217 }
Chris@0 218 return TRUE;
Chris@0 219 }
Chris@0 220 return FALSE;
Chris@0 221 }
Chris@0 222
Chris@0 223 }