Skip to content

Commit ff0af00

Browse files
committed
refactor: add beforeLoading hook to simplify the frontControllerPath method for some drivers
- Add `beforeLoading` method. - Move most `$_SERVER` variables for many drivers into the new method. - Remove the now unused `$sitename` param from the `serverScript` method in `Typo3ValetDriver`.
1 parent 861a02d commit ff0af00

12 files changed

+172
-46
lines changed

cli/Valet/Drivers/BasicValetDriver.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ public function serves($sitePath, $siteName, $uri) {
1616
return true;
1717
}
1818

19+
20+
/**
21+
* Take any steps necessary before loading the front controller for this driver.
22+
*
23+
* @param string $sitePath
24+
* @param string $siteName
25+
* @param string $uri
26+
*
27+
* @return void
28+
*/
29+
public function beforeLoading($sitePath, $siteName, $uri) {
30+
$_SERVER['PHP_SELF'] = $uri;
31+
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
32+
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
33+
}
34+
1935
/**
2036
* Determine if the incoming request is for a static file.
2137
*
@@ -46,10 +62,6 @@ public function isStaticFile($sitePath, $siteName, $uri) {
4662
* @return string
4763
*/
4864
public function frontControllerPath($sitePath, $siteName, $uri) {
49-
$_SERVER['PHP_SELF'] = $uri;
50-
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
51-
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
52-
5365
$uri = rtrim($uri, '/');
5466

5567
$candidates = [

cli/Valet/Drivers/LaravelValetDriver.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ public function serves($sitePath, $siteName, $uri) {
1616
return file_exists("$sitePath/public/index.php") && file_exists("$sitePath/artisan");
1717
}
1818

19+
/**
20+
* Take any steps necessary before loading the front controller for this driver.
21+
*
22+
* @param string $sitePath
23+
* @param string $siteName
24+
* @param string $uri
25+
*
26+
* @return void
27+
*/
28+
public function beforeLoading($sitePath, $siteName, $uri) {
29+
// Shortcut for getting the "local" hostname as the HTTP_HOST, especially when
30+
// proxied or using 'share'
31+
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
32+
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
33+
}
34+
}
35+
1936
/**
2037
* Determine if the incoming request is for a static file.
2138
*
@@ -53,10 +70,6 @@ public function isStaticFile($sitePath, $siteName, $uri) {
5370
* @return string
5471
*/
5572
public function frontControllerPath($sitePath, $siteName, $uri) {
56-
// Shortcut for getting the "local" hostname as the HTTP_HOST
57-
if (isset($_SERVER['HTTP_X_ORIGINAL_HOST'], $_SERVER['HTTP_X_FORWARDED_HOST'])) {
58-
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
59-
}
6073

6174
return "$sitePath/public/index.php";
6275
}

cli/Valet/Drivers/Specific/BedrockValetDriver.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ public function serves($sitePath, $siteName, $uri) {
1818
return file_exists("$sitePath/web/app/mu-plugins/bedrock-autoloader.php") || (is_dir("$sitePath/web/app/") && file_exists("$sitePath/web/wp-config.php") && file_exists("$sitePath/config/application.php"));
1919
}
2020

21+
/**
22+
* Take any steps necessary before loading the front controller for this driver.
23+
*
24+
* @param string $sitePath
25+
* @param string $siteName
26+
* @param string $uri
27+
*
28+
* @return void
29+
*/
30+
public function beforeLoading($sitePath, $siteName, $uri) {
31+
$_SERVER['PHP_SELF'] = $uri;
32+
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
33+
}
34+
2135
/**
2236
* Determine if the incoming request is for a static file.
2337
*
@@ -47,9 +61,6 @@ public function isStaticFile($sitePath, $siteName, $uri) {
4761
* @return string
4862
*/
4963
public function frontControllerPath($sitePath, $siteName, $uri) {
50-
$_SERVER['PHP_SELF'] = $uri;
51-
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
52-
5364
if (strpos($uri, '/wp/') === 0) {
5465
return is_dir("$sitePath/web$uri")
5566
? "$sitePath/web" . $this->forceTrailingSlash($uri) . '/index.php'
@@ -74,4 +85,4 @@ private function forceTrailingSlash($uri) {
7485

7586
return $uri;
7687
}
77-
}
88+
}

cli/Valet/Drivers/Specific/CakeValetDriver.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ public function serves($sitePath, $siteName, $uri) {
1818
return file_exists("{$sitePath}/bin/cake");
1919
}
2020

21+
/**
22+
* Take any steps necessary before loading the front controller for this driver.
23+
*
24+
* @param string $sitePath
25+
* @param string $siteName
26+
* @param string $uri
27+
*
28+
* @return void
29+
*/
30+
public function beforeLoading($sitePath, $siteName, $uri) {
31+
$_SERVER['DOCUMENT_ROOT'] = "{$sitePath}/webroot";
32+
$_SERVER['SCRIPT_FILENAME'] = "{$sitePath}/webroot/index.php";
33+
$_SERVER['SCRIPT_NAME'] = '/index.php';
34+
$_SERVER['PHP_SELF'] = '/index.php';
35+
}
36+
2137
/**
2238
* Determine if the incoming request is for a static file.
2339
*
@@ -45,11 +61,6 @@ public function isStaticFile($sitePath, $siteName, $uri) {
4561
* @return string
4662
*/
4763
public function frontControllerPath($sitePath, $siteName, $uri) {
48-
$_SERVER['DOCUMENT_ROOT'] = "{$sitePath}/webroot";
49-
$_SERVER['SCRIPT_FILENAME'] = "{$sitePath}/webroot/index.php";
50-
$_SERVER['SCRIPT_NAME'] = '/index.php';
51-
$_SERVER['PHP_SELF'] = '/index.php';
52-
5364
return "{$sitePath}/webroot/index.php";
5465
}
5566
}

cli/Valet/Drivers/Specific/JoomlaValetDriver.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ public function serves($sitePath, $siteName, $uri) {
1818
return is_dir("{$sitePath}/libraries/joomla");
1919
}
2020

21+
/**
22+
* Take any steps necessary before loading the front controller for this driver.
23+
*
24+
* @param string $sitePath
25+
* @param string $siteName
26+
* @param string $uri
27+
* @return void
28+
*/
29+
public function beforeLoading($sitePath, $siteName, $uri) {
30+
$_SERVER['PHP_SELF'] = $uri;
31+
}
32+
2133
/**
2234
* Get the fully resolved path to the application's front controller.
2335
*
@@ -28,8 +40,6 @@ public function serves($sitePath, $siteName, $uri) {
2840
* @return string
2941
*/
3042
public function frontControllerPath($sitePath, $siteName, $uri) {
31-
$_SERVER['PHP_SELF'] = $uri;
32-
3343
return parent::frontControllerPath($sitePath, $siteName, $uri);
3444
}
3545
}

cli/Valet/Drivers/Specific/NeosValetDriver.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ public function serves($sitePath, $siteName, $uri) {
1818
return file_exists("{$sitePath}/flow") && is_dir("{$sitePath}/Web");
1919
}
2020

21+
/**
22+
* Take any steps necessary before loading the front controller for this driver.
23+
*
24+
* @param string $sitePath
25+
* @param string $siteName
26+
* @param string $uri
27+
* @return void
28+
*/
29+
public function beforeLoading($sitePath, $siteName, $uri) {
30+
putenv('FLOW_CONTEXT=Development');
31+
putenv('FLOW_REWRITEURLS=1');
32+
$_SERVER['SCRIPT_FILENAME'] = "{$sitePath}/Web/index.php";
33+
$_SERVER['SCRIPT_NAME'] = '/index.php';
34+
}
35+
2136
/**
2237
* Determine if the incoming request is for a static file.
2338
*
@@ -45,11 +60,6 @@ public function isStaticFile($sitePath, $siteName, $uri) {
4560
* @return string
4661
*/
4762
public function frontControllerPath($sitePath, $siteName, $uri) {
48-
putenv('FLOW_CONTEXT=Development');
49-
putenv('FLOW_REWRITEURLS=1');
50-
$_SERVER['SCRIPT_FILENAME'] = "{$sitePath}/Web/index.php";
51-
$_SERVER['SCRIPT_NAME'] = '/index.php';
52-
5363
return "{$sitePath}/Web/index.php";
5464
}
5565
}

cli/Valet/Drivers/Specific/NetteValetDriver.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ public function serves($sitePath, $siteName, $uri): bool {
2121
file_exists("{$sitePath}/config/services.neon");
2222
}
2323

24+
/**
25+
* Take any steps necessary before loading the front controller for this driver.
26+
*
27+
* @param string $sitePath
28+
* @param string $siteName
29+
* @param string $uri
30+
* @return void
31+
*/
32+
public function beforeLoading($sitePath, $siteName, $uri) {
33+
$_SERVER['DOCUMENT_ROOT'] = "{$sitePath}/www";
34+
$_SERVER['SCRIPT_FILENAME'] = "{$sitePath}/www/index.php";
35+
$_SERVER['SCRIPT_NAME'] = '/index.php';
36+
$_SERVER['PHP_SELF'] = '/index.php';
37+
}
38+
2439
/**
2540
* Determine if the incoming request is for a static file.
2641
*
@@ -48,11 +63,6 @@ public function isStaticFile($sitePath, $siteName, $uri) {
4863
* @return string
4964
*/
5065
public function frontControllerPath($sitePath, $siteName, $uri) {
51-
$_SERVER['DOCUMENT_ROOT'] = "{$sitePath}/www";
52-
$_SERVER['SCRIPT_FILENAME'] = "{$sitePath}/www/index.php";
53-
$_SERVER['SCRIPT_NAME'] = '/index.php';
54-
$_SERVER['PHP_SELF'] = '/index.php';
55-
5666
return "{$sitePath}/www/index.php";
5767
}
5868
}

cli/Valet/Drivers/Specific/RadicleValetDriver.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ public function serves($sitePath, $siteName, $uri) {
2020
file_exists("{$sitePath}/bedrock/application.php");
2121
}
2222

23+
/**
24+
* Take any steps necessary before loading the front controller for this driver.
25+
*
26+
* @param string $sitePath
27+
* @param string $siteName
28+
* @param string $uri
29+
* @return void
30+
*/
31+
public function beforeLoading($sitePath, $siteName, $uri) {
32+
$_SERVER['PHP_SELF'] = $uri;
33+
}
34+
2335
/**
2436
* Determine if the incoming request is for a static file.
2537
*
@@ -48,7 +60,6 @@ public function isStaticFile($sitePath, $siteName, $uri) {
4860
* @return string
4961
*/
5062
public function frontControllerPath($sitePath, $siteName, $uri) {
51-
$_SERVER['PHP_SELF'] = $uri;
5263
if (strpos($uri, '/wp/') === 0) {
5364
return is_dir("{$sitePath}/public{$uri}")
5465
? "{$sitePath}/public" . $this->forceTrailingSlash($uri) . '/index.php'

cli/Valet/Drivers/Specific/Typo3ValetDriver.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ public function serves($sitePath, $siteName, $uri) {
5656
return file_exists($typo3Dir) && is_dir($typo3Dir);
5757
}
5858

59+
/**
60+
* Take any steps necessary before loading the front controller for this driver.
61+
*
62+
* @param string $sitePath
63+
* @param string $siteName
64+
* @param string $uri
65+
* @return void
66+
*/
67+
public function beforeLoading($sitePath, $siteName, $uri) {
68+
// without modifying the URI, redirect if necessary
69+
$this->handleRedirectBackendShorthandUris($uri);
70+
71+
$_SERVER['SERVER_NAME'] = "{$siteName}.dev";
72+
$_SERVER['DOCUMENT_URI'] = $uri;
73+
$_SERVER['SCRIPT_NAME'] = $uri;
74+
$_SERVER['PHP_SELF'] = $uri;
75+
}
76+
5977
/**
6078
* Determine if the incoming request is for a static file. That is, it is
6179
* no PHP script file and the URI points to a valid file (no folder) on
@@ -115,9 +133,6 @@ private function isAccessAuthorized($uri) {
115133
* @return string
116134
*/
117135
public function frontControllerPath($sitePath, $siteName, $uri) {
118-
// without modifying the URI, redirect if necessary
119-
$this->handleRedirectBackendShorthandUris($uri);
120-
121136
// from now on, remove trailing / for convenience for all the following join operations
122137
$uri = rtrim($uri, '/');
123138

@@ -126,7 +141,7 @@ public function frontControllerPath($sitePath, $siteName, $uri) {
126141
if (is_dir($absoluteFilePath)) {
127142
if (file_exists("{$absoluteFilePath}/index.php")) {
128143
// this folder can be served by index.php
129-
return $this->serveScript($sitePath, $siteName, "{$uri}/index.php");
144+
return $this->serveScript($sitePath, "{$uri}/index.php");
130145
}
131146

132147
if (file_exists("{$absoluteFilePath}/index.html")) {
@@ -136,12 +151,12 @@ public function frontControllerPath($sitePath, $siteName, $uri) {
136151
}
137152
elseif (pathinfo($absoluteFilePath, PATHINFO_EXTENSION) === 'php') {
138153
// this file can be served directly
139-
return $this->serveScript($sitePath, $siteName, $uri);
154+
return $this->serveScript($sitePath, $uri);
140155
}
141156
}
142157

143158
// the global index.php will handle all other cases
144-
return $this->serveScript($sitePath, $siteName, '/index.php');
159+
return $this->serveScript($sitePath, '/index.php');
145160
}
146161

147162
/**
@@ -170,21 +185,16 @@ private function handleRedirectBackendShorthandUris($uri) {
170185
* the specified URI and returns it absolute file path.
171186
*
172187
* @param string $sitePath
173-
* @param string $siteName
174188
* @param string $uri
175189
*
176190
* @return string
177191
*/
178-
private function serveScript($sitePath, $siteName, $uri) {
192+
private function serveScript($sitePath, $uri) {
179193
$docroot = "{$sitePath}{$this->documentRoot}";
180194
$abspath = "{$docroot}{$uri}";
181195

182-
$_SERVER['SERVER_NAME'] = "{$siteName}.dev";
183196
$_SERVER['DOCUMENT_ROOT'] = $docroot;
184-
$_SERVER['DOCUMENT_URI'] = $uri;
185197
$_SERVER['SCRIPT_FILENAME'] = $abspath;
186-
$_SERVER['SCRIPT_NAME'] = $uri;
187-
$_SERVER['PHP_SELF'] = $uri;
188198

189199
return $abspath;
190200
}

cli/Valet/Drivers/Specific/WordPressValetDriver.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,29 @@ public function serves($sitePath, $siteName, $uri) {
1919
}
2020

2121
/**
22-
* Get the fully resolved path to the application's front controller.
22+
* Take any steps necessary before loading the front controller for this driver.
2323
*
2424
* @param string $sitePath
2525
* @param string $siteName
2626
* @param string $uri
27-
*
28-
* @return string
27+
* @return void
2928
*/
30-
public function frontControllerPath($sitePath, $siteName, $uri) {
29+
public function beforeLoading($sitePath, $siteName, $uri) {
3130
$_SERVER['PHP_SELF'] = $uri;
3231
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
3332
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
33+
}
3434

35+
/**
36+
* Get the fully resolved path to the application's front controller.
37+
*
38+
* @param string $sitePath
39+
* @param string $siteName
40+
* @param string $uri
41+
*
42+
* @return string
43+
*/
44+
public function frontControllerPath($sitePath, $siteName, $uri) {
3545
return parent::frontControllerPath(
3646
$sitePath,
3747
$siteName,

0 commit comments

Comments
 (0)