1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace floor12\metamaster; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Component; |
7
|
|
|
use yii\web\Request; |
8
|
|
|
use yii\web\View; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package floor12\metamaster |
12
|
|
|
* @property string $siteName |
13
|
|
|
* @property string $type |
14
|
|
|
* @property string $title |
15
|
|
|
* @property string $keywords |
16
|
|
|
* @property string $description |
17
|
|
|
* @property string $url |
18
|
|
|
* @property string $defaultImage |
19
|
|
|
* @property string $image |
20
|
|
|
* @property string $imagePath |
21
|
|
|
* @property string $web |
22
|
|
|
* @property View $view |
23
|
|
|
*/ |
24
|
|
|
class MetaMaster extends Component |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $siteName = 'My Test Application'; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $web = "@app/web"; |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $defaultImage; |
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $protocol = 'https'; |
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $type = 'article'; |
46
|
|
|
/** |
47
|
|
|
* @var Request |
48
|
|
|
*/ |
49
|
|
|
private $request; |
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $title; |
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $description; |
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $url; |
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private $image; |
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $imagePath; |
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $view; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
|
|
public function init() |
79
|
|
|
{ |
80
|
|
|
if (!$this->request) |
81
|
|
|
$this->request = Yii::$app->request; |
|
|
|
|
82
|
|
|
parent::init(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** Site name setter |
86
|
|
|
* @param $siteName |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function setSiteName(string $siteName) |
90
|
|
|
{ |
91
|
|
|
$this->siteName = $siteName; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** Page title setter |
96
|
|
|
* @param $title |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function setTitle(string $title) |
100
|
|
|
{ |
101
|
|
|
$this->title = $title; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** Url setter |
106
|
|
|
* @param $title |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function setUrl(string $url) |
110
|
|
|
{ |
111
|
|
|
$this->url = $url; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** Set request object |
116
|
|
|
* @param $title |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setRequest($request) |
120
|
|
|
{ |
121
|
|
|
$this->request = $request; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** OgType setter |
127
|
|
|
* @param $type |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function setType(string $type) |
131
|
|
|
{ |
132
|
|
|
$this->type = $type; |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** Meta description setter |
137
|
|
|
* @param string $description |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setDescription(string $description) |
141
|
|
|
{ |
142
|
|
|
$this->description = $description; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** Meta keyword setter is deprecated from 1.1.0 |
147
|
|
|
* @param string $keywords |
148
|
|
|
* @return $this |
149
|
|
|
* @deprecated |
150
|
|
|
*/ |
151
|
|
|
public function setKeywords(string $keywords) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** Set Open Graph image tag |
157
|
|
|
* @param string $image |
158
|
|
|
* @param string|null $imagePath |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function setImage(string $image, string $imagePath = null) |
162
|
|
|
{ |
163
|
|
|
$this->image = $image; |
164
|
|
|
$this->imagePath = $imagePath; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** Register meta tags in View |
169
|
|
|
* @param View $view |
170
|
|
|
*/ |
171
|
|
|
public function register(View $view) |
172
|
|
|
{ |
173
|
|
|
$this->view = $view; |
174
|
|
|
$this->registerCoreInfo(); |
175
|
|
|
$this->registerTitle(); |
176
|
|
|
$this->registerDescription(); |
177
|
|
|
$this->registerImage(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Register core meta and og tags |
182
|
|
|
*/ |
183
|
|
|
private function registerCoreInfo() |
184
|
|
|
{ |
185
|
|
|
$this->view->registerMetaTag(['property' => 'og:site_name', 'content' => $this->siteName]); |
186
|
|
|
$this->view->registerMetaTag(['property' => 'og:type', 'content' => $this->type]); |
187
|
|
|
$this->view->registerMetaTag(['property' => 'og:url', 'content' => $this->url ?: $this->getAbsoluteUrl()]); |
188
|
|
|
$this->view->registerMetaTag(['name' => 'twitter:card', 'content' => 'summary']); |
189
|
|
|
$this->view->registerMetaTag(['name' => 'twitter:domain', 'content' => $this->getAbsoluteUrl('')]); |
190
|
|
|
$this->view->registerMetaTag(['name' => 'twitter:site', 'content' => $this->siteName]); |
191
|
|
|
$this->view->registerMetaTag(['name' => 'twitter:site', 'content' => $this->siteName]); |
192
|
|
|
$this->view->registerLinkTag(['rel' => 'canonical', 'href' => $this->url ?: $this->getAbsoluteUrl()]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param null $absoluteUrl |
|
|
|
|
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
public function getAbsoluteUrl($absoluteUrl = null) |
200
|
|
|
{ |
201
|
|
|
|
202
|
|
|
if ($absoluteUrl !== null) |
|
|
|
|
203
|
|
|
$absoluteUrl = $this->request->getHostInfo() . $absoluteUrl; |
204
|
|
|
else |
205
|
|
|
$absoluteUrl = $this->request->absoluteUrl; |
206
|
|
|
return preg_replace('/https|http/', $this->protocol, $absoluteUrl, -1, $count); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Register title |
211
|
|
|
*/ |
212
|
|
|
private function registerTitle() |
213
|
|
|
{ |
214
|
|
|
if ($this->title) { |
215
|
|
|
$this->view->title = $this->title; |
216
|
|
|
$this->view->registerMetaTag(['property' => 'og:title', 'content' => $this->title]); |
217
|
|
|
$this->view->registerMetaTag(['itemprop' => 'name', 'content' => $this->title]); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Register description |
223
|
|
|
*/ |
224
|
|
|
private function registerDescription() |
225
|
|
|
{ |
226
|
|
|
if ($this->description) { |
227
|
|
|
$this->view->registerMetaTag(['name' => 'description', 'content' => $this->description]); |
228
|
|
|
$this->view->registerMetaTag(['property' => 'og:description', 'content' => $this->description]); |
229
|
|
|
$this->view->registerMetaTag(['name' => 'twitter:description', 'content' => $this->description]); |
230
|
|
|
|
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Register image |
236
|
|
|
*/ |
237
|
|
|
private function registerImage() |
238
|
|
|
{ |
239
|
|
|
$image = $this->image ?: $this->defaultImage; |
240
|
|
|
if ($image) { |
241
|
|
|
$imageUrl = $this->getAbsoluteUrl($image); |
242
|
|
|
$this->view->registerMetaTag(['property' => 'og:image', 'content' => $imageUrl]); |
243
|
|
|
$this->view->registerMetaTag(['property' => 'twitter:image:src', 'content' => $imageUrl]); |
244
|
|
|
$this->view->registerMetaTag(['itemprop' => 'image', 'content' => $imageUrl]); |
245
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$path = Yii::getAlias($this->imagePath ?: $this->web . $image); |
249
|
|
|
if ($this->imagePath) |
250
|
|
|
$path = $this->imagePath; |
251
|
|
|
if (file_exists($path)) { |
|
|
|
|
252
|
|
|
$imageSize = getimagesize($path); |
|
|
|
|
253
|
|
|
$this->view->registerMetaTag(['property' => 'og:image:width', 'content' => $imageSize[0]]); |
254
|
|
|
$this->view->registerMetaTag(['property' => 'og:image:height', 'content' => $imageSize[1]]); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function getRequest(): Request |
259
|
|
|
{ |
260
|
|
|
return $this->request; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.