Skip to content

Instantly share code, notes, and snippets.

@chaoticgeek
Forked from davidalger/LocalValetDriver.php
Created July 12, 2019 22:06
Show Gist options
  • Save chaoticgeek/a77c6e2c188e788be9c6034f7d357e64 to your computer and use it in GitHub Desktop.
Save chaoticgeek/a77c6e2c188e788be9c6034f7d357e64 to your computer and use it in GitHub Desktop.
Valet+ driver for running Wordpress in a sub-directory of a Magento2 installation
<?php
/**
* Custom driver for Valet+ to support running Wordpress installation at /wp/
*
* @author David Alger
*/
class LocalValetDriver extends Magento2ValetDriver
{
/**
* Path to Wordpress installation
*/
const WORDPRESS_PATH = '/wp';
/** @var WordPressValetDriver */
protected $wordpressDriver = null;
/**
* Loads driver dependencies
*/
public function __construct()
{
$this->wordpressDriver = new WordPressValetDriver();
}
/**
* 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 parent::serves($sitePath, $siteName, $uri) || $this->wordpressDriver->serves($sitePath, $siteName, $uri);
}
/**
* 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 ($this->isWordpressUri($uri)) {
$candidate = $this->wordpressDriver->frontControllerPath($sitePath, $siteName, $uri);
if (stripos($candidate, $sitePath.self::WORDPRESS_PATH.'/') === false) {
$candidate = $sitePath.self::WORDPRESS_PATH.'/index.php';
$_SERVER['SCRIPT_NAME'] = self::WORDPRESS_PATH.'/index.php';
$_SERVER['SCRIPT_FILENAME'] = $candidate;
}
return $candidate;
}
return parent::frontControllerPath($sitePath, $siteName, $uri);
}
/**
* 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->isWordpressUri($uri)) {
return $this->wordpressDriver->isStaticFile($sitePath, $siteName, $uri);
}
return parent::isStaticFile($sitePath, $siteName, $uri);
}
/**
* Determine if URI is a Wordpress directory or not
*
* @param string $uri
* @return bool
*/
public function isWordpressUri($uri)
{
return stripos($uri, self::WORDPRESS_PATH.'/') === 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment