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\controllers; |
12
|
|
|
|
13
|
|
|
use hidev\base\File; |
14
|
|
|
use hidev\helpers\Helper; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* File controller. |
20
|
|
|
* @author Andrii Vasyliev <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class FileController extends CollectionController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected $_make = ['load', 'save', 'modify']; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string specifies handler to be used |
31
|
|
|
*/ |
32
|
|
|
public $fileType; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool Don't touch file if exists |
36
|
|
|
*/ |
37
|
|
|
public $once; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string Username to change file owner to |
41
|
|
|
*/ |
42
|
|
|
public $chown; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Group to change file group to |
46
|
|
|
*/ |
47
|
|
|
public $chgrp; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string|integer Permissions to change to |
51
|
|
|
*/ |
52
|
|
|
public $chmod; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array|File the file to be handled |
56
|
|
|
*/ |
57
|
|
|
protected $_file; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string path to copy from |
61
|
|
|
*/ |
62
|
|
|
protected $_copy; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string the path to the file |
66
|
|
|
*/ |
67
|
|
|
protected $_path; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string the template name |
71
|
|
|
*/ |
72
|
|
|
protected $_template; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Template setter. |
76
|
|
|
* @param string $template name |
77
|
|
|
*/ |
78
|
1 |
|
public function setTemplate($template) |
79
|
|
|
{ |
80
|
1 |
|
$this->_template = $template; |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Template getter. |
85
|
|
|
*/ |
86
|
|
|
public function getTemplate() |
87
|
|
|
{ |
88
|
|
|
return Helper::file2template($this->_template ?: $this->id); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the file object. |
93
|
|
|
* Instantiates it if necessary. |
94
|
|
|
* |
95
|
|
|
* @return File |
96
|
|
|
*/ |
97
|
5 |
|
public function getFile() |
98
|
|
|
{ |
99
|
|
|
if (!is_object($this->_file)) { |
100
|
5 |
|
$this->_file = Yii::createObject(array_merge([ |
101
|
5 |
|
'class' => File::class, |
102
|
5 |
|
'template' => $this->getTemplate(), |
103
|
5 |
|
'goal' => $this, |
104
|
5 |
|
'path' => $this->_path ?: $this->id, |
105
|
|
|
], is_string($this->_file) |
106
|
3 |
|
? ['path' => $this->_file] |
107
|
5 |
|
: (array) $this->_file |
108
|
|
|
)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->_file; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sets file with given info. |
116
|
|
|
* @param mixed $info could be anything that is good for File::create |
117
|
|
|
*/ |
118
|
|
|
public function setFile($info) |
119
|
|
|
{ |
120
|
|
|
$this->_file = $info; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sets the path to the file, but file info has precendence. |
125
|
|
|
* @param string $value |
126
|
|
|
*/ |
127
|
|
|
public function setPath($value) |
128
|
|
|
{ |
129
|
|
|
$this->_path = $value; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Copy setter. Turns this file type to `copy`. |
134
|
|
|
*/ |
135
|
|
|
public function setCopy($value) |
136
|
|
|
{ |
137
|
|
|
$this->fileType = 'copy'; |
138
|
|
|
$this->_copy = $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Copy getter. Processes aliases. |
143
|
|
|
*/ |
144
|
|
|
public function getCopy() |
145
|
|
|
{ |
146
|
|
|
return Yii::getAlias($this->_copy); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Dirname getter. |
151
|
|
|
*/ |
152
|
|
|
public function getDirname() |
153
|
|
|
{ |
154
|
|
|
return $this->getFile()->getDirname(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Path getter. |
159
|
|
|
*/ |
160
|
|
|
public function getPath() |
161
|
|
|
{ |
162
|
|
|
return $this->getFile()->getPath(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Checks if the file exists. |
167
|
|
|
*/ |
168
|
|
|
public function exists() |
169
|
|
|
{ |
170
|
|
|
return $this->getFile()->exists(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Read the file. |
175
|
|
|
*/ |
176
|
|
|
public function read() |
177
|
|
|
{ |
178
|
|
|
return $this->getFile()->read(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Read the file into array. |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public function readArray() |
186
|
|
|
{ |
187
|
|
|
return $this->getFile()->readArray(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
public function actionPerform($name = null, $path = null) |
194
|
|
|
{ |
195
|
|
|
Yii::trace("Started: '$this->id'"); |
196
|
|
|
|
197
|
|
|
return $this->runActions(['before', 'make', 'after']); |
198
|
|
|
} |
199
|
|
|
|
200
|
5 |
|
public function actionLoad() |
201
|
|
|
{ |
202
|
|
|
$data = $this->getFile()->load() ?: []; |
203
|
5 |
|
if ($data) { /// TODO think what's better |
204
|
|
|
// $this->setItems(ArrayHelper::merge($data, $this->toArray())); |
|
|
|
|
205
|
|
|
$this->setItems(ArrayHelper::merge($this->toArray(), $data)); |
206
|
|
|
// $this->setItems($data); |
|
|
|
|
207
|
|
|
} |
208
|
5 |
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
5 |
|
public function actionSave() |
214
|
|
|
{ |
215
|
5 |
|
if ($this->once && $this->exists()) { |
216
|
|
|
return 0; |
217
|
|
|
} |
218
|
|
|
$this->_items = Helper::uniqueConfig($this->_items); |
219
|
|
|
$this->getFile()->save($this); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Modify action. |
224
|
|
|
*/ |
225
|
5 |
|
public function actionModify() |
226
|
|
|
{ |
227
|
5 |
|
foreach (['chown', 'chgrp', 'chmod'] as $k) { |
228
|
|
|
$v = $this->{$k}; |
229
|
|
|
if ($v) { |
230
|
|
|
$this->file->{$k}($v); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
} |
233
|
5 |
|
} |
234
|
|
|
} |
235
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.