-
Notifications
You must be signed in to change notification settings - Fork 700
/
ValetDriver.php
217 lines (187 loc) · 5.96 KB
/
ValetDriver.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
<?php
abstract class ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
abstract public function serves($sitePath, $siteName, $uri);
/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string|false
*/
abstract public function isStaticFile($sitePath, $siteName, $uri);
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
abstract public function frontControllerPath($sitePath, $siteName, $uri);
/**
* Find a driver that can serve the incoming request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return ValetDriver|null
*/
public static function assign($sitePath, $siteName, $uri)
{
$drivers = [];
if ($customSiteDriver = static::customSiteDriver($sitePath)) {
$drivers[] = $customSiteDriver;
}
$drivers = array_merge($drivers, static::driversIn(VALET_HOME_PATH.'/Drivers'));
$drivers[] = 'LaravelValetDriver';
$drivers[] = 'WordPressValetDriver';
$drivers[] = 'BedrockValetDriver';
$drivers[] = 'ContaoValetDriver';
$drivers[] = 'SymfonyValetDriver';
$drivers[] = 'CraftValetDriver';
$drivers[] = 'StatamicValetDriver';
$drivers[] = 'StatamicV1ValetDriver';
$drivers[] = 'CakeValetDriver';
$drivers[] = 'SculpinValetDriver';
$drivers[] = 'JigsawValetDriver';
$drivers[] = 'KirbyValetDriver';
$drivers[] = 'KatanaValetDriver';
$drivers[] = 'JoomlaValetDriver';
$drivers[] = 'DrupalValetDriver';
$drivers[] = 'Concrete5ValetDriver';
$drivers[] = 'Typo3ValetDriver';
$drivers[] = 'NeosValetDriver';
$drivers[] = 'Magento2ValetDriver';
$drivers[] = 'BasicValetDriver';
foreach ($drivers as $driver) {
$driver = new $driver;
if ($driver->serves($sitePath, $siteName, $driver->mutateUri($uri))) {
return $driver;
}
}
}
/**
* Get the custom driver class from the site path, if one exists.
*
* @param string $sitePath
* @return string
*/
public static function customSiteDriver($sitePath)
{
if (! file_exists($sitePath.'/LocalValetDriver.php')) {
return;
}
require_once $sitePath.'/LocalValetDriver.php';
return 'LocalValetDriver';
}
/**
* Get all of the driver classes in a given path.
*
* @param string $path
* @return array
*/
public static function driversIn($path)
{
if (! is_dir($path)) {
return [];
}
$drivers = [];
$dir = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dir);
$regex = new RegexIterator($iterator, '/^.+ValetDriver\.php$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($regex as $file) {
require_once $file[0];
$drivers[] = basename($file[0], '.php');
}
return $drivers;
}
/**
* Mutate the incoming URI.
*
* @param string $uri
* @return string
*/
public function mutateUri($uri)
{
return $uri;
}
/**
* Serve the static file at the given path.
*
* @param string $staticFilePath
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
{
/**
* Back story...
*
* PHP docs *claim* you can set default_mimetype = "" to disable the default
* Content-Type header. This works in PHP 7+, but in PHP 5.* it sends an
* *empty* Content-Type header, which is significantly different than
* sending *no* Content-Type header.
*
* However, if you explicitly set a Content-Type header, then explicitly
* remove that Content-Type header, PHP seems to not re-add the default.
*
* I have a hard time believing this is by design and not coincidence.
*
* Burn. it. all.
*/
header('Content-Type: text/html');
header_remove('Content-Type');
header('X-Accel-Redirect: /' . VALET_STATIC_PREFIX . $staticFilePath);
}
/**
* Determine if the path is a file and not a directory.
*
* @param string $path
* @return bool
*/
protected function isActualFile($path)
{
return ! is_dir($path) && file_exists($path);
}
/**
* Load server environment variables if available.
* Processes any '*' entries first, and then adds site-specific entries
*
* @param string $sitePath
* @param string $siteName
* @return void
*/
public function loadServerEnvironmentVariables($sitePath, $siteName)
{
$varFilePath = $sitePath . '/.valet-env.php';
if (! file_exists($varFilePath)) {
$varFilePath = VALET_HOME_PATH . '/.valet-env.php';
}
if (! file_exists($varFilePath)) {
return;
}
$variables = include $varFilePath;
$variablesToSet = isset($variables['*']) ? $variables['*'] : [];
if (isset($variables[$siteName])) {
$variablesToSet = array_merge($variablesToSet, $variables[$siteName]);
}
foreach ($variablesToSet as $key => $value) {
if (! is_string($key)) continue;
$_SERVER[$key] = $value;
$_ENV[$key] = $value;
putenv($key . '=' . $value);
}
}
}