mirror of
https://github.com/laravel/valet.git
synced 2026-02-05 08:30:07 +01:00
Add --site to isolate and unisolate commands
- Add --site to isolate - Add --site to unisolate - Refactor some tests - Update Site@getSiteUrl to throw an exception instead of returning false - Fix a few minor typos/grammatical issues
This commit is contained in:
@@ -211,9 +211,7 @@ public function stopIfUnused($phpVersion = null)
|
|||||||
*/
|
*/
|
||||||
public function isolateDirectory($directory, $version)
|
public function isolateDirectory($directory, $version)
|
||||||
{
|
{
|
||||||
if (! $site = $this->site->getSiteUrl($directory)) {
|
$site = $this->site->getSiteUrl($directory);
|
||||||
throw new DomainException("The [{$directory}] site could not be found in Valet's site list.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$version = $this->validateRequestedVersion($version);
|
$version = $this->validateRequestedVersion($version);
|
||||||
|
|
||||||
@@ -239,9 +237,7 @@ public function isolateDirectory($directory, $version)
|
|||||||
*/
|
*/
|
||||||
public function unIsolateDirectory($directory)
|
public function unIsolateDirectory($directory)
|
||||||
{
|
{
|
||||||
if (! $site = $this->site->getSiteUrl($directory)) {
|
$site = $this->site->getSiteUrl($directory);
|
||||||
throw new DomainException("The [{$directory}] site could not be found in Valet's site list.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$oldCustomPhpVersion = $this->site->customPhpVersion($site); // Example output: "74"
|
$oldCustomPhpVersion = $this->site->customPhpVersion($site); // Example output: "74"
|
||||||
|
|
||||||
@@ -339,10 +335,14 @@ public function normalizePhpVersion($version)
|
|||||||
*/
|
*/
|
||||||
public function validateRequestedVersion($version)
|
public function validateRequestedVersion($version)
|
||||||
{
|
{
|
||||||
|
if (is_null($version)) {
|
||||||
|
throw new DomainException("Please specify a PHP version (try something like 'php@8.1')");
|
||||||
|
}
|
||||||
|
|
||||||
$version = $this->normalizePhpVersion($version);
|
$version = $this->normalizePhpVersion($version);
|
||||||
|
|
||||||
if (! $this->brew->supportedPhpVersions()->contains($version)) {
|
if (! $this->brew->supportedPhpVersions()->contains($version)) {
|
||||||
throw new DomainException("Valet doesn't support PHP version: {$version} (try something like 'php@7.3' instead)");
|
throw new DomainException("Valet doesn't support PHP version: {$version} (try something like 'php@8.1' instead)");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) {
|
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) {
|
||||||
@@ -351,7 +351,7 @@ public function validateRequestedVersion($version)
|
|||||||
|
|
||||||
if ($version === 'php') {
|
if ($version === 'php') {
|
||||||
if ($this->brew->hasInstalledPhp()) {
|
if ($this->brew->hasInstalledPhp()) {
|
||||||
throw new DomainException('Brew is already using PHP '.PHP_VERSION.' as \'php\' in Homebrew. To use another version, please specify. eg: php@7.3');
|
throw new DomainException('Brew is already using PHP '.PHP_VERSION.' as \'php\' in Homebrew. To use another version, please specify. eg: php@8.1');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ public function proxies()
|
|||||||
* Get the site URL from a directory if it's a valid Valet site.
|
* Get the site URL from a directory if it's a valid Valet site.
|
||||||
*
|
*
|
||||||
* @param string $directory
|
* @param string $directory
|
||||||
* @return string|false
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getSiteUrl($directory)
|
public function getSiteUrl($directory)
|
||||||
{
|
{
|
||||||
@@ -207,7 +207,7 @@ public function getSiteUrl($directory)
|
|||||||
$directory = str_replace('.'.$tld, '', $directory); // Remove .tld from sitename if it was provided
|
$directory = str_replace('.'.$tld, '', $directory); // Remove .tld from sitename if it was provided
|
||||||
|
|
||||||
if (! $this->parked()->merge($this->links())->where('site', $directory)->count() > 0) {
|
if (! $this->parked()->merge($this->links())->where('site', $directory)->count() > 0) {
|
||||||
return false; // Invalid directory provided
|
throw new DomainException("The [{$directory}] site could not be found in Valet's site list.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $directory.'.'.$tld;
|
return $directory.'.'.$tld;
|
||||||
|
|||||||
@@ -288,7 +288,7 @@
|
|||||||
* Generate a publicly accessible URL for your project.
|
* Generate a publicly accessible URL for your project.
|
||||||
*/
|
*/
|
||||||
$app->command('share', function () {
|
$app->command('share', function () {
|
||||||
warning('It looks like you are running `cli/valet.php` directly, please use the `valet` script in the project root instead.');
|
warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.');
|
||||||
})->descriptions('Generate a publicly accessible URL for your project');
|
})->descriptions('Generate a publicly accessible URL for your project');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -528,18 +528,29 @@
|
|||||||
/**
|
/**
|
||||||
* Allow the user to change the version of PHP Valet uses to serve the current site.
|
* Allow the user to change the version of PHP Valet uses to serve the current site.
|
||||||
*/
|
*/
|
||||||
$app->command('isolate [phpVersion] ', function ($phpVersion) {
|
$app->command('isolate [phpVersion] [--site=]', function ($phpVersion, $site = null) {
|
||||||
PhpFpm::isolateDirectory(basename(getcwd()), $phpVersion);
|
if (! $site) {
|
||||||
|
$site = basename(getcwd());
|
||||||
|
}
|
||||||
|
|
||||||
|
PhpFpm::isolateDirectory($site, $phpVersion);
|
||||||
})->descriptions('Change the version of PHP used by Valet to serve the current working directory', [
|
})->descriptions('Change the version of PHP used by Valet to serve the current working directory', [
|
||||||
'phpVersion' => 'The PHP version you want to use, e.g php@7.3',
|
'phpVersion' => 'The PHP version you want to use; e.g php@8.1',
|
||||||
|
'--site' => 'Specify the site to isolate (e.g. if the site isn\'t linked as its directory name)',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow the user to un-do specifying the version of PHP Valet uses to serve the current site.
|
* Allow the user to un-do specifying the version of PHP Valet uses to serve the current site.
|
||||||
*/
|
*/
|
||||||
$app->command('unisolate', function () {
|
$app->command('unisolate [--site=]', function ($site = null) {
|
||||||
PhpFpm::unIsolateDirectory(basename(getcwd()));
|
if (! $site) {
|
||||||
})->descriptions('Stop customizing the version of PHP used by Valet to serve the current working directory');
|
$site = basename(getcwd());
|
||||||
|
}
|
||||||
|
|
||||||
|
PhpFpm::unIsolateDirectory($site);
|
||||||
|
})->descriptions('Stop customizing the version of PHP used by Valet to serve the current working directory', [
|
||||||
|
'--site' => 'Specify the site to un-isolate (e.g. if the site isn\'t linked as its directory name)',
|
||||||
|
]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List isolated sites.
|
* List isolated sites.
|
||||||
|
|||||||
@@ -65,22 +65,49 @@ public function test_it_normalizes_php_versions()
|
|||||||
$this->assertEquals('php@8.1', resolve(PhpFpm::class)->normalizePhpVersion('81'));
|
$this->assertEquals('php@8.1', resolve(PhpFpm::class)->normalizePhpVersion('81'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_it_validates_php_versions_when_installed()
|
||||||
|
{
|
||||||
|
$brewMock = Mockery::mock(Brew::class);
|
||||||
|
|
||||||
|
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect(['php@7.4']));
|
||||||
|
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('7.4');
|
||||||
|
|
||||||
|
swap(Brew::class, $brewMock);
|
||||||
|
|
||||||
|
$this->assertEquals('php@7.4', resolve(PhpFpm::class)->validateRequestedVersion('7.4'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_validates_php_versions_when_uninstalled()
|
||||||
|
{
|
||||||
|
$brewMock = Mockery::mock(Brew::class);
|
||||||
|
|
||||||
|
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect(['php@7.4']));
|
||||||
|
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('ERROR - NO BREW ALIAS FOUND');
|
||||||
|
|
||||||
|
swap(Brew::class, $brewMock);
|
||||||
|
|
||||||
|
$this->assertEquals('php@7.4', resolve(PhpFpm::class)->validateRequestedVersion('7.4'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_throws_when_validating_invalid_php()
|
||||||
|
{
|
||||||
|
$this->expectException(DomainException::class);
|
||||||
|
|
||||||
|
$brewMock = Mockery::mock(Brew::class);
|
||||||
|
|
||||||
|
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect(['php@7.4',]));
|
||||||
|
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('ERROR - NO BREW ALIAS FOUND');
|
||||||
|
|
||||||
|
swap(Brew::class, $brewMock);
|
||||||
|
|
||||||
|
$this->assertEquals('php@7.4', resolve(PhpFpm::class)->validateRequestedVersion('9.1'));
|
||||||
|
}
|
||||||
|
|
||||||
public function test_utilized_php_versions()
|
public function test_utilized_php_versions()
|
||||||
{
|
{
|
||||||
$fileSystemMock = Mockery::mock(Filesystem::class);
|
|
||||||
$brewMock = Mockery::mock(Brew::class);
|
$brewMock = Mockery::mock(Brew::class);
|
||||||
$nginxMock = Mockery::mock(Nginx::class);
|
$nginxMock = Mockery::mock(Nginx::class);
|
||||||
|
$fileSystemMock = Mockery::mock(Filesystem::class);
|
||||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
|
||||||
$brewMock,
|
|
||||||
Mockery::mock(CommandLine::class),
|
|
||||||
$fileSystemMock,
|
|
||||||
resolve(Configuration::class),
|
|
||||||
Mockery::mock(Site::class),
|
|
||||||
$nginxMock,
|
|
||||||
])->makePartial();
|
|
||||||
|
|
||||||
swap(PhpFpm::class, $phpFpmMock);
|
|
||||||
|
|
||||||
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
|
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
|
||||||
'php@7.1',
|
'php@7.1',
|
||||||
@@ -111,37 +138,30 @@ public function test_utilized_php_versions()
|
|||||||
];
|
];
|
||||||
|
|
||||||
foreach ($sites as $site) {
|
foreach ($sites as $site) {
|
||||||
$fileSystemMock->shouldReceive('get')->once()->with(VALET_HOME_PATH.'/Nginx/'.$site['site'])->andReturn($site['conf']);
|
$fileSystemMock->shouldReceive('get')->once()->with(VALET_HOME_PATH . '/Nginx/' . $site['site'])->andReturn($site['conf']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
swap(Filesystem::class, $fileSystemMock);
|
||||||
|
swap(Brew::class, $brewMock);
|
||||||
|
swap(Nginx::class, $nginxMock);
|
||||||
|
|
||||||
$this->assertEquals(['php@7.1', 'php@7.2', 'php@7.3'], resolve(PhpFpm::class)->utilizedPhpVersions());
|
$this->assertEquals(['php@7.1', 'php@7.2', 'php@7.3'], resolve(PhpFpm::class)->utilizedPhpVersions());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_it_lists_isolated_directories()
|
public function test_it_lists_isolated_directories()
|
||||||
{
|
{
|
||||||
$fileSystemMock = Mockery::mock(Filesystem::class);
|
|
||||||
$nginxMock = Mockery::mock(Nginx::class);
|
$nginxMock = Mockery::mock(Nginx::class);
|
||||||
$site = Mockery::mock(Site::class);
|
$siteMock = Mockery::mock(Site::class);
|
||||||
|
$fileSystemMock = Mockery::mock(Filesystem::class);
|
||||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
|
||||||
Mockery::mock(Brew::class),
|
|
||||||
Mockery::mock(CommandLine::class),
|
|
||||||
$fileSystemMock,
|
|
||||||
resolve(Configuration::class),
|
|
||||||
$site,
|
|
||||||
$nginxMock,
|
|
||||||
])->makePartial();
|
|
||||||
|
|
||||||
swap(PhpFpm::class, $phpFpmMock);
|
|
||||||
|
|
||||||
$nginxMock->shouldReceive('configuredSites')
|
$nginxMock->shouldReceive('configuredSites')
|
||||||
->once()
|
->once()
|
||||||
->andReturn(collect(['isolated-site-71.test', 'isolated-site-72.test', 'not-isolated-site.test']));
|
->andReturn(collect(['isolated-site-71.test', 'isolated-site-72.test', 'not-isolated-site.test']));
|
||||||
|
|
||||||
$site->shouldReceive('customPhpVersion')->with('isolated-site-71.test')->andReturn('71');
|
$siteMock->shouldReceive('customPhpVersion')->with('isolated-site-71.test')->andReturn('71');
|
||||||
$site->shouldReceive('customPhpVersion')->with('isolated-site-72.test')->andReturn('72');
|
$siteMock->shouldReceive('customPhpVersion')->with('isolated-site-72.test')->andReturn('72');
|
||||||
$site->shouldReceive('normalizePhpVersion')->with('71')->andReturn('php@7.1');
|
$siteMock->shouldReceive('normalizePhpVersion')->with('71')->andReturn('php@7.1');
|
||||||
$site->shouldReceive('normalizePhpVersion')->with('72')->andReturn('php@7.2');
|
$siteMock->shouldReceive('normalizePhpVersion')->with('72')->andReturn('php@7.2');
|
||||||
|
|
||||||
$sites = [
|
$sites = [
|
||||||
[
|
[
|
||||||
@@ -162,6 +182,10 @@ public function test_it_lists_isolated_directories()
|
|||||||
$fileSystemMock->shouldReceive('get')->once()->with(VALET_HOME_PATH.'/Nginx/'.$site['site'])->andReturn($site['conf']);
|
$fileSystemMock->shouldReceive('get')->once()->with(VALET_HOME_PATH.'/Nginx/'.$site['site'])->andReturn($site['conf']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
swap(Nginx::class, $nginxMock);
|
||||||
|
swap(Site::class, $siteMock);
|
||||||
|
swap(Filesystem::class, $fileSystemMock);
|
||||||
|
|
||||||
$this->assertEquals([
|
$this->assertEquals([
|
||||||
[
|
[
|
||||||
'url' => 'isolated-site-71.test',
|
'url' => 'isolated-site-71.test',
|
||||||
@@ -400,24 +424,17 @@ public function test_un_isolate_will_remove_isolation_for_a_site()
|
|||||||
|
|
||||||
public function test_isolate_will_throw_if_site_is_not_parked_or_linked()
|
public function test_isolate_will_throw_if_site_is_not_parked_or_linked()
|
||||||
{
|
{
|
||||||
$siteMock = Mockery::mock(Site::class);
|
$brewMock = Mockery::mock(Brew::class);
|
||||||
|
|
||||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
swap(Brew::class, $brewMock);
|
||||||
Mockery::mock(Brew::class),
|
swap(Nginx::class, Mockery::mock(Nginx::class));
|
||||||
resolve(CommandLine::class),
|
|
||||||
resolve(Filesystem::class),
|
|
||||||
resolve(Configuration::class),
|
|
||||||
$siteMock,
|
|
||||||
Mockery::mock(Nginx::class),
|
|
||||||
])->makePartial();
|
|
||||||
|
|
||||||
$this->expectException(DomainException::class);
|
$this->expectException(DomainException::class);
|
||||||
$this->expectExceptionMessage("The [test] site could not be found in Valet's site list.");
|
$this->expectExceptionMessage("The [test] site could not be found in Valet's site list.");
|
||||||
|
|
||||||
$siteMock->shouldReceive('getSiteUrl');
|
resolve(PhpFpm::class)->isolateDirectory('test', 'php@8.1');
|
||||||
|
|
||||||
$this->assertSame(null, $phpFpmMock->isolateDirectory('test', 'php@8.1'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class StubForUpdatingFpmConfigFiles extends PhpFpm
|
class StubForUpdatingFpmConfigFiles extends PhpFpm
|
||||||
|
|||||||
@@ -575,9 +575,32 @@ public function test_gets_site_url_from_directory()
|
|||||||
|
|
||||||
$this->assertEquals('site2.test', $site->getSiteUrl('site2'));
|
$this->assertEquals('site2.test', $site->getSiteUrl('site2'));
|
||||||
$this->assertEquals('site2.test', $site->getSiteUrl('site2.test'));
|
$this->assertEquals('site2.test', $site->getSiteUrl('site2.test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_throws_getting_nonexistent_site()
|
||||||
|
{
|
||||||
|
$this->expectException(DomainException::class);
|
||||||
|
$config = Mockery::mock(Configuration::class);
|
||||||
|
|
||||||
|
swap(Configuration::class, $config);
|
||||||
|
|
||||||
|
$siteMock = Mockery::mock(Site::class, [
|
||||||
|
resolve(Configuration::class),
|
||||||
|
resolve(CommandLine::class),
|
||||||
|
resolve(Filesystem::class),
|
||||||
|
])->makePartial();
|
||||||
|
|
||||||
|
swap(Site::class, $siteMock);
|
||||||
|
|
||||||
|
$config->shouldReceive('read')
|
||||||
|
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK, 'paths' => []]);
|
||||||
|
|
||||||
|
$siteMock->shouldReceive('parked')->andReturn(collect());
|
||||||
|
$siteMock->shouldReceive('links')->andReturn(collect([]));
|
||||||
|
$siteMock->shouldReceive('host')->andReturn('site1');
|
||||||
|
|
||||||
|
$site = resolve(Site::class);
|
||||||
$this->assertEquals(false, $site->getSiteUrl('site3'));
|
$this->assertEquals(false, $site->getSiteUrl('site3'));
|
||||||
$this->assertEquals(false, $site->getSiteUrl('site3.test'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_isolation_will_persist_when_adding_ssl_certificate()
|
public function test_isolation_will_persist_when_adding_ssl_certificate()
|
||||||
|
|||||||
Reference in New Issue
Block a user