1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Task runner, code generator and build tool for easier continuos integration |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hidev |
7
|
|
|
* @package hidev |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hidev\controllers; |
13
|
|
|
|
14
|
|
|
use hidev\base\File; |
15
|
|
|
use hidev\helpers\Helper; |
16
|
|
|
use Yii; |
17
|
|
|
use yii\helpers\ArrayHelper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* File controller. |
21
|
|
|
*/ |
22
|
|
|
class FileController extends CollectionController |
23
|
|
|
{ |
24
|
|
|
protected $_make = ['load', 'save', 'modify']; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string specifies handler to be used |
28
|
|
|
*/ |
29
|
|
|
public $fileType; |
30
|
|
|
|
31
|
|
|
public $once; |
32
|
|
|
public $chown; |
33
|
|
|
public $chgrp; |
34
|
|
|
public $chmod; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array|File the file to be handled. |
38
|
|
|
*/ |
39
|
|
|
protected $_file; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string path to copy from. |
43
|
|
|
*/ |
44
|
|
|
public $_copy; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string the path to the file. |
48
|
|
|
*/ |
49
|
|
|
protected $_path; |
50
|
|
|
|
51
|
|
|
protected $_template; |
52
|
|
|
|
53
|
1 |
|
public function setTemplate($template) |
54
|
|
|
{ |
55
|
1 |
|
$this->_template = $template; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
public function getTemplate() |
59
|
|
|
{ |
60
|
|
|
return Helper::file2template($this->_template ?: $this->id); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the file object. |
65
|
|
|
* Instantiates it if necessary. |
66
|
|
|
* |
67
|
|
|
* @return File |
68
|
|
|
*/ |
69
|
|
|
public function getFile() |
70
|
|
|
{ |
71
|
|
|
if (!is_object($this->_file)) { |
72
|
|
|
$this->_file = Yii::createObject(array_merge([ |
73
|
|
|
'class' => File::className(), |
74
|
|
|
'template' => $this->getTemplate(), |
75
|
|
|
'goal' => $this, |
76
|
|
|
'path' => $this->_path ?: $this->id, |
77
|
|
|
], is_string($this->_file) |
78
|
|
|
? ['path' => $this->_file] |
79
|
|
|
: (array) $this->_file |
80
|
|
|
)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->_file; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets file with given info. |
88
|
|
|
* @param mixed $info could be anything that is good for File::create |
89
|
|
|
*/ |
90
|
|
|
public function setFile($info) |
91
|
|
|
{ |
92
|
|
|
$this->_file = $info; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets the path to the file, but file info has precendence. |
97
|
|
|
* @param string $value |
98
|
|
|
*/ |
99
|
|
|
public function setPath($value) |
100
|
|
|
{ |
101
|
|
|
$this->_path = $value; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setCopy($value) |
105
|
|
|
{ |
106
|
|
|
$this->fileType = 'copy'; |
107
|
|
|
$this->_copy = $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getCopy() |
111
|
|
|
{ |
112
|
|
|
return Yii::getAlias($this->_copy); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getDirname() |
116
|
|
|
{ |
117
|
|
|
return $this->getFile()->getDirname(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getPath() |
121
|
|
|
{ |
122
|
|
|
return $this->getFile()->getPath(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function exists() |
126
|
|
|
{ |
127
|
|
|
return $this->getFile()->exists(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function read() |
131
|
|
|
{ |
132
|
|
|
return $this->getFile()->read(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function readArray() |
136
|
|
|
{ |
137
|
|
|
return $this->getFile()->readArray(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function actionPerform($name = null, $path = null) |
141
|
|
|
{ |
142
|
|
|
Yii::trace("Started: '$this->id'"); |
143
|
|
|
return $this->runActions(['before', 'make', 'after']); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function actionLoad() |
147
|
|
|
{ |
148
|
|
|
$data = $this->getFile()->load() ?: []; |
149
|
|
|
if ($data) { /// TODO think what's better |
150
|
|
|
// $this->setItems(ArrayHelper::merge($data, $this->toArray())); |
|
|
|
|
151
|
|
|
$this->setItems(ArrayHelper::merge($this->toArray(), $data)); |
152
|
|
|
// $this->setItems($data); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function actionSave() |
157
|
|
|
{ |
158
|
|
|
if ($this->once && $this->exists()) { |
159
|
|
|
return 0; |
160
|
|
|
} |
161
|
|
|
$this->_items = Helper::uniqueConfig($this->_items); |
162
|
|
|
$this->getFile()->save($this); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function actionModify() |
166
|
|
|
{ |
167
|
|
|
foreach (['chown', 'chgrp', 'chmod'] as $k) { |
168
|
|
|
$v = $this->{$k}; |
169
|
|
|
if ($v) { |
170
|
|
|
$this->file->{$k}($v); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
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.