Created
February 5, 2017 13:10
-
-
Save kayintveen/45ca3eb9c2c5d7f2ec2f60273b3b6b46 to your computer and use it in GitHub Desktop.
Laravel Valet Magento 2 driver that helps me daily in installing local Magento 2 development locations with Nginx and Mysql. For more info about how to use this, checkout our blog where we wrote an article about it http://www.microdesign.nl/blog/
This file contains hidden or 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 | |
/** | |
* Laravel Valet Magento 2 Driver | |
* Originally written bysschlein | |
* | |
* Sligthly altered by: | |
* Kay in 't Veen - Microdesign B.V. | |
* http://www.microdesign.nl | |
* | |
*/ | |
class Magento2ValetDriver extends ValetDriver | |
{ | |
/** | |
* Holds the MAGE_MODE from app/etc/config.php or $ENV | |
* | |
* @var string | |
*/ | |
private $mageMode; | |
/** | |
* Determine if the driver serves the request. | |
* | |
* @param string $sitePath | |
* @param string $siteName | |
* @param string $uri | |
* @return boolean | |
*/ | |
public function serves($sitePath, $siteName, $uri) | |
{ | |
return file_exists($sitePath . '/bin/magento') && file_exists($sitePath . '/pub/index.php'); | |
} | |
/** | |
* 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) | |
{ | |
$this->checkMageMode($sitePath); | |
$route = parse_url(substr($uri, 1))['path']; | |
$pub = ''; | |
if ('developer' === $this->mageMode) { | |
$pub = 'pub/'; | |
} | |
if (!$this->isPubDirectory($sitePath, $route, $pub)) { | |
return false; | |
} | |
$magentoPackagePubDir = $sitePath; | |
if ('developer' !== $this->mageMode) { | |
$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'; | |
exit; | |
} | |
if (strpos($route, $pub . 'media/') === 0) { | |
include $magentoPackagePubDir . '/' . $pub . 'get.php'; | |
exit; | |
} | |
return false; | |
} | |
/** | |
* Determine the current MAGE_MODE | |
* | |
* @param string $sitePath | |
*/ | |
private function checkMageMode($sitePath) | |
{ | |
if (null !== $this->mageMode) { | |
// 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. | |
* | |
* @param string $sitePath | |
* @param string $route | |
* @param string $pub | |
* @return bool | |
*/ | |
private function isPubDirectory($sitePath, $route, $pub = '') | |
{ | |
$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. | |
* | |
* @param string $sitePath | |
* @param string $siteName | |
* @param string $uri | |
* @return string | |
*/ | |
public function frontControllerPath($sitePath, $siteName, $uri) | |
{ | |
$this->checkMageMode($sitePath); | |
if ('developer' === $this->mageMode) { | |
return $sitePath . '/index.php'; | |
} | |
return $sitePath . '/pub/index.php'; | |
} | |
} |
Keep getting this error?
Fatal error: Cannot declare class Magento2ValetDriver, because the name is already in use in /Users/fishtank/.valet/Drivers/Magento2ValetDriver.php on line 149
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any issues with getting shipping rates on Valet 2 with Magento 2?