forked from weprovide/valet-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Magento2ValetDriver.php
176 lines (144 loc) · 5.49 KB
/
Magento2ValetDriver.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
class Magento2ValetDriver extends ValetDriver
{
public function configure($devtools, $url) {
info('Configuring Magento 2...');
$devtools->cli->quietlyAsUser('chmod +x bin/magento');
$sitePath = getcwd();
if(!$this->envExists($sitePath)) {
info('env.php missing. Installing default env.php...');
$devtools->files->putAsUser(
$sitePath.'/app/etc/env.php',
str_replace(
'DBNAME',
$devtools->mysql->getDirName(),
$devtools->files->get(__DIR__.'/../stubs/magento2/env.php')
)
);
}
if(!$this->moduleConfigExists($sitePath)) {
info('config.php missing. Enabling all modules...');
$devtools->cli->quietlyAsUser('bin/magento module:enable --all');
}
info('Setting base url...');
$devtools->cli->quietlyAsUser('n98-magerun2 config:store:set web/unsecure/base_url ' . $url . '/');
$devtools->cli->quietlyAsUser('n98-magerun2 config:store:set web/secure/base_url ' . $url . '/');
info('Setting elastic search hostname...');
$devtools->cli->quietlyAsUser('n98-magerun2 config:store:set catalog/search/elasticsearch_server_hostname 127.0.0.1');
info('Enabling URL rewrites...');
$devtools->cli->quietlyAsUser('n98-magerun2 config:store:set web/seo/use_rewrites 1');
info('Flushing cache...');
$devtools->cli->quietlyAsUser('n98-magerun2 cache:flush');
info('Configured Magento 2');
}
/**
* 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 . '/pub/index.php') &&
file_exists($sitePath . '/bin/magento');
}
public function envExists($sitePath) {
return file_exists($sitePath.'/app/etc/env.php');
}
public function moduleConfigExists($sitePath) {
return file_exists($sitePath.'/app/etc/config.php');
}
public function installed($sitePath) {
return $this->envExists($sitePath) && $this->moduleConfigExists($sitePath);
}
/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string|false
*/
public function isStaticFile($sitePath, $siteName, $uri)
{
$isMagentoStatic = false;
$resource = $uri;
if(strpos($uri,'/errors') === 0 && file_exists($sitePath.'/pub'.$uri)) {
return $sitePath.'/pub'.$uri;
}
if(strpos($uri,'/pub') === 0 && file_exists($sitePath.'/setup'.$uri)) {
return $sitePath.'/setup'.$uri;
}
if (strpos($uri, '/static/') !== false) {
$isMagentoStatic = true;
}
if (!$isMagentoStatic && strpos($uri, '/media/') === false) {
return false;
}
if ($isMagentoStatic) {
$resource = preg_replace('#static(/version[0-9]+)?/#', '', $uri, 1);
$uri = '/static' . $resource;
}
if (strpos($uri, '/js-translation.json') === false && file_exists($staticFilePath = $sitePath . '/pub' . $uri)) {
return $staticFilePath;
}
if(strpos($uri, '/js-translation.json') !== false) {
header('Cache-Control: no-store, must-revalidate');
}
if (strpos($uri, '/static/') === 0) {
$_GET['resource'] = $resource;
include($sitePath . DIRECTORY_SEPARATOR . 'pub' . DIRECTORY_SEPARATOR . 'static.php');
exit;
}
if (strpos($uri, '/media/') === 0) {
include($sitePath . DIRECTORY_SEPARATOR . 'pub' . DIRECTORY_SEPARATOR . 'get.php');
exit;
}
return false;
}
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
public function frontControllerPath($sitePath, $siteName, $uri)
{
if(isset($_GET['profile'])) {
$_SERVER['MAGE_PROFILER'] = 'html';
}
if(strpos($uri, '/errors') === 0) {
$file = $sitePath . '/pub' . $uri;
if (file_exists($file)) {
return $file;
}
return $sitePath . '/pub/errors/404.php';
}
if($uri === '/setup') {
Header('HTTP/1.1 301 Moved Permanently');
Header('Location: http://' . $_SERVER['HTTP_HOST'] . $uri . '/');
die;
}
if(strpos($uri, '/setup') === 0) {
$_SERVER['SCRIPT_FILENAME'] = $sitePath.'/setup/index.php';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['DOCUMENT_ROOT'] = $sitePath.'/setup/';
$_SERVER['REQUEST_URI'] = str_replace('/setup', '', $_SERVER['REQUEST_URI']);
if($_SERVER['REQUEST_URI'] === '') {
$_SERVER['REQUEST_URI'] = '/';
}
return $sitePath.'/setup/index.php';
}
if(!$this->installed($sitePath)) {
http_response_code(404);
require __DIR__.'/../templates/magento2.php';
exit;
}
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
return $sitePath . '/pub/index.php';
}
}