Skip to content
This repository was archived by the owner on Dec 9, 2019. It is now read-only.

Commit b346124

Browse files
authored
Merge pull request cpriego#92 from dakira/update-drivers
Sync ValetDrivers with upstream, add support for LocalValetDriver
2 parents 0c584e5 + 688d152 commit b346124

9 files changed

+452
-7
lines changed

cli/drivers/CakeValetDriver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public function isStaticFile($sitePath, $siteName, $uri)
4242
*/
4343
public function frontControllerPath($sitePath, $siteName, $uri)
4444
{
45+
$_SERVER['DOCUMENT_ROOT'] = $sitePath.'/webroot';
4546
$_SERVER['SCRIPT_FILENAME'] = $sitePath.'/webroot/index.php';
47+
$_SERVER['SCRIPT_NAME'] = '/index.php';
48+
$_SERVER['PHP_SELF'] = '/index.php';
4649

4750
return $sitePath.'/webroot/index.php';
4851
}

cli/drivers/CraftValetDriver.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ class CraftValetDriver extends ValetDriver
1212
*/
1313
public function serves($sitePath, $siteName, $uri)
1414
{
15-
return is_dir($sitePath.'/craft');
15+
return file_exists($sitePath.'/craft');
16+
}
17+
18+
/**
19+
* Determine the name of the directory where the front controller lives.
20+
*
21+
* @param string $sitePath
22+
* @return string
23+
*/
24+
public function frontControllerDirectory($sitePath)
25+
{
26+
return is_file($sitePath.'/craft') ? 'web' : 'public';
1627
}
1728

1829
/**
@@ -25,7 +36,9 @@ public function serves($sitePath, $siteName, $uri)
2536
*/
2637
public function isStaticFile($sitePath, $siteName, $uri)
2738
{
28-
if ($this->isActualFile($staticFilePath = $sitePath.'/public'.$uri)) {
39+
$frontControllerDirectory = $this->frontControllerDirectory($sitePath);
40+
41+
if ($this->isActualFile($staticFilePath = $sitePath.'/'.$frontControllerDirectory.$uri)) {
2942
return $staticFilePath;
3043
}
3144

@@ -42,8 +55,10 @@ public function isStaticFile($sitePath, $siteName, $uri)
4255
*/
4356
public function frontControllerPath($sitePath, $siteName, $uri)
4457
{
58+
$frontControllerDirectory = $this->frontControllerDirectory($sitePath);
59+
4560
// Default index path
46-
$indexPath = $sitePath.'/public/index.php';
61+
$indexPath = $sitePath.'/'.$frontControllerDirectory.'/index.php';
4762
$scriptName = '/index.php';
4863

4964
// Check if the first URL segment matches any of the defined locales
@@ -175,6 +190,8 @@ public function frontControllerPath($sitePath, $siteName, $uri)
175190
$_SERVER['SCRIPT_FILENAME'] = $indexPath;
176191
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
177192
$_SERVER['SCRIPT_NAME'] = $scriptName;
193+
$_SERVER['PHP_SELF'] = $scriptName;
194+
$_SERVER['DOCUMENT_ROOT'] = $sitePath.'/'.$frontControllerDirectory;
178195

179196
return $indexPath;
180197
}

cli/drivers/LaravelValetDriver.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public function serves($sitePath, $siteName, $uri)
2626
*/
2727
public function isStaticFile($sitePath, $siteName, $uri)
2828
{
29-
if (file_exists($staticFilePath = $sitePath.'/public'.$uri)) {
29+
if (file_exists($staticFilePath = $sitePath.'/public'.$uri)
30+
&& is_file($staticFilePath)) {
3031
return $staticFilePath;
3132
}
3233

@@ -53,6 +54,11 @@ public function isStaticFile($sitePath, $siteName, $uri)
5354
*/
5455
public function frontControllerPath($sitePath, $siteName, $uri)
5556
{
57+
// Shortcut for getting the "local" hostname as the HTTP_HOST
58+
if (isset($_SERVER['HTTP_X_ORIGINAL_HOST'], $_SERVER['HTTP_X_FORWARDED_HOST'])) {
59+
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
60+
}
61+
5662
return $sitePath.'/public/index.php';
5763
}
5864
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
class Magento2ValetDriver extends ValetDriver
4+
{
5+
6+
/**
7+
* Holds the MAGE_MODE from app/etc/config.php or $ENV
8+
*
9+
* @var string
10+
*/
11+
private $mageMode;
12+
13+
/**
14+
* Determine if the driver serves the request.
15+
*
16+
* @param string $sitePath
17+
* @param string $siteName
18+
* @param string $uri
19+
* @return boolean
20+
*/
21+
public function serves($sitePath, $siteName, $uri)
22+
{
23+
return file_exists($sitePath . '/bin/magento') && file_exists($sitePath . '/pub/index.php');
24+
}
25+
26+
/**
27+
* Determine if the incoming request is for a static file.
28+
*
29+
* @param string $sitePath
30+
* @param string $siteName
31+
* @param string $uri
32+
* @return string|false
33+
*/
34+
public function isStaticFile($sitePath, $siteName, $uri)
35+
{
36+
$this->checkMageMode($sitePath);
37+
38+
$uri = $this->handleForVersions($uri);
39+
$route = parse_url(substr($uri, 1))['path'];
40+
41+
$pub = '';
42+
if ('developer' === $this->mageMode) {
43+
$pub = 'pub/';
44+
}
45+
46+
if (!$this->isPubDirectory($sitePath, $route, $pub)) {
47+
return false;
48+
}
49+
50+
$magentoPackagePubDir = $sitePath;
51+
if ('developer' !== $this->mageMode) {
52+
$magentoPackagePubDir .= '/pub';
53+
}
54+
55+
$file = $magentoPackagePubDir . '/' . $route;
56+
57+
if (file_exists($file)) {
58+
return $magentoPackagePubDir . $uri;
59+
}
60+
61+
if (strpos($route, $pub . 'static/') === 0) {
62+
$route = preg_replace('#' . $pub . 'static/#', '', $route, 1);
63+
$_GET['resource'] = $route;
64+
include $magentoPackagePubDir . '/' . $pub . 'static.php';
65+
exit;
66+
}
67+
68+
if (strpos($route, $pub . 'media/') === 0) {
69+
include $magentoPackagePubDir . '/' . $pub . 'get.php';
70+
exit;
71+
}
72+
73+
return false;
74+
}
75+
76+
/**
77+
* Rewrite URLs that look like "versions12345/" to remove
78+
* the versions12345/ part
79+
*
80+
* @param string $route
81+
*/
82+
private function handleForVersions($route)
83+
{
84+
return preg_replace('/version\d*\//', '', $route);
85+
}
86+
87+
/**
88+
* Determine the current MAGE_MODE
89+
*
90+
* @param string $sitePath
91+
*/
92+
private function checkMageMode($sitePath)
93+
{
94+
if (null !== $this->mageMode) {
95+
// We have already figure out mode, no need to check it again
96+
return;
97+
}
98+
if (!file_exists($sitePath . '/index.php')) {
99+
$this->mageMode = 'production'; // Can't use developer mode without index.php in project root
100+
return;
101+
}
102+
$mageConfig = [];
103+
if (file_exists($sitePath . '/app/etc/env.php')) {
104+
$mageConfig = require $sitePath . '/app/etc/env.php';
105+
}
106+
if (array_key_exists('MAGE_MODE', $mageConfig)) {
107+
$this->mageMode = $mageConfig['MAGE_MODE'];
108+
}
109+
}
110+
111+
/**
112+
* Checks to see if route is referencing any directory inside pub. This is a dynamic check so that if any new
113+
* directories are added to pub this driver will not need to be updated.
114+
*
115+
* @param string $sitePath
116+
* @param string $route
117+
* @param string $pub
118+
* @return bool
119+
*/
120+
private function isPubDirectory($sitePath, $route, $pub = '')
121+
{
122+
$sitePath .= '/pub/';
123+
$dirs = glob($sitePath . '*', GLOB_ONLYDIR);
124+
125+
$dirs = str_replace($sitePath, '', $dirs);
126+
foreach ($dirs as $dir) {
127+
if (strpos($route, $pub . $dir . '/') === 0) {
128+
return true;
129+
}
130+
}
131+
return false;
132+
}
133+
134+
/**
135+
* Get the fully resolved path to the application's front controller.
136+
*
137+
* @param string $sitePath
138+
* @param string $siteName
139+
* @param string $uri
140+
* @return string
141+
*/
142+
public function frontControllerPath($sitePath, $siteName, $uri)
143+
{
144+
$this->checkMageMode($sitePath);
145+
146+
if ('developer' === $this->mageMode) {
147+
return $sitePath . '/index.php';
148+
}
149+
return $sitePath . '/pub/index.php';
150+
}
151+
}

cli/drivers/NeosValetDriver.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
class NeosValetDriver extends ValetDriver
4+
{
5+
/**
6+
* Determine if the driver serves the request.
7+
*
8+
* @param string $sitePath
9+
* @param string $siteName
10+
* @param string $uri
11+
* @return bool
12+
*/
13+
public function serves($sitePath, $siteName, $uri)
14+
{
15+
return file_exists($sitePath.'/flow') && is_dir($sitePath.'/Web');
16+
}
17+
18+
/**
19+
* Determine if the incoming request is for a static file.
20+
*
21+
* @param string $sitePath
22+
* @param string $siteName
23+
* @param string $uri
24+
* @return string|false
25+
*/
26+
public function isStaticFile($sitePath, $siteName, $uri)
27+
{
28+
if ($this->isActualFile($staticFilePath = $sitePath.'/Web'.$uri)) {
29+
return $staticFilePath;
30+
}
31+
return false;
32+
}
33+
34+
/**
35+
* Get the fully resolved path to the application's front controller.
36+
*
37+
* @param string $sitePath
38+
* @param string $siteName
39+
* @param string $uri
40+
* @return string
41+
*/
42+
public function frontControllerPath($sitePath, $siteName, $uri)
43+
{
44+
putenv('FLOW_CONTEXT=Development');
45+
putenv('FLOW_REWRITEURLS=1');
46+
$_SERVER['SCRIPT_FILENAME'] = $sitePath.'/Web/index.php';
47+
$_SERVER['SCRIPT_NAME'] = '/index.php';
48+
return $sitePath.'/Web/index.php';
49+
}
50+
}

cli/drivers/SymfonyValetDriver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class SymfonyValetDriver extends ValetDriver
1313
public function serves($sitePath, $siteName, $uri)
1414
{
1515
return (file_exists($sitePath.'/web/app_dev.php') || file_exists($sitePath.'/web/app.php')) &&
16-
(file_exists($sitePath.'/app/AppKernel.php'));
16+
(file_exists($sitePath.'/app/AppKernel.php')) || (file_exists($sitePath.'/web/index.php')) &&
17+
(file_exists($sitePath.'/src/Kernel.php'))
18+
;
1719
}
1820

1921
/**
@@ -47,6 +49,8 @@ public function frontControllerPath($sitePath, $siteName, $uri)
4749
return $frontControllerPath;
4850
} elseif (file_exists($frontControllerPath = $sitePath.'/web/app.php')) {
4951
return $frontControllerPath;
52+
} elseif (file_exists($frontControllerPath = $sitePath.'/web/index.php')) {
53+
return $frontControllerPath;
5054
}
5155
}
5256
}

0 commit comments

Comments
 (0)