mirror of
https://github.com/laravel/valet.git
synced 2026-02-07 17:10:05 +01:00
swap to driver based setup
This commit is contained in:
51
drivers/LaravelValetDriver.php
Normal file
51
drivers/LaravelValetDriver.php
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class LaravelValetDriver extends ValetDriver
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the driver serves the request.
|
||||||
|
*
|
||||||
|
* @param string $sitePath
|
||||||
|
* @param string $siteName
|
||||||
|
* @param string $uri
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function serves($sitePath, $siteName, $uri)
|
||||||
|
{
|
||||||
|
return file_exists($sitePath.'/public/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)
|
||||||
|
{
|
||||||
|
if (file_exists($staticFilePath = $sitePath.'/public/'.$uri)) {
|
||||||
|
return $staticFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists($sitePath.'/storage/public/'.$uri)) {
|
||||||
|
return $sitePath.'/public/'.$uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
return $sitePath.'/public/index.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
59
drivers/StatamicValetDriver.php
Normal file
59
drivers/StatamicValetDriver.php
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class StatamicValetDriver extends ValetDriver
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the driver serves the request.
|
||||||
|
*
|
||||||
|
* @param string $sitePath
|
||||||
|
* @param string $siteName
|
||||||
|
* @param string $uri
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function serves($sitePath, $siteName, $uri)
|
||||||
|
{
|
||||||
|
return is_dir($sitePath.'/statamic');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 (strpos($uri, '/site') === 0 && strpos($uri, '/site/themes') !== 0) {
|
||||||
|
return false;
|
||||||
|
} elseif (strpos($uri, '/local') === 0 || strpos($uri, '/statamic') === 0) {
|
||||||
|
return false;
|
||||||
|
} elseif (file_exists($staticFilePath = $sitePath.'/'.$uri)) {
|
||||||
|
return $staticFilePath;
|
||||||
|
} elseif (file_exists($staticFilePath = $sitePath.'/public/'.$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)
|
||||||
|
{
|
||||||
|
if (file_exists($indexPath = $sitePath.'/index.php')) {
|
||||||
|
return $indexPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists($indexPath = $sitePath.'/public/index.php')) {
|
||||||
|
return $indexPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
97
drivers/ValetDriver.php
Normal file
97
drivers/ValetDriver.php
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
abstract class ValetDriver
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the driver serves the request.
|
||||||
|
*
|
||||||
|
* @param string $sitePath
|
||||||
|
* @param string $siteName
|
||||||
|
* @param string $uri
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
abstract public function serves($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
|
||||||
|
*/
|
||||||
|
abstract public function isStaticFile($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
|
||||||
|
*/
|
||||||
|
abstract public function frontControllerPath($sitePath, $siteName, $uri);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a driver that can serve the incoming request.
|
||||||
|
*
|
||||||
|
* @param string $sitePath
|
||||||
|
* @param string $siteName
|
||||||
|
* @param string $uri
|
||||||
|
* @return ValetDriver|null
|
||||||
|
*/
|
||||||
|
public static function assign($sitePath, $siteName, $uri)
|
||||||
|
{
|
||||||
|
$drivers = static::driversIn(VALET_HOME_PATH.'/Drivers');
|
||||||
|
|
||||||
|
$drivers[] = 'StatamicValetDriver';
|
||||||
|
$drivers[] = 'LaravelValetDriver';
|
||||||
|
|
||||||
|
foreach ($drivers as $driver) {
|
||||||
|
$driver = new $driver;
|
||||||
|
|
||||||
|
if ($driver->serves($sitePath, $siteName, $uri)) {
|
||||||
|
return $driver;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all of the driver classes in a given path.
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function driversIn($path)
|
||||||
|
{
|
||||||
|
$drivers = [];
|
||||||
|
|
||||||
|
foreach (scandir($path) as $file) {
|
||||||
|
if ($file !== 'ValetDriver.php' && strpos($file, 'ValetDriver') !== false) {
|
||||||
|
require_once $path.'/'.$file;
|
||||||
|
|
||||||
|
$drivers[] = basename($file, '.php');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $drivers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serve the static file at the given path.
|
||||||
|
*
|
||||||
|
* @param string $staticFilePath
|
||||||
|
* @param string $sitePath
|
||||||
|
* @param string $siteName
|
||||||
|
* @param string $uri
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
|
||||||
|
{
|
||||||
|
$mimes = require(__DIR__.'/../mimes.php');
|
||||||
|
|
||||||
|
header('Content-Type: '.$mimes[pathinfo($uri)['extension']]);
|
||||||
|
|
||||||
|
readfile($staticFilePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
114
server.php
114
server.php
@@ -1,102 +1,82 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the user's "~/.valet" path.
|
||||||
|
*/
|
||||||
|
define('VALET_HOME_PATH', '/Users/'.posix_getpwuid(fileowner(__FILE__))['name'].'/.valet');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the Valet 404 "Not Found" page.
|
||||||
|
*/
|
||||||
|
function show_valet_404()
|
||||||
|
{
|
||||||
|
http_response_code(404);
|
||||||
|
require __DIR__.'/404.html';
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the Valet configuration.
|
* Load the Valet configuration.
|
||||||
*/
|
*/
|
||||||
$GLOBALS['VALET'] = json_decode(
|
$valetConfig = json_decode(
|
||||||
file_get_contents('/Users/'.posix_getpwuid(fileowner(__FILE__))['name'].'/.valet/config.json'), true
|
file_get_contents(VALET_HOME_PATH.'/config.json'), true
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the URI and host for the incoming request.
|
* Parse the URI and site / host for the incoming request.
|
||||||
*/
|
*/
|
||||||
$uri = urldecode(
|
$uri = urldecode(
|
||||||
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
|
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
|
||||||
);
|
);
|
||||||
|
|
||||||
$site = basename(
|
$siteName = basename(
|
||||||
$_SERVER['HTTP_HOST'],
|
$_SERVER['HTTP_HOST'],
|
||||||
'.'.$GLOBALS['VALET']['domain']
|
'.'.$valetConfig['domain']
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the fully qualified path to the site.
|
* Determine the fully qualified path to the site.
|
||||||
*/
|
*/
|
||||||
foreach ($GLOBALS['VALET']['paths'] as $path) {
|
$valetSitePath = null;
|
||||||
if (is_dir($path.'/'.$site)) {
|
|
||||||
define('VALET_SITE_PATH', $path.'/'.$site);
|
foreach ($valetConfig['paths'] as $path) {
|
||||||
|
if (is_dir($path.'/'.$siteName)) {
|
||||||
|
$valetSitePath = $path.'/'.$siteName;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! defined('VALET_SITE_PATH')) {
|
if (is_null($valetSitePath)) {
|
||||||
return require __DIR__.'/404.html';
|
show_valet_404();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the site is a Statamic site.
|
* Find the appropriate Valet driver for the request.
|
||||||
*/
|
*/
|
||||||
if (is_dir(VALET_SITE_PATH.'/statamic')) {
|
$valetDriver = null;
|
||||||
require __DIR__.'/servers/statamic.php';
|
|
||||||
|
|
||||||
exit;
|
require_once __DIR__.'/drivers/ValetDriver.php';
|
||||||
|
require_once __DIR__.'/drivers/StatamicValetDriver.php';
|
||||||
|
require_once __DIR__.'/drivers/LaravelValetDriver.php';
|
||||||
|
|
||||||
|
$valetDriver = ValetDriver::assign($valetSitePath, $siteName, $uri);
|
||||||
|
|
||||||
|
if (! $valetDriver) {
|
||||||
|
show_valet_404();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the given URI is a static file.
|
* Dispatch the request.
|
||||||
*
|
|
||||||
* @param string $site
|
|
||||||
* @param string $uri
|
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
function is_static_file($site, $uri)
|
if ($uri !== '/' && $staticFilePath = $valetDriver->isStaticFile($valetSitePath, $siteName, $uri)) {
|
||||||
{
|
return $valetDriver->serveStaticFile($staticFilePath, $valetSitePath, $siteName, $uri);
|
||||||
if ($uri === '/') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file_exists(VALET_SITE_PATH.'/public'.$uri)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
$frontControllerPath = $valetDriver->frontControllerPath(
|
||||||
* Serve a static file by URI.
|
$valetSitePath, $siteName, $uri
|
||||||
*
|
);
|
||||||
* @param string $site
|
|
||||||
* @param string $uri
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
function serve_file($site, $uri)
|
|
||||||
{
|
|
||||||
$mimes = require(__DIR__.'/mimes.php');
|
|
||||||
|
|
||||||
header('Content-Type: '.$mimes[pathinfo($uri)['extension']]);
|
posix_setuid(fileowner($frontControllerPath));
|
||||||
|
|
||||||
if (file_exists(VALET_SITE_PATH.'/public'.$uri)) {
|
require $frontControllerPath;
|
||||||
readfile(VALET_SITE_PATH.'/public'.$uri);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dispatch to the given site's Laravel installation.
|
|
||||||
*/
|
|
||||||
function dispatch($site)
|
|
||||||
{
|
|
||||||
if (file_exists($indexPath = VALET_SITE_PATH.'/public/index.php')) {
|
|
||||||
posix_setuid(fileowner($indexPath));
|
|
||||||
|
|
||||||
return require_once $indexPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
http_response_code(404);
|
|
||||||
|
|
||||||
require __DIR__.'/404.html';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serve the request.
|
|
||||||
*/
|
|
||||||
is_static_file($site, $uri) ? serve_file($site, $uri) : dispatch($site);
|
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the given URI is a static file.
|
|
||||||
*
|
|
||||||
* @param string $site
|
|
||||||
* @param string $uri
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
function is_static_statamic_file($site, $uri)
|
|
||||||
{
|
|
||||||
if ($uri === '/') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strpos($uri, '/site') === 0 && strpos($uri, '/site/themes') !== 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strpos($uri, '/local') === 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strpos($uri, '/statamic') === 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file_exists(VALET_SITE_PATH.'/'.$uri) ||
|
|
||||||
file_exists(VALET_SITE_PATH.'/public/'.$uri)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serve a static file by URI.
|
|
||||||
*
|
|
||||||
* @param string $site
|
|
||||||
* @param string $uri
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
function serve_statamic_file($site, $uri)
|
|
||||||
{
|
|
||||||
$mimes = require(__DIR__.'/../mimes.php');
|
|
||||||
|
|
||||||
header('Content-Type: '.$mimes[pathinfo($uri)['extension']]);
|
|
||||||
|
|
||||||
if (file_exists($path = VALET_SITE_PATH.'/'.$uri) ||
|
|
||||||
file_exists($path = VALET_SITE_PATH.'/public/'.$uri)) {
|
|
||||||
readfile($path);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serve the request for static assets.
|
|
||||||
*/
|
|
||||||
if (is_static_statamic_file($site, $uri)) {
|
|
||||||
serve_statamic_file($site, $uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serve the request to the front controller.
|
|
||||||
*/
|
|
||||||
if (file_exists($indexPath = VALET_SITE_PATH.'/index.php') ||
|
|
||||||
file_exists($indexPath = VALET_SITE_PATH.'/public/index.php')) {
|
|
||||||
posix_setuid(fileowner($indexPath));
|
|
||||||
|
|
||||||
return require_once $indexPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
http_response_code(404);
|
|
||||||
|
|
||||||
require __DIR__.'/../404.html';
|
|
||||||
@@ -17,6 +17,12 @@ public static function install()
|
|||||||
chown($directory, $_SERVER['SUDO_USER']);
|
chown($directory, $_SERVER['SUDO_USER']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (! is_dir($driversDirectory = $_SERVER['HOME'].'/.valet/Drivers')) {
|
||||||
|
mkdir($driversDirectory, 0755);
|
||||||
|
|
||||||
|
chown($driversDirectory, $_SERVER['SUDO_USER']);
|
||||||
|
}
|
||||||
|
|
||||||
if (! file_exists(static::path())) {
|
if (! file_exists(static::path())) {
|
||||||
static::write(['domain' => 'dev', 'paths' => []]);
|
static::write(['domain' => 'dev', 'paths' => []]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user