f073f46f7cd584cf213ed2e8716a09da32357e0b
[yaffs-website] / web / core / lib / Drupal / Core / Theme / ThemeAccessCheck.php
1 <?php
2
3 namespace Drupal\Core\Theme;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Extension\ThemeHandlerInterface;
7 use Drupal\Core\Routing\Access\AccessInterface;
8
9 /**
10  * Provides access checking for themes for routing and theme negotiation.
11  */
12 class ThemeAccessCheck implements AccessInterface {
13
14   /**
15    * The theme handler.
16    *
17    * @var \Drupal\Core\Extension\ThemeHandlerInterface
18    */
19   protected $themeHandler;
20
21   /**
22    * Constructs a \Drupal\Core\Theme\Registry object.
23    *
24    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
25    *   The theme handler.
26    */
27   public function __construct(ThemeHandlerInterface $theme_handler) {
28     $this->themeHandler = $theme_handler;
29   }
30
31   /**
32    * Checks access to the theme for routing.
33    *
34    * @param string $theme
35    *   The name of a theme.
36    *
37    * @return \Drupal\Core\Access\AccessResultInterface
38    *   The access result.
39    */
40   public function access($theme) {
41     // Cacheable until the theme settings are modified.
42     return AccessResult::allowedIf($this->checkAccess($theme))->addCacheTags(['config:' . $theme . '.settings']);
43   }
44
45   /**
46    * Indicates whether the theme is accessible based on whether it is installed.
47    *
48    * @param string $theme
49    *   The name of a theme.
50    *
51    * @return bool
52    *   TRUE if the theme is installed, FALSE otherwise.
53    */
54   public function checkAccess($theme) {
55     $themes = $this->themeHandler->listInfo();
56     return !empty($themes[$theme]->status);
57   }
58
59 }