mirror of
https://github.com/laravel/valet.git
synced 2026-02-04 16:10:08 +01:00
Fix magento2 driver (#1420)
+ simplify, let mode determine by Magento env config
This commit is contained in:
@@ -7,16 +7,7 @@
|
||||
class Magento2ValetDriver extends ValetDriver
|
||||
{
|
||||
/**
|
||||
* Holds the MAGE_MODE from app/etc/config.php or $ENV.
|
||||
*
|
||||
* Can't be correctly typed yet because PHP 7.3.
|
||||
*
|
||||
* @param string|null
|
||||
*/
|
||||
private $mageMode = null;
|
||||
|
||||
/**
|
||||
* Determine if the driver serves the request.
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function serves(string $sitePath, string $siteName, string $uri): bool
|
||||
{
|
||||
@@ -24,44 +15,24 @@ public function serves(string $sitePath, string $siteName, string $uri): bool
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the incoming request is for a static file.
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function isStaticFile(string $sitePath, string $siteName, string $uri)/*: string|false */
|
||||
{
|
||||
$this->checkMageMode($sitePath);
|
||||
$uri = preg_replace('/^\/static(\/version[\d]+)/', '/static', $uri);
|
||||
|
||||
$uri = $this->handleForVersions($uri);
|
||||
$route = parse_url(substr($uri, 1))['path'];
|
||||
|
||||
$pub = '';
|
||||
if ($this->mageMode === 'developer') {
|
||||
$pub = 'pub/';
|
||||
if (file_exists($staticFilePath = $sitePath . '/pub' . $uri)) {
|
||||
return $staticFilePath;
|
||||
}
|
||||
|
||||
if (! $this->isPubDirectory($sitePath, $route, $pub)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$magentoPackagePubDir = $sitePath;
|
||||
if ($this->mageMode !== 'developer') {
|
||||
$magentoPackagePubDir .= '/pub';
|
||||
}
|
||||
|
||||
$file = $magentoPackagePubDir.'/'.$route;
|
||||
|
||||
if (file_exists($file)) {
|
||||
return $magentoPackagePubDir.$uri;
|
||||
}
|
||||
|
||||
if (strpos($route, $pub.'static/') === 0) {
|
||||
$route = preg_replace('#'.$pub.'static/#', '', $route, 1);
|
||||
$_GET['resource'] = $route;
|
||||
include $magentoPackagePubDir.'/'.$pub.'static.php';
|
||||
if (strpos($uri, '/static/') === 0) {
|
||||
$_GET['resource'] = preg_replace('#static/#', '', $uri, 1);
|
||||
include($sitePath . '/pub/static.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
if (strpos($route, $pub.'media/') === 0) {
|
||||
include $magentoPackagePubDir.'/'.$pub.'get.php';
|
||||
if (strpos($uri, '/media/') === 0) {
|
||||
include($sitePath . '/pub/get.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -69,76 +40,13 @@ public function isStaticFile(string $sitePath, string $siteName, string $uri)/*:
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite URLs that look like "versions12345/" to remove
|
||||
* the versions12345/ part.
|
||||
*/
|
||||
private function handleForVersions($route): string
|
||||
{
|
||||
return preg_replace('/version\d*\//', '', $route);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the current MAGE_MODE.
|
||||
*/
|
||||
private function checkMageMode($sitePath): void
|
||||
{
|
||||
if ($this->mageMode !== null) {
|
||||
// We have already figure out mode, no need to check it again
|
||||
return;
|
||||
}
|
||||
|
||||
if (! file_exists($sitePath.'/index.php')) {
|
||||
$this->mageMode = 'production'; // Can't use developer mode without index.php in project root
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$mageConfig = [];
|
||||
|
||||
if (file_exists($sitePath.'/app/etc/env.php')) {
|
||||
$mageConfig = require $sitePath.'/app/etc/env.php';
|
||||
}
|
||||
|
||||
if (array_key_exists('MAGE_MODE', $mageConfig)) {
|
||||
$this->mageMode = $mageConfig['MAGE_MODE'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if route is referencing any directory inside pub. This is a dynamic check so that if any new
|
||||
* directories are added to pub this driver will not need to be updated.
|
||||
*/
|
||||
private function isPubDirectory($sitePath, $route, $pub = ''): bool
|
||||
{
|
||||
$sitePath .= '/pub/';
|
||||
$dirs = glob($sitePath.'*', GLOB_ONLYDIR);
|
||||
|
||||
$dirs = str_replace($sitePath, '', $dirs);
|
||||
|
||||
foreach ($dirs as $dir) {
|
||||
if (strpos($route, $pub.$dir.'/') === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the fully resolved path to the application's front controller.
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function frontControllerPath(string $sitePath, string $siteName, string $uri): ?string
|
||||
{
|
||||
$this->checkMageMode($sitePath);
|
||||
|
||||
if ($this->mageMode === 'developer') {
|
||||
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
|
||||
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
|
||||
|
||||
return $sitePath.'/index.php';
|
||||
}
|
||||
|
||||
$_SERVER['DOCUMENT_ROOT'] = $sitePath.'/pub';
|
||||
|
||||
return $sitePath.'/pub/index.php';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user