Skip to content

Commit 108bbf7

Browse files
authored
Fix magento2 driver (#1420)
+ simplify, let mode determine by Magento env config
1 parent 68528ba commit 108bbf7

File tree

1 file changed

+13
-105
lines changed

1 file changed

+13
-105
lines changed

cli/Valet/Drivers/Specific/Magento2ValetDriver.php

Lines changed: 13 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -7,137 +7,45 @@
77
class Magento2ValetDriver extends ValetDriver
88
{
99
/**
10-
* Holds the MAGE_MODE from app/etc/config.php or $ENV.
11-
*
12-
* Can't be correctly typed yet because PHP 7.3.
13-
*
14-
* @param string|null
15-
*/
16-
private $mageMode = null;
17-
18-
/**
19-
* Determine if the driver serves the request.
10+
* @inheritdoc
2011
*/
2112
public function serves(string $sitePath, string $siteName, string $uri): bool
2213
{
2314
return file_exists($sitePath.'/bin/magento') && file_exists($sitePath.'/pub/index.php');
2415
}
2516

2617
/**
27-
* Determine if the incoming request is for a static file.
18+
* @inheritdoc
2819
*/
2920
public function isStaticFile(string $sitePath, string $siteName, string $uri)/*: string|false */
3021
{
31-
$this->checkMageMode($sitePath);
32-
33-
$uri = $this->handleForVersions($uri);
34-
$route = parse_url(substr($uri, 1))['path'];
35-
36-
$pub = '';
37-
if ($this->mageMode === 'developer') {
38-
$pub = 'pub/';
39-
}
40-
41-
if (! $this->isPubDirectory($sitePath, $route, $pub)) {
42-
return false;
43-
}
44-
45-
$magentoPackagePubDir = $sitePath;
46-
if ($this->mageMode !== 'developer') {
47-
$magentoPackagePubDir .= '/pub';
48-
}
49-
50-
$file = $magentoPackagePubDir.'/'.$route;
22+
$uri = preg_replace('/^\/static(\/version[\d]+)/', '/static', $uri);
5123

52-
if (file_exists($file)) {
53-
return $magentoPackagePubDir.$uri;
24+
if (file_exists($staticFilePath = $sitePath . '/pub' . $uri)) {
25+
return $staticFilePath;
5426
}
5527

56-
if (strpos($route, $pub.'static/') === 0) {
57-
$route = preg_replace('#'.$pub.'static/#', '', $route, 1);
58-
$_GET['resource'] = $route;
59-
include $magentoPackagePubDir.'/'.$pub.'static.php';
28+
if (strpos($uri, '/static/') === 0) {
29+
$_GET['resource'] = preg_replace('#static/#', '', $uri, 1);
30+
include($sitePath . '/pub/static.php');
6031
exit;
6132
}
6233

63-
if (strpos($route, $pub.'media/') === 0) {
64-
include $magentoPackagePubDir.'/'.$pub.'get.php';
34+
if (strpos($uri, '/media/') === 0) {
35+
include($sitePath . '/pub/get.php');
6536
exit;
6637
}
6738

6839
return false;
6940
}
7041

7142
/**
72-
* Rewrite URLs that look like "versions12345/" to remove
73-
* the versions12345/ part.
74-
*/
75-
private function handleForVersions($route): string
76-
{
77-
return preg_replace('/version\d*\//', '', $route);
78-
}
79-
80-
/**
81-
* Determine the current MAGE_MODE.
82-
*/
83-
private function checkMageMode($sitePath): void
84-
{
85-
if ($this->mageMode !== null) {
86-
// We have already figure out mode, no need to check it again
87-
return;
88-
}
89-
90-
if (! file_exists($sitePath.'/index.php')) {
91-
$this->mageMode = 'production'; // Can't use developer mode without index.php in project root
92-
93-
return;
94-
}
95-
96-
$mageConfig = [];
97-
98-
if (file_exists($sitePath.'/app/etc/env.php')) {
99-
$mageConfig = require $sitePath.'/app/etc/env.php';
100-
}
101-
102-
if (array_key_exists('MAGE_MODE', $mageConfig)) {
103-
$this->mageMode = $mageConfig['MAGE_MODE'];
104-
}
105-
}
106-
107-
/**
108-
* Checks to see if route is referencing any directory inside pub. This is a dynamic check so that if any new
109-
* directories are added to pub this driver will not need to be updated.
110-
*/
111-
private function isPubDirectory($sitePath, $route, $pub = ''): bool
112-
{
113-
$sitePath .= '/pub/';
114-
$dirs = glob($sitePath.'*', GLOB_ONLYDIR);
115-
116-
$dirs = str_replace($sitePath, '', $dirs);
117-
118-
foreach ($dirs as $dir) {
119-
if (strpos($route, $pub.$dir.'/') === 0) {
120-
return true;
121-
}
122-
}
123-
124-
return false;
125-
}
126-
127-
/**
128-
* Get the fully resolved path to the application's front controller.
43+
* @inheritdoc
12944
*/
13045
public function frontControllerPath(string $sitePath, string $siteName, string $uri): ?string
13146
{
132-
$this->checkMageMode($sitePath);
133-
134-
if ($this->mageMode === 'developer') {
135-
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
136-
137-
return $sitePath.'/index.php';
138-
}
139-
140-
$_SERVER['DOCUMENT_ROOT'] = $sitePath.'/pub';
47+
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
48+
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
14149

14250
return $sitePath.'/pub/index.php';
14351
}

0 commit comments

Comments
 (0)