forked from felipearosemena/wordpress-proxy-valet-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordPressProxyValetDriver.php
129 lines (116 loc) · 3.09 KB
/
WordPressProxyValetDriver.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
<?php
/**
* Class WordPressProxyValetDriver
* @author Jeff Sagal
* @package Briteweb
* @url https://github.com/Briteweb/wordpress-proxy-valet-driver
*
* This Valet Driver will proxy given file types to
* a production URL. This is useful for local dev -
* you don't need to worry about syncing the uploads
* folder to your local copy, it'll hit the production
* server for those files.
*
* Configuration is simple. Add `.uploads-proxy` to the
* WordPress root. The file should contain only one line:
* the URL of the site you need to proxy to.
*
* ex) https://briteweb.com
*
*/
class WordPressProxyValetDriver extends WordPressValetDriver
{
/**
* The file types that should be served
* through a proxy.
*
* @var array
*/
protected $proxyable = [
'png',
'jpg',
'jpeg',
'gif',
'ico',
'svg',
'webm',
'mp4',
];
/**
* The file to check for when determining
* whether to use this driver.
*
* Should contain a single line: the URL
* that proxy requests will be sent to.
*
* @var string
*/
protected $configFile = '.uploads-proxy';
/**
* 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/$this->configFile");
}
/**
* Determine if the incoming request is for a static file.
* Return early if it's going to be served through a proxy.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
public function isStaticFile($sitePath, $siteName, $uri)
{
if ($this->shouldProxy($uri) && ! $this->isActualFile($staticFilePath = $sitePath.$uri)) {
return true;
}
return parent::isStaticFile($sitePath, $siteName, $uri);
}
/**
* Serve the static file at the given path.
*
* @param string $staticFilePath
* @param string $sitePath
* @param string $siteName
* @param string $uri
*/
public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
{
if ($this->shouldProxy($uri) && !file_exists($staticFilePath)) {
$proxy = $this->getProxyUrl($sitePath);
header("Location: $proxy/$uri");
return;
}
parent::serveStaticFile($staticFilePath, $sitePath, $siteName, $uri);
}
/**
* Determine if the URI should be proxied.
*
* @param $uri
* @return bool
*/
public function shouldProxy($uri)
{
$extension = pathinfo($uri, PATHINFO_EXTENSION);
return in_array($extension, $this->proxyable);
}
/**
* Get the URL from the config file.
*
* @param $sitePath
* @return string
*/
protected function getProxyUrl($sitePath)
{
$proxy = rtrim(file_get_contents("$sitePath/$this->configFile"), '/');
return rtrim($proxy);
}
}