forked from weprovide/valet-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CraftValetDriver.php
198 lines (187 loc) · 4.58 KB
/
CraftValetDriver.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
<?php
class CraftValetDriver extends ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves($sitePath, $siteName, $uri)
{
return file_exists($sitePath.'/craft');
}
/**
* Determine the name of the directory where the front controller lives.
*
* @param string $sitePath
* @return string
*/
public function frontControllerDirectory($sitePath)
{
return is_file($sitePath.'/craft') ? 'web' : 'public';
}
/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string|false
*/
public function isStaticFile($sitePath, $siteName, $uri)
{
$frontControllerDirectory = $this->frontControllerDirectory($sitePath);
if ($this->isActualFile($staticFilePath = $sitePath.'/'.$frontControllerDirectory.$uri)) {
return $staticFilePath;
}
return false;
}
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
public function frontControllerPath($sitePath, $siteName, $uri)
{
$frontControllerDirectory = $this->frontControllerDirectory($sitePath);
// Default index path
$indexPath = $sitePath.'/'.$frontControllerDirectory.'/index.php';
$scriptName = '/index.php';
// Check if the first URL segment matches any of the defined locales
$locales = [
'ar',
'ar_sa',
'bg',
'bg_bg',
'ca_es',
'cs',
'cy_gb',
'da',
'da_dk',
'de',
'de_at',
'de_ch',
'de_de',
'el',
'el_gr',
'en',
'en_as',
'en_au',
'en_bb',
'en_be',
'en_bm',
'en_bw',
'en_bz',
'en_ca',
'en_dsrt',
'en_dsrt_us',
'en_gb',
'en_gu',
'en_gy',
'en_hk',
'en_ie',
'en_in',
'en_jm',
'en_mh',
'en_mp',
'en_mt',
'en_mu',
'en_na',
'en_nz',
'en_ph',
'en_pk',
'en_sg',
'en_shaw',
'en_tt',
'en_um',
'en_us',
'en_us_posix',
'en_vi',
'en_za',
'en_zw',
'en_zz',
'es',
'es_cl',
'es_es',
'es_mx',
'es_us',
'es_ve',
'et',
'fi',
'fi_fi',
'fil',
'fr',
'fr_be',
'fr_ca',
'fr_ch',
'fr_fr',
'fr_ma',
'he',
'hr',
'hr_hr',
'hu',
'hu_hu',
'id',
'id_id',
'it',
'it_ch',
'it_it',
'ja',
'ja_jp',
'ko',
'ko_kr',
'lt',
'lv',
'ms',
'ms_my',
'nb',
'nb_no',
'nl',
'nl_be',
'nl_nl',
'nn',
'nn_no',
'no',
'pl',
'pl_pl',
'pt',
'pt_br',
'pt_pt',
'ro',
'ro_ro',
'ru',
'ru_ru',
'sk',
'sl',
'sr',
'sv',
'sv_se',
'th',
'th_th',
'tr',
'tr_tr',
'uk',
'vi',
'zh',
'zh_cn',
'zh_tw',
];
$parts = explode('/', $uri);
if (count($parts) > 1 && in_array($parts[1], $locales)) {
$indexPath = $sitePath.'/public/'. $parts[1] .'/index.php';
$scriptName = '/' . $parts[1] . '/index.php';
}
$_SERVER['SCRIPT_FILENAME'] = $indexPath;
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
$_SERVER['SCRIPT_NAME'] = $scriptName;
$_SERVER['PHP_SELF'] = $scriptName;
$_SERVER['DOCUMENT_ROOT'] = $sitePath.'/'.$frontControllerDirectory;
return $indexPath;
}
}