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\handlers; |
12
|
|
|
|
13
|
|
|
use hidev\helpers\FileHelper; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\InvalidConfigException; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Base Handler. |
20
|
|
|
* Knows how to parse and render it's file type. |
21
|
|
|
*/ |
22
|
|
|
class BaseHandler extends \yii\base\Object |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Goal |
26
|
|
|
*/ |
27
|
|
|
public $goal; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string template file name to be used for rendering |
31
|
|
|
*/ |
32
|
|
|
public $template; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var View object |
36
|
|
|
*/ |
37
|
|
|
protected $_view; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the view object that can be used to render views or view files. |
41
|
|
|
* If not set, it will default to the "view" application component. |
42
|
|
|
* |
43
|
|
|
* @return View|\yii\web\View the view object that can be used to render views or view files |
44
|
|
|
*/ |
45
|
5 |
|
public function getView() |
46
|
|
|
{ |
47
|
5 |
|
if ($this->_view === null) { |
48
|
|
|
$this->_view = Yii::$app->getView(); |
49
|
|
|
} |
50
|
|
|
|
51
|
5 |
|
return $this->_view; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Renders prepared data. |
56
|
|
|
* Must be redefined in child. |
57
|
|
|
* |
58
|
|
|
* @param array $data |
59
|
|
|
* |
60
|
|
|
* @throws InvalidConfigException |
61
|
|
|
* |
62
|
|
|
* @return string file content |
63
|
|
|
*/ |
64
|
|
|
public function renderPrepared(array $data) |
65
|
|
|
{ |
66
|
|
|
throw new InvalidConfigException('Render not available'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Renders raw data. |
71
|
|
|
* Prepares data with ArrayHelper::toArray. |
72
|
|
|
* |
73
|
|
|
* @param mixed $data |
74
|
|
|
* |
75
|
|
|
* @return string file content |
76
|
|
|
*/ |
77
|
|
|
public function render($data) |
78
|
|
|
{ |
79
|
|
|
return $this->renderPrepared($this->prepareData($data)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function prepareData($data) |
83
|
|
|
{ |
84
|
|
|
return ArrayHelper::toArray($data, [], false); |
85
|
|
|
//return $data; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Renders file content using given data. |
90
|
|
|
* Converts to array with ArrayHelper::toArray. |
91
|
|
|
* |
92
|
|
|
* @param mixed $data |
93
|
|
|
* |
94
|
|
|
* @return string file content |
95
|
|
|
*/ |
96
|
|
|
public function renderPath($path, $data) |
97
|
|
|
{ |
98
|
|
|
return $this->write($path, $this->render($data)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function parsePath($path, $minimal = null) |
102
|
|
|
{ |
103
|
|
|
return $this->parse($this->read($path)); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Parses string input. To be redefined in real handlers. |
108
|
|
|
* @param string $input to parse |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
5 |
|
public function parse($input) |
112
|
|
|
{ |
113
|
5 |
|
return []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Writes given content to the file. |
118
|
|
|
* Doesn't touch file if it has exactly same content. |
119
|
|
|
* Creates intermediate directories when necessary. |
120
|
|
|
* @param string $path |
121
|
|
|
* @param string $content |
122
|
|
|
* @return bool true if file was changed |
123
|
|
|
*/ |
124
|
|
|
public function write($path, $content) |
125
|
|
|
{ |
126
|
|
|
return FileHelper::write($path, $content); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Creates directory if not exists. |
131
|
|
|
* @param string $path |
132
|
|
|
* @return bool true if directory did not exist and was created |
133
|
|
|
*/ |
134
|
|
|
public function mkdir($path) |
135
|
|
|
{ |
136
|
|
|
return FileHelper::mkdir($path); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Read file into a string or array. |
141
|
|
|
* @param string $path |
142
|
|
|
* @param bool $asArray |
143
|
|
|
* @return string|array |
144
|
|
|
*/ |
145
|
5 |
|
public function read($path, $asArray = false) |
146
|
|
|
{ |
147
|
|
|
if (file_exists($path)) { |
148
|
|
|
Yii::info('Read file: ' . $path, 'file'); |
149
|
|
|
|
150
|
|
|
return $asArray ? file($path) : file_get_contents($path); |
151
|
|
|
} else { |
152
|
|
|
Yii::error('Couldn\'t read file: ' . $path, 'file'); |
153
|
|
|
|
154
|
5 |
|
return; |
155
|
|
|
} |
156
|
3 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Read file into array of strings. |
160
|
|
|
* @param string $path |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function readArray($path) |
164
|
|
|
{ |
165
|
|
|
return $this->read($path, true); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.