forked from weprovide/valet-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrupalValetDriver.php
127 lines (107 loc) · 3.49 KB
/
DrupalValetDriver.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
<?php
class DrupalValetDriver extends ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serves($sitePath, $siteName, $uri)
{
/**
* /misc/drupal.js = Drupal 7
* /core/lib/Drupal.php = Drupal 8
*/
if (file_exists($sitePath . '/misc/drupal.js') ||
file_exists($sitePath . '/core/lib/Drupal.php')) {
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($sitePath . $uri) &&
!is_dir($sitePath . $uri) &&
pathinfo($sitePath . $uri)['extension'] != 'php') {
return $sitePath . $uri;
}
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)
{
if (isset($_GET['q']) === false && !empty($uri) && $uri !== '/') {
$_GET['q'] = $uri;
}
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
$matches = [];
if (preg_match('/^\/(.*?)\.php/', $uri, $matches)) {
$filename = $matches[0];
if (file_exists($sitePath . $filename) && !is_dir($sitePath . $filename)) {
$_SERVER['SCRIPT_FILENAME'] = $sitePath . $filename;
$_SERVER['SCRIPT_NAME'] = $filename;
return $sitePath . $filename;
}
}
// Fallback
$_SERVER['SCRIPT_FILENAME'] = $sitePath . '/index.php';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Check for multisite folder structure
if (file_exists($sitePath . '/sites/sites.php')) {
include $sitePath . '/sites/sites.php';
if (!isset($sites)) {
return $sitePath . '/index.php';
}
$sites = array_keys($sites);
$urlPath = $siteName . '.' . $GLOBALS['valetConfig']['domain'];
$subfolder = explode('/', $uri)[1];
$isInUrlPath = in_array($urlPath, $sites);
$isInSubFolderPath = in_array($subfolder, $sites);
if ($isInUrlPath === false && $isInSubFolderPath === false) {
return $sitePath . '/index.php';
}
if ($isInSubFolderPath) {
$this->forceTrailingSlash($uri);
}
$subfolder = '/' . explode('/', $uri)[1];
if ($isInSubFolderPath) {
$_SERVER['SCRIPT_NAME'] = $subfolder . '/index.php';
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
}
$_SERVER['SCRIPT_FILENAME'] = $sitePath . $subfolder . '/index.php';
unset($_SERVER['DOCUMENT_URI']);
}
return $sitePath . '/index.php';
}
/**
* Redirect to uri with trailing slash.
*
* @param string $uri
* @return string
*/
private function forceTrailingSlash($uri)
{
if (substr($uri, -1) !== '/') {
header('Location: ' . $uri . '/');
die;
}
return $uri;
}
}