diff --git a/cli/Valet/Drivers/BasicValetDriver.php b/cli/Valet/Drivers/BasicValetDriver.php new file mode 100644 index 000000000..bd7e887ff --- /dev/null +++ b/cli/Valet/Drivers/BasicValetDriver.php @@ -0,0 +1,86 @@ +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; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/BasicWithPublicValetDriver.php b/cli/Valet/Drivers/BasicWithPublicValetDriver.php new file mode 100644 index 000000000..f3ec119cc --- /dev/null +++ b/cli/Valet/Drivers/BasicWithPublicValetDriver.php @@ -0,0 +1,77 @@ +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; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/LaravelValetDriver.php b/cli/Valet/Drivers/LaravelValetDriver.php new file mode 100644 index 000000000..4cbfdafe3 --- /dev/null +++ b/cli/Valet/Drivers/LaravelValetDriver.php @@ -0,0 +1,79 @@ +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"; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/BedrockValetDriver.php b/cli/Valet/Drivers/Specific/BedrockValetDriver.php new file mode 100644 index 000000000..d19e45a25 --- /dev/null +++ b/cli/Valet/Drivers/Specific/BedrockValetDriver.php @@ -0,0 +1,88 @@ +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; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/CakeValetDriver.php b/cli/Valet/Drivers/Specific/CakeValetDriver.php new file mode 100644 index 000000000..d2370bd38 --- /dev/null +++ b/cli/Valet/Drivers/Specific/CakeValetDriver.php @@ -0,0 +1,66 @@ +isActualFile($staticFilePath = "{$sitePath}/webroot/{$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) { + return "{$sitePath}/webroot/index.php"; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/Concrete5ValetDriver.php b/cli/Valet/Drivers/Specific/Concrete5ValetDriver.php new file mode 100644 index 000000000..6d1b6bf21 --- /dev/null +++ b/cli/Valet/Drivers/Specific/Concrete5ValetDriver.php @@ -0,0 +1,52 @@ +isActualFile($staticFilePath = "{$sitePath}/web{$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 ($uri === '/install.php') { + return "{$sitePath}/web/install.php"; + } + + if (strncmp($uri, '/app_dev.php', 12) === 0) { + $_SERVER['SCRIPT_NAME'] = '/app_dev.php'; + $_SERVER['SCRIPT_FILENAME'] = "{$sitePath}/app_dev.php"; + + return "{$sitePath}/web/app_dev.php"; + } + + return "{$sitePath}/web/app.php"; + } +} \ No newline at end of file diff --git a/cli/drivers/CraftValetDriver.php b/cli/Valet/Drivers/Specific/CraftValetDriver.php similarity index 75% rename from cli/drivers/CraftValetDriver.php rename to cli/Valet/Drivers/Specific/CraftValetDriver.php index 8a26c4a5c..50f48264c 100644 --- a/cli/drivers/CraftValetDriver.php +++ b/cli/Valet/Drivers/Specific/CraftValetDriver.php @@ -1,48 +1,55 @@ frontControllerDirectory($sitePath); - if ($this->isActualFile($staticFilePath = $sitePath . '/' . $frontControllerDirectory . $uri)) { + if ($this->isActualFile($staticFilePath = "{$sitePath}/{$frontControllerDirectory}{$uri}")) { return $staticFilePath; } @@ -52,16 +59,16 @@ 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 + * @param string $sitePath + * @param string $siteName + * @param string $uri + * * @return string */ public function frontControllerPath($sitePath, $siteName, $uri) { $frontControllerDirectory = $this->frontControllerDirectory($sitePath); - // Default index path - $indexPath = $sitePath . '/' . $frontControllerDirectory . '/index.php'; + $indexPath = "{$sitePath}/{$frontControllerDirectory}/index.php"; $scriptName = '/index.php'; // Check if the first URL segment matches any of the defined locales @@ -186,12 +193,12 @@ public function frontControllerPath($sitePath, $siteName, $uri) { $parts = explode('/', $uri); if (count($parts) > 1 && in_array($parts[1], $locales)) { - $indexLocalizedPath = $sitePath . '/' . $frontControllerDirectory . '/' . $parts[1] . '/index.php'; + $indexLocalizedPath = "$sitePath/$frontControllerDirectory/{$parts[1]}/index.php"; // Check if index.php exists in the localized folder, this is optional in Craft 3 if (file_exists($indexLocalizedPath)) { $indexPath = $indexLocalizedPath; - $scriptName = '/' . $parts[1] . '/index.php'; + $scriptName = "/{$parts[1]}/index.php"; } } @@ -199,8 +206,8 @@ public function frontControllerPath($sitePath, $siteName, $uri) { $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; $_SERVER['SCRIPT_NAME'] = $scriptName; $_SERVER['PHP_SELF'] = $scriptName; - $_SERVER['DOCUMENT_ROOT'] = $sitePath . '/' . $frontControllerDirectory; + $_SERVER['DOCUMENT_ROOT'] = "$sitePath/$frontControllerDirectory"; return $indexPath; } -} +} \ No newline at end of file diff --git a/cli/drivers/DrupalValetDriver.php b/cli/Valet/Drivers/Specific/DrupalValetDriver.php similarity index 63% rename from cli/drivers/DrupalValetDriver.php rename to cli/Valet/Drivers/Specific/DrupalValetDriver.php index 3859521ae..606eb9f20 100644 --- a/cli/drivers/DrupalValetDriver.php +++ b/cli/Valet/Drivers/Specific/DrupalValetDriver.php @@ -1,40 +1,39 @@ addSubdirectory($sitePath); - /** - * /misc/drupal.js = Drupal 7 - * /core/lib/Drupal.php = Drupal 8. - */ - if (file_exists($sitePath . '/misc/drupal.js') || file_exists($sitePath . '/core/lib/Drupal.php')) { - return true; - } - return false; + return file_exists("{$sitePath}/misc/drupal.js") || file_exists("{$sitePath}/core/lib/Drupal.php"); } /** * Determine if the incoming request is for a static file. * - * @param string $sitePath - * @param string $siteName - * @param string $uri + * @param string $sitePath + * @param string $siteName + * @param string $uri + * * @return string|false */ public function isStaticFile($sitePath, $siteName, $uri) { $sitePath = $this->addSubdirectory($sitePath); - if (file_exists($sitePath . $uri) && !is_dir($sitePath . $uri) && pathinfo($sitePath . $uri)['extension'] != 'php') { - return $sitePath . $uri; + if (file_exists("{$sitePath}{$uri}") && !is_dir("{$sitePath}{$uri}") && pathinfo("{$sitePath}{$uri}")['extension'] != 'php') { + return "{$sitePath}{$uri}"; } return false; @@ -43,9 +42,10 @@ 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 + * @param string $sitePath + * @param string $siteName + * @param string $uri + * * @return string */ public function frontControllerPath($sitePath, $siteName, $uri) { @@ -58,27 +58,30 @@ public function frontControllerPath($sitePath, $siteName, $uri) { $matches = []; if (preg_match('/^\/(.*?)\.php/', $uri, $matches)) { $filename = $matches[0]; - if (file_exists($sitePath . $filename) && !is_dir($sitePath . $filename)) { - $_SERVER['SCRIPT_FILENAME'] = $sitePath . $filename; + if (file_exists("{$sitePath}{$filename}") && !is_dir("{$sitePath}{$filename}")) { + $_SERVER['SCRIPT_FILENAME'] = "{$sitePath}{$filename}"; $_SERVER['SCRIPT_NAME'] = $filename; - return $sitePath . $filename; + return "{$sitePath}{$filename}"; } } - // Fallback - $_SERVER['SCRIPT_FILENAME'] = $sitePath . '/index.php'; + $_SERVER['SCRIPT_FILENAME'] = "{$sitePath}/index.php"; $_SERVER['SCRIPT_NAME'] = '/index.php'; - return $sitePath . '/index.php'; + return "{$sitePath}/index.php"; } /** * Add any matching subdirectory to the site path. + * + * @param string $sitePath + * + * @return string */ public function addSubdirectory($sitePath) { $paths = array_map(function ($subDir) use ($sitePath) { - return "$sitePath/$subDir"; + return "{$sitePath}/{$subDir}"; }, $this->possibleSubdirectories()); $foundPaths = array_filter($paths, function ($path) { @@ -102,4 +105,4 @@ public function addSubdirectory($sitePath) { private function possibleSubdirectories() { return ['docroot', 'public', 'web']; } -} +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/JigsawValetDriver.php b/cli/Valet/Drivers/Specific/JigsawValetDriver.php new file mode 100644 index 000000000..8d3fc4c52 --- /dev/null +++ b/cli/Valet/Drivers/Specific/JigsawValetDriver.php @@ -0,0 +1,31 @@ +isActualFile($staticFilePath = "{$sitePath}{$uri}")) { + return $staticFilePath; + } + elseif ($this->isActualFile($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) { + $scriptName = '/index.php'; + + if ($this->isActualFile("$sitePath/index.php")) { + $indexPath = "$sitePath/index.php"; + } + + if ($isAboveWebroot = $this->isActualFile("$sitePath/public/index.php")) { + $indexPath = "$sitePath/public/index.php"; + } + + if (preg_match('/^\/panel/', $uri) && $this->isActualFile("$sitePath/panel/index.php")) { + $scriptName = '/panel/index.php'; + $indexPath = "$sitePath/panel/index.php"; + } + + $sitePathPrefix = ($isAboveWebroot) ? "$sitePath/public" : $sitePath; + + $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; + $_SERVER['SCRIPT_NAME'] = $scriptName; + $_SERVER['SCRIPT_FILENAME'] = "{$sitePathPrefix}{$scriptName}"; + + return $indexPath; + } +} \ No newline at end of file diff --git a/cli/drivers/Magento2ValetDriver.php b/cli/Valet/Drivers/Specific/Magento2ValetDriver.php similarity index 61% rename from cli/drivers/Magento2ValetDriver.php rename to cli/Valet/Drivers/Specific/Magento2ValetDriver.php index 97c0c4b5c..44792adfc 100644 --- a/cli/drivers/Magento2ValetDriver.php +++ b/cli/Valet/Drivers/Specific/Magento2ValetDriver.php @@ -1,5 +1,9 @@ mageMode) { @@ -91,7 +100,7 @@ private function checkMageMode($sitePath) { return; } - if (!file_exists($sitePath . '/index.php')) { + if (!file_exists("{$sitePath}/index.php")) { $this->mageMode = 'production'; // Can't use developer mode without index.php in project root return; @@ -99,8 +108,8 @@ private function checkMageMode($sitePath) { $mageConfig = []; - if (file_exists($sitePath . '/app/etc/env.php')) { - $mageConfig = require $sitePath . '/app/etc/env.php'; + if (file_exists("{$sitePath}/app/etc/env.php")) { + $mageConfig = require "{$sitePath}/app/etc/env.php"; } if (array_key_exists('MAGE_MODE', $mageConfig)) { @@ -109,21 +118,23 @@ private function checkMageMode($sitePath) { } /** - * 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. + * 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 * - * @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 = glob("{$sitePath}*", GLOB_ONLYDIR); $dirs = str_replace($sitePath, '', $dirs); foreach ($dirs as $dir) { - if (strpos($route, $pub . $dir . '/') === 0) { + if (strpos($route, "{$pub}{$dir}/") === 0) { return true; } } @@ -134,9 +145,10 @@ private function isPubDirectory($sitePath, $route, $pub = '') { /** * Get the fully resolved path to the application's front controller. * - * @param string $sitePath - * @param string $siteName - * @param string $uri + * @param string $sitePath + * @param string $siteName + * @param string $uri + * * @return string */ public function frontControllerPath($sitePath, $siteName, $uri) { @@ -145,11 +157,11 @@ public function frontControllerPath($sitePath, $siteName, $uri) { if ('developer' === $this->mageMode) { $_SERVER['DOCUMENT_ROOT'] = $sitePath; - return $sitePath . '/index.php'; + return "{$sitePath}/index.php"; } - $_SERVER['DOCUMENT_ROOT'] = $sitePath . '/pub'; + $_SERVER['DOCUMENT_ROOT'] = "{$sitePath}/pub"; - return $sitePath . '/pub/index.php'; + return "{$sitePath}/pub/index.php"; } -} +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/NeosValetDriver.php b/cli/Valet/Drivers/Specific/NeosValetDriver.php new file mode 100644 index 000000000..ff8176e33 --- /dev/null +++ b/cli/Valet/Drivers/Specific/NeosValetDriver.php @@ -0,0 +1,65 @@ +isActualFile($staticFilePath = "{$sitePath}/Web{$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) { + return "{$sitePath}/Web/index.php"; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/NetteValetDriver.php b/cli/Valet/Drivers/Specific/NetteValetDriver.php new file mode 100644 index 000000000..68c770247 --- /dev/null +++ b/cli/Valet/Drivers/Specific/NetteValetDriver.php @@ -0,0 +1,68 @@ +isActualFile($staticFilePath = "{$sitePath}/www/{$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) { + return "{$sitePath}/www/index.php"; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/RadicleValetDriver.php b/cli/Valet/Drivers/Specific/RadicleValetDriver.php new file mode 100644 index 000000000..554a32ad6 --- /dev/null +++ b/cli/Valet/Drivers/Specific/RadicleValetDriver.php @@ -0,0 +1,87 @@ +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}/public{$uri}") + ? "{$sitePath}/public" . $this->forceTrailingSlash($uri) . '/index.php' + : "{$sitePath}/public{$uri}"; + } + + return "{$sitePath}/public/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; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/SculpinValetDriver.php b/cli/Valet/Drivers/Specific/SculpinValetDriver.php new file mode 100644 index 000000000..a294207de --- /dev/null +++ b/cli/Valet/Drivers/Specific/SculpinValetDriver.php @@ -0,0 +1,53 @@ +isModernSculpinProject($sitePath) || $this->isLegacySculpinProject($sitePath); + } + + /** + * Determine if the project is a modern Sculpin project. + * + * @param string $sitePath + * + * @return bool + */ + private function isModernSculpinProject($sitePath) { + return is_dir("{$sitePath}/source") && is_dir("{$sitePath}/output_dev") && $this->composerRequires($sitePath, 'sculpin/sculpin'); + } + + /** + * Determine if the project is a legacy Sculpin project. + * + * @param string $sitePath + * + * @return bool + */ + private function isLegacySculpinProject($sitePath) { + return is_dir("{$sitePath}/.sculpin"); + } + + /** + * Mutate the incoming URI. + * + * @param string $uri + * + * @return string + */ + public function mutateUri($uri) { + return rtrim("/output_dev{$uri}", '/'); + } +} \ No newline at end of file diff --git a/cli/drivers/StatamicV1ValetDriver.php b/cli/Valet/Drivers/Specific/StatamicV1ValetDriver.php similarity index 64% rename from cli/drivers/StatamicV1ValetDriver.php rename to cli/Valet/Drivers/Specific/StatamicV1ValetDriver.php index c87755d2d..184415841 100644 --- a/cli/drivers/StatamicV1ValetDriver.php +++ b/cli/Valet/Drivers/Specific/StatamicV1ValetDriver.php @@ -1,24 +1,30 @@ isActualFile($staticFilePath = $sitePath . $uri)) { + if ($this->isActualFile($staticFilePath = "{$sitePath}{$uri}")) { return $staticFilePath; } @@ -36,26 +42,27 @@ 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 + * @param string $sitePath + * @param string $siteName + * @param string $uri + * * @return string */ public function frontControllerPath($sitePath, $siteName, $uri) { if (strpos($uri, '/admin.php') === 0) { $_SERVER['SCRIPT_NAME'] = '/admin.php'; - return $sitePath . '/admin.php'; + return "{$sitePath}/admin.php"; } if ($uri === '/admin') { $_SERVER['SCRIPT_NAME'] = '/admin/index.php'; - return $sitePath . '/admin/index.php'; + return "{$sitePath}/admin/index.php"; } $_SERVER['SCRIPT_NAME'] = '/index.php'; - return $sitePath . '/index.php'; + return "{$sitePath}/index.php"; } -} +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/StatamicV2ValetDriver.php b/cli/Valet/Drivers/Specific/StatamicV2ValetDriver.php new file mode 100644 index 000000000..913ba3797 --- /dev/null +++ b/cli/Valet/Drivers/Specific/StatamicV2ValetDriver.php @@ -0,0 +1,153 @@ +isActualFile($staticFilePath = "{$sitePath}{$uri}")) { + return $staticFilePath; + } + elseif ($this->isActualFile($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 ($_SERVER['REQUEST_METHOD'] === 'GET' && $this->isActualFile($staticPath = $this->getStaticPath($sitePath))) { + return $staticPath; + } + + if ($uri === '/installer.php') { + return "{$sitePath}/installer.php"; + } + + $scriptName = '/index.php'; + + if ($this->isActualFile("{$sitePath}/index.php")) { + $indexPath = "{$sitePath}/index.php"; + } + + if ($isAboveWebroot = $this->isActualFile("{$sitePath}/public/index.php")) { + $indexPath = "{$sitePath}/public/index.php"; + } + + $sitePathPrefix = $isAboveWebroot ? "{$sitePath}/public" : $sitePath; + + if ($locale = $this->getUriLocale($uri)) { + if ($this->isActualFile($localeIndexPath = "{$sitePathPrefix}/{$locale}/index.php")) { + // Force trailing slashes on locale roots. + if ($uri === "/{$locale}") { + header("Location: {$uri}/"); + exit; + } + + $indexPath = $localeIndexPath; + $scriptName = "/{$locale}/index.php"; + } + } + + $_SERVER['SCRIPT_NAME'] = $scriptName; + $_SERVER['SCRIPT_FILENAME'] = "{$sitePathPrefix}{$scriptName}"; + + return $indexPath; + } + + /** + * Get the locale from this URI. + * + * @param string $uri + * + * @return string|null + */ + public function getUriLocale($uri) { + $parts = explode('/', $uri); + $locale = $parts[1]; + + if (count($parts) < 2 || !in_array($locale, $this->getLocales())) { + return null; + } + + return $locale; + } + + /** + * Get the list of possible locales used in the first segment of a URI. + * + * @return string[] + */ + public function getLocales() { + // phpcs:disable Squiz.Arrays.ArrayDeclaration.ValueNoNewline + return [ + 'af', 'ax', 'al', 'dz', 'as', 'ad', 'ao', 'ai', 'aq', 'ag', 'ar', 'am', 'aw', 'au', 'at', 'az', 'bs', 'bh', + 'bd', 'bb', 'by', 'be', 'bz', 'bj', 'bm', 'bt', 'bo', 'bq', 'ba', 'bw', 'bv', 'br', 'io', 'bn', 'bg', 'bf', + 'bi', 'cv', 'kh', 'cm', 'ca', 'ky', 'cf', 'td', 'cl', 'cn', 'cx', 'cc', 'co', 'km', 'cg', 'cd', 'ck', 'cr', + 'ci', 'hr', 'cu', 'cw', 'cy', 'cz', 'dk', 'dj', 'dm', 'do', 'ec', 'eg', 'sv', 'gq', 'er', 'ee', 'et', 'fk', + 'fo', 'fj', 'fi', 'fr', 'gf', 'pf', 'tf', 'ga', 'gm', 'ge', 'de', 'gh', 'gi', 'gr', 'gl', 'gd', 'gp', 'gu', + 'gt', 'gg', 'gn', 'gw', 'gy', 'ht', 'hm', 'va', 'hn', 'hk', 'hu', 'is', 'in', 'id', 'ir', 'iq', 'ie', 'im', + 'il', 'it', 'jm', 'jp', 'je', 'jo', 'kz', 'ke', 'ki', 'kp', 'kr', 'kw', 'kg', 'la', 'lv', 'lb', 'ls', 'lr', + 'ly', 'li', 'lt', 'lu', 'mo', 'mk', 'mg', 'mw', 'my', 'mv', 'ml', 'mt', 'mh', 'mq', 'mr', 'mu', 'yt', 'mx', + 'fm', 'md', 'mc', 'mn', 'me', 'ms', 'ma', 'mz', 'mm', 'na', 'nr', 'np', 'nl', 'nc', 'nz', 'ni', 'ne', 'ng', + 'nu', 'nf', 'mp', 'no', 'om', 'pk', 'pw', 'ps', 'pa', 'pg', 'py', 'pe', 'ph', 'pn', 'pl', 'pt', 'pr', 'qa', + 're', 'ro', 'ru', 'rw', 'bl', 'sh', 'kn', 'lc', 'mf', 'pm', 'vc', 'ws', 'sm', 'st', 'sa', 'sn', 'rs', 'sc', + 'sl', 'sg', 'sx', 'sk', 'si', 'sb', 'so', 'za', 'gs', 'ss', 'es', 'lk', 'sd', 'sr', 'sj', 'sz', 'se', 'ch', + 'sy', 'tw', 'tj', 'tz', 'th', 'tl', 'tg', 'tk', 'to', 'tt', 'tn', 'tr', 'tm', 'tc', 'tv', 'ug', 'ua', 'ae', + 'gb', 'us', 'um', 'uy', 'uz', 'vu', 've', 'vn', 'vg', 'vi', 'wf', 'eh', 'ye', 'zm', 'zw', 'en', 'zh' + ]; + // phpcs:enable + } + + /** + * Get the path to a statically cached page. + * + * @param string $sitePath + * + * @return string + */ + protected function getStaticPath($sitePath) { + $parts = parse_url($_SERVER['REQUEST_URI']); + $query = isset($parts['query']) ? $parts['query'] : ''; + + return "{$sitePath}/static{$parts['path']}_{$query}.html"; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/StatamicValetDriver.php b/cli/Valet/Drivers/Specific/StatamicValetDriver.php new file mode 100644 index 000000000..a28271453 --- /dev/null +++ b/cli/Valet/Drivers/Specific/StatamicValetDriver.php @@ -0,0 +1,153 @@ +isActualFile($staticFilePath = "{$sitePath}{$uri}")) { + return $staticFilePath; + } + elseif ($this->isActualFile($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 ($_SERVER['REQUEST_METHOD'] === 'GET' && $this->isActualFile($staticPath = $this->getStaticPath($sitePath))) { + return $staticPath; + } + + if ($uri === '/installer.php') { + return "{$sitePath}/installer.php"; + } + + $scriptName = '/index.php'; + + if ($this->isActualFile("{$sitePath}/index.php")) { + $indexPath = "{$sitePath}/index.php"; + } + + if ($isAboveWebroot = $this->isActualFile("{$sitePath}/public/index.php")) { + $indexPath = "{$sitePath}/public/index.php"; + } + + $sitePathPrefix = $isAboveWebroot ? "{$sitePath}/public" : $sitePath; + + if ($locale = $this->getUriLocale($uri)) { + if ($this->isActualFile($localeIndexPath = "{$sitePathPrefix}/{$locale}/index.php")) { + // Force trailing slashes on locale roots. + if ($uri === "/{$locale}") { + header("Location: {$uri}/"); + exit; + } + + $indexPath = $localeIndexPath; + $scriptName = "/{$locale}/index.php"; + } + } + + $_SERVER['SCRIPT_NAME'] = $scriptName; + $_SERVER['SCRIPT_FILENAME'] = "{$sitePathPrefix}{$scriptName}"; + + return $indexPath; + } + + /** + * Get the locale from this URI. + * + * @param string $uri + * + * @return string|null + */ + public function getUriLocale($uri) { + $parts = explode('/', $uri); + $locale = $parts[1]; + + if (count($parts) < 2 || !in_array($locale, $this->getLocales())) { + return; + } + + return $locale; + } + + /** + * Get the list of possible locales used in the first segment of a URI. + * + * @return array + */ + public function getLocales() { + // phpcs:disable Squiz.Arrays.ArrayDeclaration.ValueNoNewline + return [ + 'af', 'ax', 'al', 'dz', 'as', 'ad', 'ao', 'ai', 'aq', 'ag', 'ar', 'am', 'aw', 'au', 'at', 'az', 'bs', 'bh', + 'bd', 'bb', 'by', 'be', 'bz', 'bj', 'bm', 'bt', 'bo', 'bq', 'ba', 'bw', 'bv', 'br', 'io', 'bn', 'bg', 'bf', + 'bi', 'cv', 'kh', 'cm', 'ca', 'ky', 'cf', 'td', 'cl', 'cn', 'cx', 'cc', 'co', 'km', 'cg', 'cd', 'ck', 'cr', + 'ci', 'hr', 'cu', 'cw', 'cy', 'cz', 'dk', 'dj', 'dm', 'do', 'ec', 'eg', 'sv', 'gq', 'er', 'ee', 'et', 'fk', + 'fo', 'fj', 'fi', 'fr', 'gf', 'pf', 'tf', 'ga', 'gm', 'ge', 'de', 'gh', 'gi', 'gr', 'gl', 'gd', 'gp', 'gu', + 'gt', 'gg', 'gn', 'gw', 'gy', 'ht', 'hm', 'va', 'hn', 'hk', 'hu', 'is', 'in', 'id', 'ir', 'iq', 'ie', 'im', + 'il', 'it', 'jm', 'jp', 'je', 'jo', 'kz', 'ke', 'ki', 'kp', 'kr', 'kw', 'kg', 'la', 'lv', 'lb', 'ls', 'lr', + 'ly', 'li', 'lt', 'lu', 'mo', 'mk', 'mg', 'mw', 'my', 'mv', 'ml', 'mt', 'mh', 'mq', 'mr', 'mu', 'yt', 'mx', + 'fm', 'md', 'mc', 'mn', 'me', 'ms', 'ma', 'mz', 'mm', 'na', 'nr', 'np', 'nl', 'nc', 'nz', 'ni', 'ne', 'ng', + 'nu', 'nf', 'mp', 'no', 'om', 'pk', 'pw', 'ps', 'pa', 'pg', 'py', 'pe', 'ph', 'pn', 'pl', 'pt', 'pr', 'qa', + 're', 'ro', 'ru', 'rw', 'bl', 'sh', 'kn', 'lc', 'mf', 'pm', 'vc', 'ws', 'sm', 'st', 'sa', 'sn', 'rs', 'sc', + 'sl', 'sg', 'sx', 'sk', 'si', 'sb', 'so', 'za', 'gs', 'ss', 'es', 'lk', 'sd', 'sr', 'sj', 'sz', 'se', 'ch', + 'sy', 'tw', 'tj', 'tz', 'th', 'tl', 'tg', 'tk', 'to', 'tt', 'tn', 'tr', 'tm', 'tc', 'tv', 'ug', 'ua', 'ae', + 'gb', 'us', 'um', 'uy', 'uz', 'vu', 've', 'vn', 'vg', 'vi', 'wf', 'eh', 'ye', 'zm', 'zw', 'en', 'zh' + ]; + // phpcs:enable + } + + /** + * Get the path to a statically cached page. + * + * @param string $sitePath + * + * @return string + */ + protected function getStaticPath($sitePath) { + $parts = parse_url($_SERVER['REQUEST_URI']); + $query = isset($parts['query']) ? $parts['query'] : ''; + + return "{$sitePath}/static{$parts['path']}_{$query}.html"; + } +} \ No newline at end of file diff --git a/cli/Valet/Drivers/Specific/SymfonyValetDriver.php b/cli/Valet/Drivers/Specific/SymfonyValetDriver.php new file mode 100644 index 000000000..07d28b16e --- /dev/null +++ b/cli/Valet/Drivers/Specific/SymfonyValetDriver.php @@ -0,0 +1,61 @@ +isActualFile($staticFilePath = "{$sitePath}/web/{$uri}")) { + return $staticFilePath; + } + elseif ($this->isActualFile($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($frontControllerPath = "{$sitePath}/web/app_dev.php")) { + return $frontControllerPath; + } + elseif (file_exists($frontControllerPath = "{$sitePath}/web/app.php")) { + return $frontControllerPath; + } + elseif (file_exists($frontControllerPath = "{$sitePath}/public/index.php")) { + return $frontControllerPath; + } + } +} \ No newline at end of file diff --git a/cli/drivers/Typo3ValetDriver.php b/cli/Valet/Drivers/Specific/Typo3ValetDriver.php similarity index 73% rename from cli/drivers/Typo3ValetDriver.php rename to cli/Valet/Drivers/Specific/Typo3ValetDriver.php index 7e044e40c..de5b4c72e 100644 --- a/cli/drivers/Typo3ValetDriver.php +++ b/cli/Valet/Drivers/Specific/Typo3ValetDriver.php @@ -1,9 +1,13 @@ documentRoot . '/typo3'; + $typo3Dir = "{$sitePath}{$this->documentRoot}/typo3"; return file_exists($typo3Dir) && is_dir($typo3Dir); } + /** + * 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) { + // without modifying the URI, redirect if necessary + $this->handleRedirectBackendShorthandUris($uri); + + $_SERVER['SERVER_NAME'] = "{$siteName}.dev"; + $_SERVER['DOCUMENT_URI'] = $uri; + $_SERVER['SCRIPT_NAME'] = $uri; + $_SERVER['PHP_SELF'] = $uri; + } + /** * Determine if the incoming request is for a static file. That is, it is * no PHP script file and the URI points to a valid file (no folder) on * the disk. Access to those static files will be authorized. * - * @param string $sitePath - * @param string $siteName - * @param string $uri + * @param string $sitePath + * @param string $siteName + * @param string $uri + * * @return string|false */ public function isStaticFile($sitePath, $siteName, $uri) { // May the file contains a cache busting version string like filename.12345678.css // If that is the case, the file cannot be found on disk, so remove the version // identifier before retrying below. - if (!$this->isActualFile($filePath = $sitePath . $this->documentRoot . $uri)) { + if (!$this->isActualFile($filePath = "{$sitePath}{$this->documentRoot}{$uri}")) { $uri = preg_replace("@^(.+)\.(\d+)\.(js|css|png|jpg|gif|gzip)$@", '$1.$3', $uri); } // Now that any possible version string is cleared from the filename, the resulting // URI should be a valid file on disc. So assemble the absolute file name with the // same schema as above and if it exists, authorize access and return its path. - if ($this->isActualFile($filePath = $sitePath . $this->documentRoot . $uri)) { + if ($this->isActualFile($filePath = "{$sitePath}{$this->documentRoot}{$uri}")) { return $this->isAccessAuthorized($uri) ? $filePath : false; } @@ -83,7 +107,8 @@ public function isStaticFile($sitePath, $siteName, $uri) { /** * Determines if the given URI is blacklisted so that access is prevented. * - * @param string $uri + * @param string $uri + * * @return bool */ private function isAccessAuthorized($uri) { @@ -101,39 +126,37 @@ private function isAccessAuthorized($uri) { * This can be the currently requested PHP script, a folder that * contains an index.php or the global index.php otherwise. * - * @param string $sitePath - * @param string $siteName - * @param string $uri + * @param string $sitePath + * @param string $siteName + * @param string $uri + * * @return string */ public function frontControllerPath($sitePath, $siteName, $uri) { - // without modifying the URI, redirect if necessary - $this->handleRedirectBackendShorthandUris($uri); - // from now on, remove trailing / for convenience for all the following join operations $uri = rtrim($uri, '/'); // try to find the responsible script file for the requested folder / script URI - if (file_exists($absoluteFilePath = $sitePath . $this->documentRoot . $uri)) { + if (file_exists($absoluteFilePath = "{$sitePath}{$this->documentRoot}{$uri}")) { if (is_dir($absoluteFilePath)) { - if (file_exists($absoluteFilePath . '/index.php')) { + if (file_exists("{$absoluteFilePath}/index.php")) { // this folder can be served by index.php - return $this->serveScript($sitePath, $siteName, $uri . '/index.php'); + return $this->serveScript($sitePath, "{$uri}/index.php"); } - if (file_exists($absoluteFilePath . '/index.html')) { + if (file_exists("{$absoluteFilePath}/index.html")) { // this folder can be served by index.html - return $absoluteFilePath . '/index.html'; + return "{$absoluteFilePath}/index.html"; } } elseif (pathinfo($absoluteFilePath, PATHINFO_EXTENSION) === 'php') { // this file can be served directly - return $this->serveScript($sitePath, $siteName, $uri); + return $this->serveScript($sitePath, $uri); } } // the global index.php will handle all other cases - return $this->serveScript($sitePath, $siteName, '/index.php'); + return $this->serveScript($sitePath, '/index.php'); } /** @@ -141,7 +164,9 @@ public function frontControllerPath($sitePath, $siteName, $uri) { * sysext install script. domain.dev/typo3 will be redirected to /typo3/, because * the generated JavaScript URIs on the login screen would be broken on /typo3. * - * @param string $uri + * @param string $uri + * + * @return void */ private function handleRedirectBackendShorthandUris($uri) { if (rtrim($uri, '/') === '/typo3/install') { @@ -159,23 +184,18 @@ private function handleRedirectBackendShorthandUris($uri) { * Configures the $_SERVER globals for serving the script at * the specified URI and returns it absolute file path. * - * @param string $sitePath - * @param string $siteName - * @param string $uri - * @param string $script + * @param string $sitePath + * @param string $uri + * * @return string */ - private function serveScript($sitePath, $siteName, $uri) { - $docroot = $sitePath . $this->documentRoot; - $abspath = $docroot . $uri; + private function serveScript($sitePath, $uri) { + $docroot = "{$sitePath}{$this->documentRoot}"; + $abspath = "{$docroot}{$uri}"; - $_SERVER['SERVER_NAME'] = $siteName . '.dev'; $_SERVER['DOCUMENT_ROOT'] = $docroot; - $_SERVER['DOCUMENT_URI'] = $uri; $_SERVER['SCRIPT_FILENAME'] = $abspath; - $_SERVER['SCRIPT_NAME'] = $uri; - $_SERVER['PHP_SELF'] = $uri; return $abspath; } -} +} \ No newline at end of file diff --git a/cli/drivers/WordPressValetDriver.php b/cli/Valet/Drivers/Specific/WordPressValetDriver.php similarity index 51% rename from cli/drivers/WordPressValetDriver.php rename to cli/Valet/Drivers/Specific/WordPressValetDriver.php index 6db3d9b83..bd908e8ec 100644 --- a/cli/drivers/WordPressValetDriver.php +++ b/cli/Valet/Drivers/Specific/WordPressValetDriver.php @@ -1,31 +1,47 @@ serves($sitePath, $siteName, $driver->mutateUri($uri))) { return $driver; @@ -83,17 +90,18 @@ public static function assign($sitePath, $siteName, $uri) { } /** - * Get the custom driver class from the site path, if one exists. + * Get the custom driver class from the local site path, if one exists. + * + * @param string $sitePath * - * @param string $sitePath * @return void|string */ public static function customSiteDriver($sitePath) { - if (!file_exists($sitePath . '/LocalValetDriver.php')) { + if (!file_exists("$sitePath/LocalValetDriver.php")) { return; } - require_once $sitePath . '/LocalValetDriver.php'; + require_once "$sitePath/LocalValetDriver.php"; return 'LocalValetDriver'; } @@ -101,7 +109,8 @@ public static function customSiteDriver($sitePath) { /** * Get all of the driver classes in a given path. * - * @param string $path + * @param string $path + * * @return array */ public static function driversIn($path) { @@ -124,10 +133,46 @@ public static function driversIn($path) { return $drivers; } + /** + * Get all of the specific drivers shipped with Valet. + * + * @return array + */ + public static function specificDrivers() { + return array_map(function ($item) { + return "Specific\\$item"; + }, static::driversIn(__DIR__ . '/Specific')); + } + + /** + * Get all of the custom drivers defined by the user in `~/.config/valet/Drivers`. + * + * @return array + */ + public static function customDrivers() { + return array_map(function ($item) { + return "Custom\\$item"; + }, static::driversIn(VALET_HOME_PATH . '/Drivers')); + } + + /** + * 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) { + // Do nothing + } + /** * Mutate the incoming URI. * - * @param string $uri + * @param string $uri + * * @return string */ public function mutateUri($uri) { @@ -137,10 +182,11 @@ public function mutateUri($uri) { /** * Serve the static file at the given path. * - * @param string $staticFilePath - * @param string $sitePath - * @param string $siteName - * @param string $uri + * @param string $staticFilePath + * @param string $sitePath + * @param string $siteName + * @param string $uri + * * @return void */ public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri) { @@ -168,7 +214,8 @@ public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri) { /** * Determine if the path is a file and not a directory. * - * @param string $path + * @param string $path + * * @return bool */ protected function isActualFile($path) { @@ -179,12 +226,17 @@ protected function isActualFile($path) { * Load server environment variables if available. * Processes any '*' entries first, and then adds site-specific entries. * - * @param string $sitePath - * @param string $siteName + * @param string $sitePath + * @param string $siteName + * * @return void */ public function loadServerEnvironmentVariables($sitePath, $siteName) { - $varFilePath = $sitePath . '/.valet-env.php'; + $varFilePath = "$sitePath/.valet-env.php"; + + if (!file_exists($varFilePath)) { + $varFilePath = VALET_HOME_PATH . '/.valet-env.php'; + } if (!file_exists($varFilePath)) { return; } @@ -203,7 +255,30 @@ public function loadServerEnvironmentVariables($sitePath, $siteName) { } $_SERVER[$key] = $value; $_ENV[$key] = $value; - putenv($key . '=' . $value); + putenv("$key=$value"); } } + + /** + * Check the site's Composer dependencies + * + * @param string $sitePath + * @param string $namespacedPackage + * + * @return bool + */ + public function composerRequires($sitePath, $namespacedPackage) { + if (! file_exists("$sitePath/composer.json")) { + return false; + } + + $composer_json_source = file_get_contents("$sitePath/composer.json"); + $composer_json = json_decode($composer_json_source, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + return false; + } + + return isset($composer_json['require'][$namespacedPackage]); + } } \ No newline at end of file diff --git a/cli/drivers/BasicValetDriver.php b/cli/drivers/BasicValetDriver.php deleted file mode 100644 index 673ae779d..000000000 --- a/cli/drivers/BasicValetDriver.php +++ /dev/null @@ -1,149 +0,0 @@ -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) { - $_SERVER['PHP_SELF'] = $uri; - $_SERVER['SERVER_ADDR'] = '127.0.0.1'; - $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; - - $dynamicCandidates = [ - $this->asActualFile($sitePath, $uri), - $this->asPhpIndexFileInDirectory($sitePath, $uri), - $this->asHtmlIndexFileInDirectory($sitePath, $uri) - ]; - - foreach ($dynamicCandidates as $candidate) { - if ($this->isActualFile($candidate)) { - $_SERVER['SCRIPT_FILENAME'] = $candidate; - $_SERVER['SCRIPT_NAME'] = str_replace($sitePath, '', $candidate); - $_SERVER['DOCUMENT_ROOT'] = $sitePath; - - return $candidate; - } - } - - $fixedCandidatesAndDocroots = [ - $this->asRootPhpIndexFile($sitePath) => $sitePath, - $this->asPublicPhpIndexFile($sitePath) => $sitePath . '/public', - $this->asPublicHtmlIndexFile($sitePath) => $sitePath . '/public' - ]; - - foreach ($fixedCandidatesAndDocroots as $candidate => $docroot) { - if ($this->isActualFile($candidate)) { - $_SERVER['SCRIPT_FILENAME'] = $candidate; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['DOCUMENT_ROOT'] = $docroot; - - return $candidate; - } - } - } - - /** - * Concatenate the site path and URI as a single file name. - * - * @param string $sitePath - * @param string $uri - * @return string - */ - protected function asActualFile($sitePath, $uri) { - return $sitePath . $uri; - } - - /** - * Format the site path and URI with a trailing "index.php". - * - * @param string $sitePath - * @param string $uri - * @return string - */ - protected function asPhpIndexFileInDirectory($sitePath, $uri) { - return $sitePath . rtrim($uri, '/') . '/index.php'; - } - - /** - * Format the site path and URI with a trailing "index.html". - * - * @param string $sitePath - * @param string $uri - * @return string - */ - protected function asHtmlIndexFileInDirectory($sitePath, $uri) { - return $sitePath . rtrim($uri, '/') . '/index.html'; - } - - /** - * Format the incoming site path as root "index.php" file path. - * - * @param string $sitePath - * @return string - */ - protected function asRootPhpIndexFile($sitePath) { - return $sitePath . '/index.php'; - } - - /** - * Format the incoming site path as a "public/index.php" file path. - * - * @param string $sitePath - * @return string - */ - protected function asPublicPhpIndexFile($sitePath) { - return $sitePath . '/public/index.php'; - } - - /** - * Format the incoming site path as a "public/index.php" file path. - * - * @param string $sitePath - * @return string - */ - protected function asPublicHtmlIndexFile($sitePath) { - return $sitePath . '/public/index.html'; - } -} diff --git a/cli/drivers/BedrockValetDriver.php b/cli/drivers/BedrockValetDriver.php deleted file mode 100644 index 2704770b1..000000000 --- a/cli/drivers/BedrockValetDriver.php +++ /dev/null @@ -1,69 +0,0 @@ -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) { - $_SERVER['PHP_SELF'] = $uri; - $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; - - 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; - } -} diff --git a/cli/drivers/CakeValetDriver.php b/cli/drivers/CakeValetDriver.php deleted file mode 100644 index 7fb3b1b2b..000000000 --- a/cli/drivers/CakeValetDriver.php +++ /dev/null @@ -1,48 +0,0 @@ -isActualFile($staticFilePath = $sitePath . '/webroot/' . $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) { - $_SERVER['DOCUMENT_ROOT'] = $sitePath . '/webroot'; - $_SERVER['SCRIPT_FILENAME'] = $sitePath . '/webroot/index.php'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - $_SERVER['PHP_SELF'] = '/index.php'; - - return $sitePath . '/webroot/index.php'; - } -} diff --git a/cli/drivers/Concrete5ValetDriver.php b/cli/drivers/Concrete5ValetDriver.php deleted file mode 100644 index 8ba350c54..000000000 --- a/cli/drivers/Concrete5ValetDriver.php +++ /dev/null @@ -1,44 +0,0 @@ -isActualFile($staticFilePath = $sitePath . '/web' . $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 ($uri === '/install.php') { - return $sitePath . '/web/install.php'; - } - - if (0 === strncmp($uri, '/app_dev.php', 12)) { - $_SERVER['SCRIPT_NAME'] = '/app_dev.php'; - $_SERVER['SCRIPT_FILENAME'] = $sitePath . '/app_dev.php'; - - return $sitePath . '/web/app_dev.php'; - } - - return $sitePath . '/web/app.php'; - } -} diff --git a/cli/drivers/JigsawValetDriver.php b/cli/drivers/JigsawValetDriver.php deleted file mode 100644 index 73a42f814..000000000 --- a/cli/drivers/JigsawValetDriver.php +++ /dev/null @@ -1,25 +0,0 @@ -isActualFile($staticFilePath = $sitePath . $uri)) { - return $staticFilePath; - } - elseif ($this->isActualFile($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) { - $scriptName = '/index.php'; - - if ($this->isActualFile($sitePath . '/index.php')) { - $indexPath = $sitePath . '/index.php'; - } - - if ($isAboveWebroot = $this->isActualFile($sitePath . '/public/index.php')) { - $indexPath = $sitePath . '/public/index.php'; - } - - if (preg_match('/^\/panel/', $uri) && $this->isActualFile($sitePath . '/panel/index.php')) { - $scriptName = '/panel/index.php'; - $indexPath = $sitePath . '/panel/index.php'; - } - - $sitePathPrefix = ($isAboveWebroot) ? $sitePath . '/public' : $sitePath; - - $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; - $_SERVER['SCRIPT_NAME'] = $scriptName; - $_SERVER['SCRIPT_FILENAME'] = $sitePathPrefix . $scriptName; - - return $indexPath; - } -} diff --git a/cli/drivers/LaravelValetDriver.php b/cli/drivers/LaravelValetDriver.php deleted file mode 100644 index f1664b928..000000000 --- a/cli/drivers/LaravelValetDriver.php +++ /dev/null @@ -1,58 +0,0 @@ -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) { - // Shortcut for getting the "local" hostname as the HTTP_HOST - if (isset($_SERVER['HTTP_X_ORIGINAL_HOST'], $_SERVER['HTTP_X_FORWARDED_HOST'])) { - $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST']; - } - - return $sitePath . '/public/index.php'; - } -} diff --git a/cli/drivers/NeosValetDriver.php b/cli/drivers/NeosValetDriver.php deleted file mode 100644 index b42f89e2e..000000000 --- a/cli/drivers/NeosValetDriver.php +++ /dev/null @@ -1,48 +0,0 @@ -isActualFile($staticFilePath = $sitePath . '/Web' . $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) { - putenv('FLOW_CONTEXT=Development'); - putenv('FLOW_REWRITEURLS=1'); - $_SERVER['SCRIPT_FILENAME'] = $sitePath . '/Web/index.php'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - - return $sitePath . '/Web/index.php'; - } -} diff --git a/cli/drivers/SculpinValetDriver.php b/cli/drivers/SculpinValetDriver.php deleted file mode 100644 index ec2039342..000000000 --- a/cli/drivers/SculpinValetDriver.php +++ /dev/null @@ -1,48 +0,0 @@ -isModernSculpinProject($sitePath) || $this->isLegacySculpinProject($sitePath); - } - - private function isModernSculpinProject($sitePath) { - return is_dir($sitePath . '/source') && is_dir($sitePath . '/output_dev') && $this->composerRequiresSculpin($sitePath); - } - - private function isLegacySculpinProject($sitePath) { - return is_dir($sitePath . '/.sculpin'); - } - - private function composerRequiresSculpin($sitePath) { - if (!file_exists($sitePath . '/composer.json')) { - return false; - } - - $composer_json_source = file_get_contents($sitePath . '/composer.json'); - $composer_json = json_decode($composer_json_source, true); - - if (json_last_error() !== JSON_ERROR_NONE) { - return false; - } - - return isset($composer_json['require']['sculpin/sculpin']); - } - - /** - * Mutate the incoming URI. - * - * @param string $uri - * @return string - */ - public function mutateUri($uri) { - return rtrim('/output_dev' . $uri, '/'); - } -} diff --git a/cli/drivers/StatamicValetDriver.php b/cli/drivers/StatamicValetDriver.php deleted file mode 100644 index 340f3c2d0..000000000 --- a/cli/drivers/StatamicValetDriver.php +++ /dev/null @@ -1,379 +0,0 @@ -isActualFile($staticFilePath = $sitePath . $uri)) { - return $staticFilePath; - } - elseif ($this->isActualFile($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 ($_SERVER['REQUEST_METHOD'] === 'GET' && $this->isActualFile($staticPath = $this->getStaticPath($sitePath))) { - return $staticPath; - } - - if ($uri === '/installer.php') { - return $sitePath . '/installer.php'; - } - - $scriptName = '/index.php'; - - if ($this->isActualFile($sitePath . '/index.php')) { - $indexPath = $sitePath . '/index.php'; - } - - if ($isAboveWebroot = $this->isActualFile($sitePath . '/public/index.php')) { - $indexPath = $sitePath . '/public/index.php'; - } - - $sitePathPrefix = ($isAboveWebroot) ? $sitePath . '/public' : $sitePath; - - if ($locale = $this->getUriLocale($uri)) { - if ($this->isActualFile($localeIndexPath = $sitePathPrefix . '/' . $locale . '/index.php')) { - // Force trailing slashes on locale roots. - if ($uri === '/' . $locale) { - header('Location: ' . $uri . '/'); - exit; - } - - $indexPath = $localeIndexPath; - $scriptName = '/' . $locale . '/index.php'; - } - } - - $_SERVER['SCRIPT_NAME'] = $scriptName; - $_SERVER['SCRIPT_FILENAME'] = $sitePathPrefix . $scriptName; - - return $indexPath; - } - - /** - * Get the locale from this URI. - * - * @param string $uri - * @return string|null - */ - public function getUriLocale($uri) { - $parts = explode('/', $uri); - $locale = $parts[1]; - - if (count($parts) < 2 || !in_array($locale, $this->getLocales())) { - return; - } - - return $locale; - } - - /** - * Get the list of possible locales used in the first segment of a URI. - * - * @return array - */ - public function getLocales() { - return [ - 'af', - 'ax', - 'al', - 'dz', - 'as', - 'ad', - 'ao', - 'ai', - 'aq', - 'ag', - 'ar', - 'am', - 'aw', - 'au', - 'at', - 'az', - 'bs', - 'bh', - 'bd', - 'bb', - 'by', - 'be', - 'bz', - 'bj', - 'bm', - 'bt', - 'bo', - 'bq', - 'ba', - 'bw', - 'bv', - 'br', - 'io', - 'bn', - 'bg', - 'bf', - 'bi', - 'cv', - 'kh', - 'cm', - 'ca', - 'ky', - 'cf', - 'td', - 'cl', - 'cn', - 'cx', - 'cc', - 'co', - 'km', - 'cg', - 'cd', - 'ck', - 'cr', - 'ci', - 'hr', - 'cu', - 'cw', - 'cy', - 'cz', - 'dk', - 'dj', - 'dm', - 'do', - 'ec', - 'eg', - 'sv', - 'gq', - 'er', - 'ee', - 'et', - 'fk', - 'fo', - 'fj', - 'fi', - 'fr', - 'gf', - 'pf', - 'tf', - 'ga', - 'gm', - 'ge', - 'de', - 'gh', - 'gi', - 'gr', - 'gl', - 'gd', - 'gp', - 'gu', - 'gt', - 'gg', - 'gn', - 'gw', - 'gy', - 'ht', - 'hm', - 'va', - 'hn', - 'hk', - 'hu', - 'is', - 'in', - 'id', - 'ir', - 'iq', - 'ie', - 'im', - 'il', - 'it', - 'jm', - 'jp', - 'je', - 'jo', - 'kz', - 'ke', - 'ki', - 'kp', - 'kr', - 'kw', - 'kg', - 'la', - 'lv', - 'lb', - 'ls', - 'lr', - 'ly', - 'li', - 'lt', - 'lu', - 'mo', - 'mk', - 'mg', - 'mw', - 'my', - 'mv', - 'ml', - 'mt', - 'mh', - 'mq', - 'mr', - 'mu', - 'yt', - 'mx', - 'fm', - 'md', - 'mc', - 'mn', - 'me', - 'ms', - 'ma', - 'mz', - 'mm', - 'na', - 'nr', - 'np', - 'nl', - 'nc', - 'nz', - 'ni', - 'ne', - 'ng', - 'nu', - 'nf', - 'mp', - 'no', - 'om', - 'pk', - 'pw', - 'ps', - 'pa', - 'pg', - 'py', - 'pe', - 'ph', - 'pn', - 'pl', - 'pt', - 'pr', - 'qa', - 're', - 'ro', - 'ru', - 'rw', - 'bl', - 'sh', - 'kn', - 'lc', - 'mf', - 'pm', - 'vc', - 'ws', - 'sm', - 'st', - 'sa', - 'sn', - 'rs', - 'sc', - 'sl', - 'sg', - 'sx', - 'sk', - 'si', - 'sb', - 'so', - 'za', - 'gs', - 'ss', - 'es', - 'lk', - 'sd', - 'sr', - 'sj', - 'sz', - 'se', - 'ch', - 'sy', - 'tw', - 'tj', - 'tz', - 'th', - 'tl', - 'tg', - 'tk', - 'to', - 'tt', - 'tn', - 'tr', - 'tm', - 'tc', - 'tv', - 'ug', - 'ua', - 'ae', - 'gb', - 'us', - 'um', - 'uy', - 'uz', - 'vu', - 've', - 'vn', - 'vg', - 'vi', - 'wf', - 'eh', - 'ye', - 'zm', - 'zw', - 'en', - 'zh' - ]; - } - - /** - * Get the path to a statically cached page. - * - * @param string $sitePath - * @return string - */ - protected function getStaticPath($sitePath) { - $parts = parse_url($_SERVER['REQUEST_URI']); - $query = isset($parts['query']) ? $parts['query'] : ''; - - return $sitePath . '/static' . $parts['path'] . '_' . $query . '.html'; - } -} diff --git a/cli/drivers/SymfonyValetDriver.php b/cli/drivers/SymfonyValetDriver.php deleted file mode 100644 index 12c365b0b..000000000 --- a/cli/drivers/SymfonyValetDriver.php +++ /dev/null @@ -1,54 +0,0 @@ -isActualFile($staticFilePath = $sitePath . '/web/' . $uri)) { - return $staticFilePath; - } - elseif ($this->isActualFile($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($frontControllerPath = $sitePath . '/web/app_dev.php')) { - return $frontControllerPath; - } - elseif (file_exists($frontControllerPath = $sitePath . '/web/app.php')) { - return $frontControllerPath; - } - elseif (file_exists($frontControllerPath = $sitePath . '/public/index.php')) { - return $frontControllerPath; - } - } -} diff --git a/cli/drivers/require.php b/cli/drivers/require.php deleted file mode 100644 index c42bedc32..000000000 --- a/cli/drivers/require.php +++ /dev/null @@ -1,12 +0,0 @@ -serveStaticFile($staticFilePath, $valetSitePath, $siteName, $uri); } +/** + * Allow for drivers to take pre-loading actions (e.g. setting server variables). + */ +$valetDriver->beforeLoading($valetSitePath, $siteName, $uri); + /** * Attempt to dispatch to a front controller. */