Completed
Push — master ( 0bff19...1b573a )
by Basil
02:06
created

Theme::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace luya\theme;
4
5
use luya\helpers\Json;
6
use luya\helpers\StringHelper;
7
use Yii;
8
use yii\base\BaseObject;
9
use yii\base\InvalidConfigException;
10
11
/**
12
 * Extend the yii2 theme component and build up the `$pathMap` depends on given {{\luya\theme\ThemeConfig}} which include parents inheritance.
13
 *
14
 * @property string $layout
15
 * @property string $viewPath
16
 * @property string $layoutPath
17
 * @property string $resourcePath
18
 *
19
 * @see \luya\theme\ThemeConfig
20
 * @author Bennet Klarhoelter <[email protected]>
21
 * @since 1.0.21
22
 */
23
class Theme extends \yii\base\Theme
24
{
25
    const LAYOUTS_PATH = 'layouts';
26
    
27
    private $_config;
28
    
29
    /**
30
     * Theme constructor with a configuration as {{\luya\theme\ThemeConfig}} object.
31
     *
32
     * @param ThemeConfig $config configuration for this theme.
33
     */
34
    public function __construct(ThemeConfig $config)
35
    {
36
        $this->_config = $config;
37
        parent::__construct(['basePath' => $config->getBasePath()]);
38
    }
39
    
40
    /**
41
     * Initialize the theme.
42
     *
43
     * @throws InvalidConfigException
44
     */
45
    public function init()
46
    {
47
        if ($this->getBasePath() === null) {
48
            throw new InvalidConfigException("Property base path must be set");
49
        }
50
        
51
        $this->initPathMap($this->_config);
52
        
53
        parent::init();
54
    }
55
    
56
    /**
57
     * Init the path mapping include the parent themes
58
     */
59
    protected function initPathMap(ThemeConfig $themeConfig)
60
    {
61
        $pathMap = ['@app/views', $themeConfig->getViewPath()];
62
    
63
        $viewPath = $this->getViewPath();
64
        $this->pathMap[$viewPath] = &$pathMap;
65
        
66
        foreach ($themeConfig->getParents() as $parentConfig) {
67
            $pathMap[] = $parentConfig->getViewPath();
68
            $this->pathMap[$parentConfig->getViewPath()] = &$pathMap;
69
        }
70
    
71
        $additionalPathMap = $this->getAdditionalPathMap($themeConfig);
72
        foreach ($additionalPathMap as $from) {
73
            $this->pathMap[$from] = $pathMap;
74
        }
75
    
76
        $pos = strpos($viewPath, '/');
77
        $rootPath = $pos === false ? $viewPath : (substr($viewPath, 0, $pos) . '/views');
78
        $this->pathMap[$rootPath] = $pathMap;
79
    
80
        $this->pathMap['@app/views'] = $pathMap;
81
    }
82
    
83
    protected $_layout = 'theme';
84
    
85
    /**
86
     * The name of the layout to be applied to this theme views.
87
     *
88
     * @return string Default `theme`
89
     */
90
    public function getLayout()
91
    {
92
        return $this->_layout;
93
    }
94
    
95
    /**
96
     * Path to the directory that contains the theme views.
97
     *
98
     * @return string
99
     */
100
    public function getViewPath()
101
    {
102
        return $this->getConfig()->getViewPath();
103
    }
104
    
105
    /**
106
     * Path to the directory that contains the theme layouts.
107
     *
108
     * @return string
109
     */
110
    public function getLayoutPath()
111
    {
112
        return $this->viewPath . '/' . self::LAYOUTS_PATH;
113
    }
114
    
115
    /**
116
     * Theme configuration that contains the base theme information.
117
     *
118
     * @return ThemeConfig
119
     */
120
    public function getConfig()
121
    {
122
        return $this->_config;
123
    }
124
    
125
    /**
126
     * @param ThemeConfig $themeConfig
127
     *
128
     * @return array
129
     * @throws InvalidConfigException
130
     * @throws \luya\Exception
131
     */
132
    protected function getAdditionalPathMap(ThemeConfig $themeConfig)
133
    {
134
        $pathMap = $themeConfig->pathMap;
135
        
136
        $parentConfig = $themeConfig->getParent();
137
        if ($parentConfig) {
138
            $pathMap = array_merge($pathMap, $this->getAdditionalPathMap($parentConfig));
139
        }
140
        
141
        return $pathMap;
142
    }
143
}