mirror of
https://github.com/laravel/valet.git
synced 2026-02-06 16:50:09 +01:00
Resolve merge
This commit is contained in:
@@ -42,11 +42,7 @@ public function beforeLoading(string $sitePath, string $siteName, string $uri):
|
||||
*/
|
||||
public function isStaticFile(string $sitePath, string $siteName, string $uri): string|false
|
||||
{
|
||||
if (file_exists($staticFilePath = $sitePath.'/public'.rtrim($uri, '/').'/index.html')) {
|
||||
return $staticFilePath;
|
||||
} elseif (file_exists($staticFilePath = $sitePath.'/public'.rtrim($uri, '/').'/index.php')) {
|
||||
return $staticFilePath;
|
||||
} elseif (file_exists($staticFilePath = $sitePath.'/public'.$uri)) {
|
||||
if (file_exists($staticFilePath = $sitePath.rtrim($uri, '/').'/index.html')) {
|
||||
return $staticFilePath;
|
||||
} elseif ($this->isActualFile($staticFilePath = $sitePath.$uri)) {
|
||||
return $staticFilePath;
|
||||
@@ -65,13 +61,16 @@ public function isStaticFile(string $sitePath, string $siteName, string $uri): s
|
||||
*/
|
||||
public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
|
||||
{
|
||||
$dynamicCandidates = [
|
||||
$this->asActualFile($sitePath, $uri),
|
||||
$this->asPhpIndexFileInDirectory($sitePath, $uri),
|
||||
$this->asHtmlIndexFileInDirectory($sitePath, $uri),
|
||||
$uri = rtrim($uri, '/');
|
||||
|
||||
$candidates = [
|
||||
$sitePath.$uri,
|
||||
$sitePath.$uri.'/index.php',
|
||||
$sitePath.'/index.php',
|
||||
$sitePath.'/index.html',
|
||||
];
|
||||
|
||||
foreach ($dynamicCandidates as $candidate) {
|
||||
foreach ($candidates as $candidate) {
|
||||
if ($this->isActualFile($candidate)) {
|
||||
$_SERVER['SCRIPT_FILENAME'] = $candidate;
|
||||
$_SERVER['SCRIPT_NAME'] = str_replace($sitePath, '', $candidate);
|
||||
@@ -80,90 +79,5 @@ public function frontControllerPath(string $sitePath, string $siteName, string $
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
||||
75
cli/Valet/Drivers/BasicWithPublicValetDriver.php
Normal file
75
cli/Valet/Drivers/BasicWithPublicValetDriver.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?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(string $sitePath, string $siteName, string $uri): bool
|
||||
{
|
||||
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 string|false
|
||||
*/
|
||||
public function isStaticFile(string $sitePath, string $siteName, string $uri): string|false
|
||||
{
|
||||
$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
|
||||
*/
|
||||
public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
|
||||
{
|
||||
$_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,7 @@ public static function assign(string $sitePath, string $siteName, string $uri):
|
||||
// Queue Valet-shipped drivers
|
||||
$drivers[] = 'LaravelValetDriver';
|
||||
$drivers = array_merge($drivers, static::specificDrivers());
|
||||
$drivers[] = 'BasicWithPublicValetDriver';
|
||||
$drivers[] = 'BasicValetDriver';
|
||||
|
||||
foreach ($drivers as $driver) {
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
class BaseDriverTestCase extends Yoast\PHPUnitPolyfills\TestCases\TestCase
|
||||
{
|
||||
public function set_up(): void
|
||||
{
|
||||
$_SERVER['HTTP_HOST'] = 'this is set in Valet requests but not phpunit';
|
||||
}
|
||||
|
||||
public function projects(): array
|
||||
{
|
||||
return Filesystem::scanDir(__DIR__.'/projects');
|
||||
|
||||
@@ -12,4 +12,59 @@ public function test_it_serves_anything()
|
||||
$this->assertTrue($driver->serves($projectDir, 'my-site', '/'));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_it_serves_php_files_from_root()
|
||||
{
|
||||
$projectPath = $this->projectDir('basic-no-public');
|
||||
$driver = new BasicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/file-in-root.php',
|
||||
$driver->frontControllerPath($projectPath, 'my-site', '/file-in-root.php')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_serves_directory_with_index_php()
|
||||
{
|
||||
$projectPath = $this->projectDir('basic-no-public');
|
||||
$driver = new BasicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/about/index.php',
|
||||
$driver->frontControllerPath($projectPath, 'my-site', '/about')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_routes_to_index_if_404()
|
||||
{
|
||||
$projectPath = $this->projectDir('basic-no-public');
|
||||
$driver = new BasicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/index.php',
|
||||
$driver->frontControllerPath($projectPath, 'my-site', '/not-a-real-url')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_serves_directory_with_index_html()
|
||||
{
|
||||
$projectPath = $this->projectDir('basic-no-public');
|
||||
$driver = new BasicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/team/index.html',
|
||||
$driver->isStaticFile($projectPath, 'my-site', '/team')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_serves_static_files()
|
||||
{
|
||||
$projectPath = $this->projectDir('basic-no-public');
|
||||
$driver = new BasicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/assets/document.txt',
|
||||
$driver->isStaticFile($projectPath, 'my-site', '/assets/document.txt')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
86
tests/Drivers/BasicWithPublicValetDriverTest.php
Normal file
86
tests/Drivers/BasicWithPublicValetDriverTest.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
use Valet\Drivers\BasicWithPublicValetDriver;
|
||||
|
||||
class BasicWithPublicValetDriverTest extends BaseDriverTestCase
|
||||
{
|
||||
public function test_it_serves_anything_with_public()
|
||||
{
|
||||
$driver = new BasicWithPublicValetDriver();
|
||||
|
||||
$this->assertTrue($driver->serves($this->projectDir('public-with-index-non-laravel'), 'my-site', '/'));
|
||||
}
|
||||
|
||||
public function test_it_doesnt_serve_from_not_public()
|
||||
{
|
||||
$driver = new BasicWithPublicValetDriver();
|
||||
|
||||
$this->assertFalse($driver->serves($this->projectDir('basic-no-public'), 'my-site', '/'));
|
||||
}
|
||||
|
||||
public function test_it_serves_php_files_from_public()
|
||||
{
|
||||
$projectPath = $this->projectDir('public-with-index-non-laravel');
|
||||
$driver = new BasicWithPublicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/public/file-in-public.php',
|
||||
$driver->frontControllerPath($projectPath, 'my-site', '/file-in-public.php')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_doesnt_serve_php_files_from_root()
|
||||
{
|
||||
$projectPath = $this->projectDir('public-with-index-non-laravel');
|
||||
$driver = new BasicWithPublicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/public/index.php',
|
||||
$driver->frontControllerPath($projectPath, 'my-site', '/file-in-root.php')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_serves_directory_with_index_php()
|
||||
{
|
||||
$projectPath = $this->projectDir('public-with-index-non-laravel');
|
||||
$driver = new BasicWithPublicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/public/about/index.php',
|
||||
$driver->frontControllerPath($projectPath, 'my-site', '/about')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_route_to_public_index_if_404()
|
||||
{
|
||||
$projectPath = $this->projectDir('public-with-index-non-laravel');
|
||||
$driver = new BasicWithPublicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/public/index.php',
|
||||
$driver->frontControllerPath($projectPath, 'my-site', '/not-a-real-url')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_serves_directory_with_index_html()
|
||||
{
|
||||
$projectPath = $this->projectDir('public-with-index-non-laravel');
|
||||
$driver = new BasicWithPublicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/public/team/index.html',
|
||||
$driver->isStaticFile($projectPath, 'my-site', '/team')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_serves_static_files()
|
||||
{
|
||||
$projectPath = $this->projectDir('public-with-index-non-laravel');
|
||||
$driver = new BasicWithPublicValetDriver();
|
||||
|
||||
$this->assertEquals(
|
||||
$projectPath.'/public/assets/document.txt',
|
||||
$driver->isStaticFile($projectPath, 'my-site', '/assets/document.txt')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,6 @@ public function test_it_gets_front_controller()
|
||||
{
|
||||
$driver = new BedrockValetDriver();
|
||||
|
||||
$_SERVER['HTTP_HOST'] = 'this is set in Valet requests but not phpunit';
|
||||
|
||||
$projectPath = $this->projectDir('bedrock');
|
||||
$this->assertEquals($projectPath.'/web/index.php', $driver->frontControllerPath($projectPath, 'my-site', '/'));
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ public function test_it_gets_front_controller()
|
||||
{
|
||||
$driver = new CraftValetDriver();
|
||||
|
||||
$_SERVER['HTTP_HOST'] = 'this is set in Valet requests but not phpunit';
|
||||
|
||||
$projectPath = $this->projectDir('craft');
|
||||
$this->assertEquals($projectPath.'/public/index.php', $driver->frontControllerPath($projectPath, 'my-site', '/'));
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ public function test_it_gets_front_controller()
|
||||
{
|
||||
$driver = new DrupalValetDriver();
|
||||
|
||||
$_SERVER['HTTP_HOST'] = 'this is set in Valet requests but not phpunit';
|
||||
|
||||
$projectPath = $this->projectDir('drupal');
|
||||
$this->assertEquals($projectPath.'/public/index.php', $driver->frontControllerPath($projectPath, 'my-site', '/'));
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ public function test_it_gets_front_controller()
|
||||
{
|
||||
$driver = new JoomlaValetDriver();
|
||||
|
||||
$_SERVER['HTTP_HOST'] = 'this is set in Valet requests but not phpunit';
|
||||
|
||||
$projectPath = $this->projectDir('joomla');
|
||||
$this->assertEquals($projectPath.'/index.php', $driver->frontControllerPath($projectPath, 'my-site', '/'));
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ public function test_it_gets_front_controller()
|
||||
{
|
||||
$driver = new KirbyValetDriver();
|
||||
|
||||
$_SERVER['HTTP_HOST'] = 'this is set in Valet requests but not phpunit';
|
||||
|
||||
$projectPath = $this->projectDir('kirby');
|
||||
$this->assertEquals($projectPath.'/index.php', $driver->frontControllerPath($projectPath, 'my-site', '/'));
|
||||
}
|
||||
|
||||
@@ -20,4 +20,12 @@ public function test_it_assigns_drivers_to_given_project()
|
||||
|
||||
$this->assertEquals(BedrockValetDriver::class, get_class($assignedDriver));
|
||||
}
|
||||
|
||||
public function test_it_prioritizes_non_basic_matches()
|
||||
{
|
||||
$assignedDriver = ValetDriver::assign($this->projectDir('laravel'), 'my-site', '/');
|
||||
|
||||
$this->assertNotEquals('Valet\Drivers\BasicWithPublicValetDriver', get_class($assignedDriver));
|
||||
$this->assertNotEquals('Valet\Drivers\BasicValetDriver', get_class($assignedDriver));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ public function test_it_gets_front_controller()
|
||||
{
|
||||
$driver = new WordPressValetDriver();
|
||||
|
||||
$_SERVER['HTTP_HOST'] = 'this is set in Valet requests but not phpunit';
|
||||
|
||||
$projectPath = $this->projectDir('wordpress');
|
||||
$this->assertEquals($projectPath.'/index.php', $driver->frontControllerPath($projectPath, 'my-site', '/'));
|
||||
}
|
||||
|
||||
0
tests/Drivers/projects/basic-no-public/index.php
Normal file
0
tests/Drivers/projects/basic-no-public/index.php
Normal file
Reference in New Issue
Block a user