forked from weprovide/valet-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicValetDriver.php
146 lines (133 loc) · 3.93 KB
/
BasicValetDriver.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
<?php
class BasicValetDriver 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 true;
}
/**
* 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)
{
if (file_exists($staticFilePath = $sitePath.'/public'.$uri)) {
return $staticFilePath;
} elseif ($this->isActualFile($staticFilePath = $sitePath.$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)
{
$dynamicCandidates = [
$this->asActualFile($sitePath, $uri),
$this->asPhpIndexFileInDirectory($sitePath, $uri),
$this->asHtmlIndexFileInDirectory($sitePath, $uri),
];
foreach ($dynamicCandidates as $candidate) {
if ($this->isActualFile($candidate)) {
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = str_replace($sitePath, '', $candidate);
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
return $candidate;
}
}
$fixedCandidatesAndDocroots = [
$this->asRootPhpIndexFile($sitePath) => $sitePath,
$this->asPublicPhpIndexFile($sitePath) => $sitePath . '/public',
$this->asPublicHtmlIndexFile($sitePath) => $sitePath . '/public',
];
foreach ($fixedCandidatesAndDocroots as $candidate => $docroot) {
if ($this->isActualFile($candidate)) {
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['DOCUMENT_ROOT'] = $docroot;
return $candidate;
}
}
}
/**
* Concatenate the site path and URI as a single file name.
*
* @param string $sitePath
* @param string $uri
* @return string
*/
protected function asActualFile($sitePath, $uri)
{
return $sitePath.$uri;
}
/**
* Format the site path and URI with a trailing "index.php".
*
* @param string $sitePath
* @param string $uri
* @return string
*/
protected function asPhpIndexFileInDirectory($sitePath, $uri)
{
return $sitePath.rtrim($uri, '/').'/index.php';
}
/**
* Format the site path and URI with a trailing "index.html".
*
* @param string $sitePath
* @param string $uri
* @return string
*/
protected function asHtmlIndexFileInDirectory($sitePath, $uri)
{
return $sitePath.rtrim($uri, '/').'/index.html';
}
/**
* Format the incoming site path as root "index.php" file path.
*
* @param string $sitePath
* @return string
*/
protected function asRootPhpIndexFile($sitePath)
{
return $sitePath.'/index.php';
}
/**
* Format the incoming site path as a "public/index.php" file path.
*
* @param string $sitePath
* @return string
*/
protected function asPublicPhpIndexFile($sitePath)
{
return $sitePath.'/public/index.php';
}
/**
* Format the incoming site path as a "public/index.php" file path.
*
* @param string $sitePath
* @return string
*/
protected function asPublicHtmlIndexFile($sitePath)
{
return $sitePath.'/public/index.html';
}
}