-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathInertia.php
160 lines (140 loc) · 4.39 KB
/
Inertia.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
<?php
namespace tebe\inertia;
use Yii;
use yii\base\Application;
use yii\base\Component;
use yii\base\Event;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
class Inertia extends Component
{
/** @var array */
public $assetsDirs = [
'@webroot/assets'
];
/** @var string */
public $shareKey = '__inertia__';
/** @var string */
public $view = '@tebe/inertia/views/inertia';
/** @var string */
public $rootElementId = 'app';
/**
* @inheritDoc
*/
public function init()
{
// Unset header since at least yii\web\ErrorAction is testing it
// Yii::$app->request->headers->set('X-Requested-With', null);
Yii::$app->on(Application::EVENT_AFTER_REQUEST, [$this, 'applicationAfterRequestHandler']);
Yii::$app->response->on(Response::EVENT_BEFORE_SEND, [$this, 'responseBeforeSendHandler']);
}
/**
* @param Event $event
*/
public function applicationAfterRequestHandler($event)
{
$response = Yii::$app->getResponse();
if ($response->headers->has('X-Redirect')) {
$url = $response->headers->get('X-Redirect', null, true);
$response->headers->set('Location', $url);
}
}
/**
* @param Event $event
*/
public function responseBeforeSendHandler($event)
{
$request = Yii::$app->getRequest();
$method = $request->getMethod();
/** @var Response $response */
$response = $event->sender;
if (!$request->headers->has('X-Inertia')) {
if ($request->enableCsrfValidation) {
$request->getCsrfToken(true);
}
return;
}
if ($response->isOk) {
$response->format = Response::FORMAT_JSON;
$response->headers->set('X-Inertia', 'true');
}
if ($method === 'GET') {
if ($request->headers->has('X-Inertia-Version')) {
$version = $request->headers->get('X-Inertia-Version', null, true);
if ($version !== $this->getVersion()) {
$response->setStatusCode(409);
$response->headers->set('X-Inertia-Location', $request->getAbsoluteUrl());
return;
}
}
}
if ($response->getIsRedirection()) {
if ($response->getStatusCode() === 302) {
if (in_array($method, ['PUT', 'PATCH', 'DELETE'])) {
$response->setStatusCode(303);
}
}
}
}
/**
* @return string
*/
public function getVersion()
{
$hashes = [];
foreach ($this->assetsDirs as $assetDir) {
$hashes[] = $this->hashDirectory(Yii::getAlias($assetDir));
}
return md5(implode('', $hashes));
}
/**
* @param array|string $key
* @param array/null $value
*/
public function share($key, $value = null)
{
if (is_array($key)) {
Yii::$app->params[$this->shareKey] = array_merge($this->getShared(), $key);
} elseif (is_string($key) && is_array($value)) {
Yii::$app->params[$this->shareKey] = array_merge($this->getShared(), [$key => $value]);
}
}
/**
* @param string|null $key
* @return array
*/
public function getShared($key = null)
{
if (is_string($key) && isset(Yii::$app->params[$this->shareKey][$key])) {
return Yii::$app->params[$this->shareKey][$key];
}
if (isset(Yii::$app->params[$this->shareKey])) {
return Yii::$app->params[$this->shareKey];
}
return [];
}
/**
* Generate an MD5 hash string from the contents of a directory.
*
* @param string $directory
* @return boolean|string
* @todo optimize by using webpack build info or a cache
*/
private function hashDirectory($directory)
{
$files = array();
$dir = dir($directory);
while (false !== ($file = $dir->read())) {
if ($file != '.' and $file != '..') {
if (is_dir($directory . '/' . $file)) {
$files[] = $this->hashDirectory($directory . '/' . $file);
} else {
$files[] = md5_file($directory . '/' . $file);
}
}
}
$dir->close();
return md5(implode('', $files));
}
}