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\handlers; |
13
|
|
|
|
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
|
|
|
public function getView() |
46
|
|
|
{ |
47
|
|
|
if ($this->_view === null) { |
48
|
|
|
$this->_view = Yii::$app->getView(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
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
|
|
|
* Writes given content to the file. |
108
|
|
|
* Doesn't touch file if it has exactly same content. |
109
|
|
|
* Creates intermediate directories when necessary. |
110
|
|
|
* @param string $path |
111
|
|
|
* @param string $content |
112
|
|
|
* @return bool true if file was changed |
113
|
|
|
*/ |
114
|
|
|
public function write($path, $content) |
115
|
|
|
{ |
116
|
|
|
if (!is_file($path) || file_get_contents($path) !== $content) { |
117
|
|
|
$this->mkdir(dirname($path)); |
118
|
|
|
file_put_contents($path, $content); |
119
|
|
|
Yii::warning('Written file: ' . $path, 'file'); |
120
|
|
|
|
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Creates directory if not exists. |
129
|
|
|
* @param string $path |
130
|
|
|
* @return bool true if directory did not exist and was created |
131
|
|
|
*/ |
132
|
|
|
public function mkdir($path) |
133
|
|
|
{ |
134
|
|
|
$path = trim(trim($path), '/'); |
135
|
|
|
if (!file_exists($path)) { |
136
|
|
|
mkdir($path, 0777, true); |
137
|
|
|
Yii::warning('Created dir: ' . $path . '/', 'file'); |
138
|
|
|
|
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
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
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function readArray($path) |
159
|
|
|
{ |
160
|
|
|
return $this->read($path, true); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: