Last active
October 29, 2019 11:43
-
-
Save spinsch/df02253e49fa7230b37bd2050647fa52 to your computer and use it in GitHub Desktop.
Laravel Valet Driver for Magento 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Magento 1 Valet Driver | |
* @author spinsch | |
* @path ~/.valet/Drivers/Magento1ValetDriver.php | |
*/ | |
class Magento1ValetDriver extends ValetDriver | |
{ | |
/** | |
* 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.'/js/mage/translate.js'); | |
} | |
/** | |
* 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) | |
{ | |
if ($this->isActualFile($staticFilePath = $sitePath.$uri)) { | |
return $staticFilePath; | |
} | |
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) | |
{ | |
// api request | |
if (substr($uri, 1, 8) == 'api/rest') { | |
$_GET['type'] = 'rest'; | |
return $sitePath.'/api.php'; | |
} | |
// default request | |
$dynamicCandidates = [ | |
$this->asActualFile($sitePath, $uri), | |
$this->asPhpIndexFileInDirectory($sitePath, $uri), | |
$this->asHtmlIndexFileInDirectory($sitePath, $uri), | |
]; | |
foreach ($dynamicCandidates as $candidate) { | |
if ($this->isActualFile($candidate)) { | |
$_SERVER['SCRIPT_FILENAME'] = $candidate; | |
$_SERVER['SCRIPT_NAME'] = str_replace($sitePath, '', $candidate); | |
$_SERVER['DOCUMENT_ROOT'] = $sitePath; | |
return $candidate; | |
} | |
} | |
$candidate = $this->asRootPhpIndexFile($sitePath); | |
if ($this->isActualFile($candidate)) { | |
$_SERVER['SCRIPT_FILENAME'] = $candidate; | |
$_SERVER['SCRIPT_NAME'] = '/index.php'; | |
$_SERVER['DOCUMENT_ROOT'] = $sitePath; | |
return $candidate; | |
} | |
} | |
/** | |
* Concatenate the site path and URI as a single file name. | |
* | |
* @param string $sitePath | |
* @param string $uri | |
* @return string | |
*/ | |
protected function asActualFile($sitePath, $uri) | |
{ | |
return $sitePath.$uri; | |
} | |
/** | |
* Format the site path and URI with a trailing "index.php". | |
* | |
* @param string $sitePath | |
* @param string $uri | |
* @return string | |
*/ | |
protected function asPhpIndexFileInDirectory($sitePath, $uri) | |
{ | |
return $sitePath.rtrim($uri, '/').'/index.php'; | |
} | |
/** | |
* Format the site path and URI with a trailing "index.html". | |
* | |
* @param string $sitePath | |
* @param string $uri | |
* @return string | |
*/ | |
protected function asHtmlIndexFileInDirectory($sitePath, $uri) | |
{ | |
return $sitePath.rtrim($uri, '/').'/index.html'; | |
} | |
/** | |
* Format the incoming site path as root "index.php" file path. | |
* | |
* @param string $sitePath | |
* @return string | |
*/ | |
protected function asRootPhpIndexFile($sitePath) | |
{ | |
return $sitePath.'/index.php'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! I tried a number to get Magento working as a subfolder within a WordPress root installation through valet. This is the only one that worked. I just added '/shop/index.php' to line 19, 52, 75 and 126.