|
7 | 7 | class Magento2ValetDriver extends ValetDriver
|
8 | 8 | {
|
9 | 9 | /**
|
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 |
20 | 11 | */
|
21 | 12 | public function serves(string $sitePath, string $siteName, string $uri): bool
|
22 | 13 | {
|
23 | 14 | return file_exists($sitePath.'/bin/magento') && file_exists($sitePath.'/pub/index.php');
|
24 | 15 | }
|
25 | 16 |
|
26 | 17 | /**
|
27 |
| - * Determine if the incoming request is for a static file. |
| 18 | + * @inheritdoc |
28 | 19 | */
|
29 | 20 | public function isStaticFile(string $sitePath, string $siteName, string $uri)/*: string|false */
|
30 | 21 | {
|
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); |
51 | 23 |
|
52 |
| - if (file_exists($file)) { |
53 |
| - return $magentoPackagePubDir.$uri; |
| 24 | + if (file_exists($staticFilePath = $sitePath . '/pub' . $uri)) { |
| 25 | + return $staticFilePath; |
54 | 26 | }
|
55 | 27 |
|
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'); |
60 | 31 | exit;
|
61 | 32 | }
|
62 | 33 |
|
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'); |
65 | 36 | exit;
|
66 | 37 | }
|
67 | 38 |
|
68 | 39 | return false;
|
69 | 40 | }
|
70 | 41 |
|
71 | 42 | /**
|
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 |
129 | 44 | */
|
130 | 45 | public function frontControllerPath(string $sitePath, string $siteName, string $uri): ?string
|
131 | 46 | {
|
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; |
141 | 49 |
|
142 | 50 | return $sitePath.'/pub/index.php';
|
143 | 51 | }
|
|
0 commit comments