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

ThemeConfig::setParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
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\FileHelper;
6
use Yii;
7
use luya\helpers\Json;
8
use yii\base\Arrayable;
9
use yii\base\ArrayableTrait;
10
use yii\base\BaseObject;
11
use yii\base\InvalidArgumentException;
12
use yii\base\InvalidConfigException;
13
14
/**
15
 * Load the config from the theme.json file and the config inheritance from the parent theme.
16
 *
17
 * @author Bennet Klarhoelter <[email protected]>
18
 * @since 1.0.21
19
 */
20
class ThemeConfig extends BaseObject implements Arrayable
21
{
22
    use ArrayableTrait;
23
    
24
    protected $_basePath;
25
    
26
    /**
27
     * @var string The pretty name of the theme.
28
     */
29
    public $name;
30
    
31
    /**
32
     * @var string Base path (or alias) of the parent theme.
33
     */
34
    public $parentTheme;
35
    
36
    /**
37
     * @var array Additional path to override by this theme.
38
     * @see \luya\theme\Theme::getAdditionalPathMap
39
     */
40
    public $pathMap = [];
41
    
42
    /**
43
     * @var string Some information about the theme.
44
     */
45
    public $description;
46
    
47
    /**
48
     * ThemeConfig constructor with base path of the theme directory and config as array.
49
     *
50
     * @param string $basePath The base path of the theme.
51
     * @param array  $config key-value pair
52
     *
53
     * @throws InvalidConfigException
54
     */
55
    public function __construct(string $basePath, array $config)
56
    {
57
        if (!is_readable(Yii::getAlias($basePath))) {
58
            throw new InvalidConfigException("The path of $basePath is not readable or not exists.");
59
        }
60
    
61
        $this->_basePath = $basePath;
62
    
63
        parent::__construct($config);
64
    }
65
    
66
    /**
67
     * @inheritDoc
68
     */
69
    public function init()
70
    {
71
        if (empty($this->name)) {
72
            $this->name = basename($this->_basePath);
73
        }
74
        
75
        parent::init();
76
    }
77
    
78
    protected $_parent;
79
    
80
    /**
81
     * Load the config of the parent theme.
82
     *
83
     * @return ThemeConfig
84
     * @throws InvalidConfigException
85
     * @throws \luya\Exception
86
     */
87
    public function getParent()
88
    {
89
        if ($this->_parent === null && $this->parentTheme) {
90
            $this->_parent = Yii::$app->themeManager->getThemeByBasePath($this->parentTheme);
91
        }
92
        
93
        return $this->_parent;
94
    }
95
    
96
    /**
97
     * Set the parent theme config. Is only required while initialize this class.
98
     *
99
     * @param ThemeConfig $themeConfig
100
     */
101
    protected function setParent(ThemeConfig $themeConfig)
102
    {
103
        $this->_parent = $themeConfig;
104
    }
105
    
106
    /**
107
     * Load all parent themes recursive in a ordered array. First entry is the parent of this theme, seconds entry is the parent of the parent and so on.
108
     *
109
     * @return array
110
     * @throws InvalidConfigException
111
     * @throws \luya\Exception
112
     */
113
    public function getParents()
114
    {
115
        $parents = [];
116
        
117
        $parent = $this->getParent();
118
        if ($parent) {
119
            $parents[] = $parent;
120
            $parents = array_merge($parents, $parent->getParents());
121
        }
122
        
123
        return $parents;
124
    }
125
    
126
    /**
127
     * Base path (or alias) to the theme directory.
128
     *
129
     * @return string
130
     */
131
    public function getBasePath()
132
    {
133
        return $this->_basePath;
134
    }
135
    
136
    /**
137
     * Path to view directory of the theme.
138
     *
139
     * @return string
140
     */
141
    public function getViewPath()
142
    {
143
        return $this->getBasePath() . '/views';
144
    }
145
}