-
Notifications
You must be signed in to change notification settings - Fork 2
/
Seo.php
228 lines (191 loc) · 5.14 KB
/
Seo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace shershennm\seo;
use Yii;
use yii\base\ActionEvent;
use yii\base\Component;
use yii\base\ViewEvent;
use yii\web\Controller;
use yii\web\View;
class Seo extends Component
{
/**
* controller namespace => seo controller namespace.
*
* @var array
*/
public $controllerMapping;
/**
* @var string|null
*/
public $defaultTitle;
/**
* @var string|null
*/
public $titleAppend;
/**
* @var string|null
*/
public $titlePrepend;
/**
* @var \ReflectionClass
*/
private $_reflectionController;
/**
* @return Controller
*/
public function getController()
{
return Yii::$app->controller;
}
/**
* @return \ReflectionClass
*/
public function getReflectionController()
{
if ($this->_reflectionController === null) {
$this->_reflectionController = $this->buildReflectionController();
}
return $this->_reflectionController;
}
/**
* {@inheritdoc}
*/
public function init()
{
$this->initEventBranches();
parent::init();
}
private function initEventBranches()
{
ActionEvent::on(Controller::className(), Controller::EVENT_BEFORE_ACTION, [$this, 'eventControllerBeforeAction']);
}
/**
* @param $event ActionEvent
*/
public function eventControllerBeforeAction($event)
{
ActionEvent::off(Controller::className(), Controller::EVENT_BEFORE_ACTION, [$this, 'eventControllerBeforeAction']);
$event->sender->view->on(View::EVENT_BEFORE_RENDER, [$this, 'eventViewBeforeRender']);
}
/**
* @param $event ViewEvent
*
* @throws \yii\base\InvalidConfigException
*/
public function eventViewBeforeRender($event)
{
$event->sender->off(View::EVENT_BEFORE_RENDER, [$this, 'eventViewBeforeRender']);
$this->setMeta($event);
}
/**
* @param $viewEvent ViewEvent
*
* @throws \yii\base\InvalidConfigException
*/
public function setMeta($viewEvent)
{
$set = false;
if ($this->isValidController() && $this->isSeoControllerInMapping() && $this->isSeoControllerExists()) {
$set = $this->executeSeoControllerAction($viewEvent);
}
if ($set === false) {
$this->addTitle($viewEvent->sender);
}
}
/**
* @param $viewEvent ViewEvent
*
* @return bool
*
* @throws \yii\base\InvalidConfigException
*/
private function executeSeoControllerAction($viewEvent)
{
$seoController = Yii::createObject($this->buildSeoControllerClassName());
if (!property_exists($this->controller->action, 'actionMethod')) {
return false;
}
$actionMethod = $this->controller->action->actionMethod;
if (!method_exists($seoController, $actionMethod)) {
return false;
}
$seoController->controller = $this->controller;
$seoController->view = $viewEvent->sender;
$meta = $seoController->$actionMethod($viewEvent->params);
$this->addMeta($viewEvent->sender, $meta);
$this->addTitle($viewEvent->sender, $seoController->title);
return true;
}
/**
* @param $title string
*
* @return string
*/
protected function buildTitle($title)
{
$defaults = [
'defaultTitle' => $this->defaultTitle,
'defaultPrepend' => $this->titlePrepend,
'defaultAppend' => $this->titleAppend,
];
if (is_array($title)) {
$title = new Title(array_merge($title, $defaults));
} else {
$title = new Title(array_merge(['title' => $title], $defaults));
}
return $title->buildTitle();
}
/**
* @param $view View
* @param $metaArray array
*/
public function addMeta($view, $metaArray)
{
foreach ($metaArray as $meta) {
$view->registerMetaTag($meta);
}
}
/**
* @param $view View
* @param $title null|string
*/
public function addTitle($view, $title = null)
{
$view->title = $this->buildTitle($title);
}
/**
* @return \ReflectionClass
*/
private function buildReflectionController()
{
return new \ReflectionClass($this->controller);
}
/**
* @return bool
*/
private function isSeoControllerInMapping()
{
return isset($this->controllerMapping[$this->reflectionController->getNamespaceName()]);
}
/**
* @return bool
*/
private function isSeoControllerExists()
{
return class_exists($this->buildSeoControllerClassName());
}
/**
* @return bool
*/
private function isValidController()
{
return Yii::$app->controller !== null && Yii::$app->controller->action->className() !== 'yii\web\ErrorAction';
}
/**
* @return string
*/
private function buildSeoControllerClassName()
{
return sprintf('%s\%s', $this->controllerMapping[$this->reflectionController->getNamespaceName()], $this->reflectionController->getShortName());
}
}