Skip to content

Commit 7cedd61

Browse files
authored
Merge pull request #401 from jasonvarga/master
Statamic caching and localization support
2 parents f21b823 + d12eb0e commit 7cedd61

File tree

1 file changed

+82
-13
lines changed

1 file changed

+82
-13
lines changed

cli/drivers/StatamicValetDriver.php

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,99 @@ public function isStaticFile($sitePath, $siteName, $uri)
4848
*/
4949
public function frontControllerPath($sitePath, $siteName, $uri)
5050
{
51-
if (file_exists($staticPath = $sitePath.'/static'.$uri.'/index.html')) {
51+
if ($this->isActualFile($staticPath = $this->getStaticPath($sitePath))) {
5252
return $staticPath;
5353
}
5454

55-
$_SERVER['SCRIPT_NAME'] = '/index.php';
56-
57-
if (strpos($_SERVER['REQUEST_URI'], '/index.php') === 0) {
58-
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 10);
55+
if ($uri === '/installer.php') {
56+
return $sitePath.'/installer.php';
5957
}
6058

61-
if ($uri === '') {
62-
$uri = '/';
59+
$scriptName = '/index.php';
60+
61+
if ($this->isActualFile($sitePath.'/index.php')) {
62+
$indexPath = $sitePath.'/index.php';
6363
}
6464

65-
if ($uri === '/installer.php') {
66-
return $sitePath.'/installer.php';
65+
if ($isAboveWebroot = $this->isActualFile($sitePath.'/public/index.php')) {
66+
$indexPath = $sitePath.'/public/index.php';
6767
}
6868

69-
if (file_exists($indexPath = $sitePath.'/index.php')) {
70-
return $indexPath;
69+
$sitePathPrefix = ($isAboveWebroot) ? $sitePath.'/public' : $sitePath;
70+
71+
if ($locale = $this->getUriLocale($uri)) {
72+
if ($this->isActualFile($localeIndexPath = $sitePathPrefix . '/' . $locale . '/index.php')) {
73+
// Force trailing slashes on locale roots.
74+
if ($uri === '/' . $locale) {
75+
header('Location: ' . $uri . '/');
76+
die;
77+
}
78+
79+
$indexPath = $localeIndexPath;
80+
$scriptName = '/' . $locale . '/index.php';
81+
}
7182
}
7283

73-
if (file_exists($indexPath = $sitePath.'/public/index.php')) {
74-
return $indexPath;
84+
$_SERVER['SCRIPT_NAME'] = $scriptName;
85+
$_SERVER['SCRIPT_FILENAME'] = $sitePathPrefix . $scriptName;
86+
87+
return $indexPath;
88+
}
89+
90+
/**
91+
* Get the locale from this URI
92+
*
93+
* @param string $uri
94+
* @return string|null
95+
*/
96+
public function getUriLocale($uri)
97+
{
98+
$parts = explode('/', $uri);
99+
$locale = $parts[1];
100+
101+
if (count($parts) < 2 || ! in_array($locale, $this->getLocales())) {
102+
return;
75103
}
104+
105+
return $locale;
106+
}
107+
108+
/**
109+
* Get the list of possible locales used in the first segment of a URI
110+
*
111+
* @return array
112+
*/
113+
public function getLocales()
114+
{
115+
return [
116+
'af', 'ax', 'al', 'dz', 'as', 'ad', 'ao', 'ai', 'aq', 'ag', 'ar', 'am', 'aw', 'au', 'at', 'az', 'bs', 'bh',
117+
'bd', 'bb', 'by', 'be', 'bz', 'bj', 'bm', 'bt', 'bo', 'bq', 'ba', 'bw', 'bv', 'br', 'io', 'bn', 'bg', 'bf',
118+
'bi', 'cv', 'kh', 'cm', 'ca', 'ky', 'cf', 'td', 'cl', 'cn', 'cx', 'cc', 'co', 'km', 'cg', 'cd', 'ck', 'cr',
119+
'ci', 'hr', 'cu', 'cw', 'cy', 'cz', 'dk', 'dj', 'dm', 'do', 'ec', 'eg', 'sv', 'gq', 'er', 'ee', 'et', 'fk',
120+
'fo', 'fj', 'fi', 'fr', 'gf', 'pf', 'tf', 'ga', 'gm', 'ge', 'de', 'gh', 'gi', 'gr', 'gl', 'gd', 'gp', 'gu',
121+
'gt', 'gg', 'gn', 'gw', 'gy', 'ht', 'hm', 'va', 'hn', 'hk', 'hu', 'is', 'in', 'id', 'ir', 'iq', 'ie', 'im',
122+
'il', 'it', 'jm', 'jp', 'je', 'jo', 'kz', 'ke', 'ki', 'kp', 'kr', 'kw', 'kg', 'la', 'lv', 'lb', 'ls', 'lr',
123+
'ly', 'li', 'lt', 'lu', 'mo', 'mk', 'mg', 'mw', 'my', 'mv', 'ml', 'mt', 'mh', 'mq', 'mr', 'mu', 'yt', 'mx',
124+
'fm', 'md', 'mc', 'mn', 'me', 'ms', 'ma', 'mz', 'mm', 'na', 'nr', 'np', 'nl', 'nc', 'nz', 'ni', 'ne', 'ng',
125+
'nu', 'nf', 'mp', 'no', 'om', 'pk', 'pw', 'ps', 'pa', 'pg', 'py', 'pe', 'ph', 'pn', 'pl', 'pt', 'pr', 'qa',
126+
're', 'ro', 'ru', 'rw', 'bl', 'sh', 'kn', 'lc', 'mf', 'pm', 'vc', 'ws', 'sm', 'st', 'sa', 'sn', 'rs', 'sc',
127+
'sl', 'sg', 'sx', 'sk', 'si', 'sb', 'so', 'za', 'gs', 'ss', 'es', 'lk', 'sd', 'sr', 'sj', 'sz', 'se', 'ch',
128+
'sy', 'tw', 'tj', 'tz', 'th', 'tl', 'tg', 'tk', 'to', 'tt', 'tn', 'tr', 'tm', 'tc', 'tv', 'ug', 'ua', 'ae',
129+
'gb', 'us', 'um', 'uy', 'uz', 'vu', 've', 'vn', 'vg', 'vi', 'wf', 'eh', 'ye', 'zm', 'zw',
130+
];
131+
}
132+
133+
/**
134+
* Get the path to a statically cached page
135+
*
136+
* @param string $sitePath
137+
* @return string
138+
*/
139+
protected function getStaticPath($sitePath)
140+
{
141+
$parts = parse_url($_SERVER['REQUEST_URI']);
142+
$query = isset($parts['query']) ? $parts['query'] : '';
143+
144+
return $sitePath . '/static' . $parts['path'] . '_' . $query . '.html';
76145
}
77146
}

0 commit comments

Comments
 (0)