Issues (144)

src/Service/ThemeResolver.php (2 issues)

1
<?php
2
3
namespace SilverStripe\Subsites\Service;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\Subsites\Model\Subsite;
8
use SilverStripe\View\SSViewer;
9
10
class ThemeResolver
11
{
12
    use Injectable;
13
    use Configurable;
14
15
    /**
16
     * Cascading definitions for themes, keyed by the name they should appear under in the CMS. For example:
17
     *
18
     * [
19
     *   'theme-1' => [
20
     *     '$public',
21
     *     'starter',
22
     *     '$default',
23
     *   ],
24
     *   'theme-2' => [
25
     *     'custom',
26
     *     'watea',
27
     *     'starter',
28
     *     '$public',
29
     *     '$default',
30
     *   ]
31
     * ]
32
     *
33
     * @config
34
     * @var null|array[]
35
     */
36
    private static $theme_options;
0 ignored issues
show
The private property $theme_options is not used, and could be removed.
Loading history...
37
38
    /**
39
     * Get the list of themes for the given sub site that can be given to SSViewer::set_themes
40
     *
41
     * @param Subsite $site
42
     * @return array
43
     */
44
    public function getThemeList(Subsite $site)
45
    {
46
        $themes = array_values(SSViewer::get_themes());
47
        $siteTheme = $site->Theme;
0 ignored issues
show
Bug Best Practice introduced by
The property Theme does not exist on SilverStripe\Subsites\Model\Subsite. Since you implemented __get, consider adding a @property annotation.
Loading history...
48
49
        if (!$siteTheme) {
50
            return $themes;
51
        }
52
53
        $customOptions = $this->config()->get('theme_options');
54
        if ($customOptions && isset($customOptions[$siteTheme])) {
55
            return $customOptions[$siteTheme];
56
        }
57
58
        // Ensure themes don't cascade "up" the list
59
        $index = array_search($siteTheme, $themes);
60
61
        if ($index > 0) {
62
            // 4.0 didn't have support for themes in the public webroot
63
            $constant = SSViewer::class . '::PUBLIC_THEME';
64
            $publicConstantDefined = defined($constant);
65
66
            // Check if the default is public themes
67
            $publicDefault = $publicConstantDefined && $themes[0] === SSViewer::PUBLIC_THEME;
68
69
            // Take only those that appear after theme chosen (non-inclusive)
70
            $themes = array_slice($themes, $index + 1);
71
72
            // Add back in public
73
            if ($publicDefault) {
74
                array_unshift($themes, SSViewer::PUBLIC_THEME);
75
            }
76
        }
77
78
        // Add our theme
79
        array_unshift($themes, $siteTheme);
80
81
        return $themes;
82
    }
83
84
    /**
85
     * Get a list of custom cascading theme definitions if available
86
     *
87
     * @return null|array
88
     */
89
    public function getCustomThemeOptions()
90
    {
91
        $config = $this->config()->get('theme_options');
92
93
        if (!$config) {
94
            return null;
95
        }
96
97
        return array_keys($config);
98
    }
99
}
100