1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Automation tool mixed with code generator for easier continuous development |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hidev |
6
|
|
|
* @package hidev |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hidev\base; |
12
|
|
|
|
13
|
|
|
use hidev\helpers\Helper; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Configuration file component. |
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ConfigFile extends \hidev\base\Component implements \yii\base\Arrayable, \ArrayAccess, \IteratorAggregate |
22
|
|
|
{ |
23
|
|
|
use \hiqdev\yii2\collection\ObjectTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string specifies handler to be used |
27
|
|
|
*/ |
28
|
|
|
public $fileType; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array|File the file to be handled |
32
|
|
|
*/ |
33
|
|
|
protected $_file; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string path to copy from |
37
|
|
|
*/ |
38
|
|
|
protected $_copy; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string the path to the file |
42
|
|
|
*/ |
43
|
|
|
protected $_path; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string the template name |
47
|
|
|
*/ |
48
|
|
|
protected $_template; |
49
|
|
|
|
50
|
|
|
public function init() |
51
|
|
|
{ |
52
|
|
|
$this->load(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Template setter. |
57
|
|
|
* @param string $template name |
58
|
|
|
*/ |
59
|
|
|
public function setTemplate($template) |
60
|
|
|
{ |
61
|
|
|
$this->_template = $template; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Template getter. |
66
|
|
|
*/ |
67
|
|
|
public function getTemplate() |
68
|
|
|
{ |
69
|
|
|
return Helper::file2template($this->_template ?: $this->id); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the file object. |
74
|
|
|
* Instantiates it if necessary. |
75
|
|
|
* @return File |
76
|
|
|
*/ |
77
|
|
|
public function getFile() |
78
|
|
|
{ |
79
|
|
|
if (!is_object($this->_file)) { |
80
|
|
|
$this->_file = Yii::createObject(array_merge([ |
81
|
|
|
'class' => File::class, |
82
|
|
|
'template' => $this->getTemplate(), |
83
|
|
|
'goal' => $this, |
84
|
|
|
'path' => $this->_path ?: $this->id, |
|
|
|
|
85
|
|
|
], is_string($this->_file) |
86
|
|
|
? ['path' => $this->_file] |
87
|
|
|
: (array) $this->_file |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->_file; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets file with given info. |
96
|
|
|
* @param mixed $info could be anything that is good for File::create |
97
|
|
|
*/ |
98
|
|
|
public function setFile($info) |
99
|
|
|
{ |
100
|
|
|
$this->_file = $info; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets the path to the file, but file info has precendence. |
105
|
|
|
* @param string $value |
106
|
|
|
*/ |
107
|
|
|
public function setPath($value) |
108
|
|
|
{ |
109
|
|
|
$this->_path = $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Copy getter. Processes aliases. |
114
|
|
|
*/ |
115
|
|
|
public function getCopy() |
116
|
|
|
{ |
117
|
|
|
return Yii::getAlias($this->_copy); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Dirname getter. |
122
|
|
|
*/ |
123
|
|
|
public function getDirname() |
124
|
|
|
{ |
125
|
|
|
return $this->getFile()->getDirname(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Path getter. |
130
|
|
|
*/ |
131
|
|
|
public function getPath() |
132
|
|
|
{ |
133
|
|
|
return $this->getFile()->getPath(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Checks if the file exists. |
138
|
|
|
*/ |
139
|
|
|
public function exists() |
140
|
|
|
{ |
141
|
|
|
return $this->getFile()->exists(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Read the file. |
146
|
|
|
*/ |
147
|
|
|
public function read() |
148
|
|
|
{ |
149
|
|
|
return $this->getFile()->read(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Read the file into array. |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function readArray() |
157
|
|
|
{ |
158
|
|
|
return $this->getFile()->readArray(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function load() |
162
|
|
|
{ |
163
|
|
|
$data = $this->getFile()->load() ?: []; |
164
|
|
|
if ($data) { /// TODO think what's better |
165
|
|
|
// $this->setItems(ArrayHelper::merge($data, $this->toArray())); |
|
|
|
|
166
|
|
|
$this->setItems(ArrayHelper::merge($this->toArray(), $data)); |
167
|
|
|
// $this->setItems($data); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Save the file. |
173
|
|
|
*/ |
174
|
|
|
public function save() |
175
|
|
|
{ |
176
|
|
|
if ($this->once && $this->exists()) { |
|
|
|
|
177
|
|
|
return 0; |
178
|
|
|
} |
179
|
|
|
$this->_items = Helper::uniqueConfig($this->_items); |
180
|
|
|
$this->getFile()->save($this); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.