Skip to content

Release v4.0.0 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions cli/Valet/Drivers/BasicValetDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Valet\Drivers;

class BasicValetDriver 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 true;
}


/**
* Take any steps necessary before loading the front controller for this driver.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return void
*/
public function beforeLoading($sitePath, $siteName, $uri) {
$_SERVER['PHP_SELF'] = $uri;
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
}

/**
* 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 (file_exists($staticFilePath = $sitePath . rtrim($uri, '/') . '/index.html')) {
return $staticFilePath;
}
elseif ($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) {
$uri = rtrim($uri, '/');

$candidates = [
$sitePath . $uri,
$sitePath . "$uri/index.php",
"$sitePath/index.php",
"$sitePath/index.html"
];

foreach ($candidates as $candidate) {
if ($this->isActualFile($candidate)) {
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = str_replace($sitePath, '', $candidate);
$_SERVER['DOCUMENT_ROOT'] = $sitePath;

return $candidate;
}
}

return null;
}
}
77 changes: 77 additions & 0 deletions cli/Valet/Drivers/BasicWithPublicValetDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Valet\Drivers;

class BasicWithPublicValetDriver 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 is_dir("$sitePath/public/");
}

/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return bool|string
*/
public function isStaticFile($sitePath, $siteName, $uri) {
$publicPath = "$sitePath/public/" . trim($uri, '/');

if ($this->isActualFile($publicPath)) {
return $publicPath;
}
elseif (file_exists("$publicPath/index.html")) {
return "$publicPath/index.html";
}

return false;
}

/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return string|null
*/
public function frontControllerPath($sitePath, $siteName, $uri) {
$_SERVER['PHP_SELF'] = $uri;
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];

$docRoot = "$sitePath/public";
$uri = rtrim($uri, '/');

$candidates = [
$docRoot . $uri,
$docRoot . "$uri/index.php",
"$docRoot/index.php",
"$docRoot/index.html"
];

foreach ($candidates as $candidate) {
if ($this->isActualFile($candidate)) {
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = str_replace("$sitePath/public", '', $candidate);
$_SERVER['DOCUMENT_ROOT'] = "$sitePath/public";

return $candidate;
}
}

return null;
}
}
79 changes: 79 additions & 0 deletions cli/Valet/Drivers/LaravelValetDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Valet\Drivers;

class LaravelValetDriver 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/public/index.php") && file_exists("$sitePath/artisan");
}

/**
* Take any steps necessary before loading the front controller for this driver.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return void
*/
public function beforeLoading($sitePath, $siteName, $uri) {
// Shortcut for getting the "local" hostname as the HTTP_HOST, especially when
// proxied or using 'share'
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
}

/**
* 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 (file_exists($staticFilePath = "$sitePath/public$uri") && is_file($staticFilePath)) {
return $staticFilePath;
}

$storageUri = $uri;

if (strpos($uri, '/storage/') === 0) {
$storageUri = substr($uri, 8);
}

if ($this->isActualFile($storagePath = "$sitePath/storage/app/public$storageUri")) {
return $storagePath;
}

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) {
if (file_exists($staticFilePath = "$sitePath/public$uri") && $this->isActualFile($staticFilePath)) {
return $staticFilePath;
}

return "$sitePath/public/index.php";
}
}
88 changes: 88 additions & 0 deletions cli/Valet/Drivers/Specific/BedrockValetDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Valet\Drivers\Specific;

use Valet\Drivers\BasicValetDriver;

class BedrockValetDriver extends BasicValetDriver {
/**
* 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/web/app/mu-plugins/bedrock-autoloader.php") || (is_dir("$sitePath/web/app/") && file_exists("$sitePath/web/wp-config.php") && file_exists("$sitePath/config/application.php"));
}

/**
* Take any steps necessary before loading the front controller for this driver.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return void
*/
public function beforeLoading($sitePath, $siteName, $uri) {
$_SERVER['PHP_SELF'] = $uri;
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
}

/**
* 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) {
$staticFilePath = "$sitePath/web$uri";

if ($this->isActualFile($staticFilePath)) {
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) {
if (strpos($uri, '/wp/') === 0) {
return is_dir("$sitePath/web$uri")
? "$sitePath/web" . $this->forceTrailingSlash($uri) . '/index.php'
: "$sitePath/web$uri";
}

return "$sitePath/web/index.php";
}

/**
* Redirect to uri with trailing slash.
*
* @param string $uri
*
* @return string
*/
private function forceTrailingSlash($uri) {
if (substr($uri, -1 * strlen('/wp/wp-admin')) == '/wp/wp-admin') {
header("Location: $uri/");
exit;
}

return $uri;
}
}
Loading