Skip to content

Instantly share code, notes, and snippets.

@arispati
Created March 6, 2024 06:27
Show Gist options
  • Save arispati/e2372119bb5e23299d736dd6250ce4ef to your computer and use it in GitHub Desktop.
Save arispati/e2372119bb5e23299d736dd6250ce4ef to your computer and use it in GitHub Desktop.
Valet driver for yii2
<?php
namespace Valet\Drivers\Custom;
use Valet\Drivers\ValetDriver;
class Yii2ValetDriver extends ValetDriver
{
/**
* Custom site path configurations
*
* @var array
*/
protected $sitePaths = [
// siteName => web path
];
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves(string $sitePath, string $siteName, string $uri): bool
{
// check if current framework is yii2
if (
file_exists($sitePath . '/yii') ||
file_exists($sitePath . '/vendor/yiisoft/yii2/Yii.php')
) {
return true;
}
return false;
}
/**
* 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(string $sitePath, string $siteName, string $uri)
{
$assetFile = sprintf('%s/web/%s', $sitePath, $uri);
if (isset($this->sitePaths[$siteName])) {
$assetFile = sprintf('%s/%s/%s', $sitePath, $this->sitePaths[$siteName], $uri);
}
if (file_exists($assetFile)) {
return $assetFile;
}
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(string $sitePath, string $siteName, string $uri): string
{
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
$frontIndex = $sitePath . '/web/index.php';
if (isset($this->sitePaths[$siteName])) {
$frontIndex = sprintf('%s/%s/%s', $sitePath, $this->sitePaths[$siteName], 'index.php');
}
$_SERVER['SCRIPT_FILENAME'] = $frontIndex;
return $frontIndex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment