From 95aa03977c03bbabf21b86fedcada82914970372 Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Mon, 14 Mar 2022 00:16:36 -0400 Subject: [PATCH] Add command to list isolated sites --- cli/Valet/Nginx.php | 13 +++++++++++++ cli/Valet/PhpFpm.php | 22 +++++++++++++++++----- cli/valet.php | 11 ++++++++++- tests/NginxTest.php | 22 ++++++++++++++++++++++ tests/PhpFpmTest.php | 15 +++++++++------ 5 files changed, 71 insertions(+), 12 deletions(-) diff --git a/cli/Valet/Nginx.php b/cli/Valet/Nginx.php index 0ab30d4..f27dfdd 100644 --- a/cli/Valet/Nginx.php +++ b/cli/Valet/Nginx.php @@ -177,4 +177,17 @@ public function uninstall() $this->brew->uninstallFormula('nginx nginx-full'); $this->cli->quietly('rm -rf '.BREW_PREFIX.'/etc/nginx '.BREW_PREFIX.'/var/log/nginx'); } + + /** + * Return a list of all sites with explicit Nginx configurations + * + * @return \Illuminate\Support\Collection + */ + public function configuredSites() + { + return collect($this->files->scandir(VALET_HOME_PATH.'/Nginx')) + ->reject(function ($file) { + return starts_with($file, '.'); + }); + } } diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index 9abd337..3acd13e 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -243,6 +243,22 @@ public function unIsolateDirectory($directory) info(sprintf('The site [%s] is now using the default PHP version.', $site)); } + /** + * List all directories with PHP isolation configured. + * + * @return \Illuminate\Support\Collection + */ + public function isolatedDirectories() + { + $configuredSites = $this->nginx->configuredSites(); + + return $configuredSites->filter(function ($item) { + return str_contains($this->files->get(VALET_HOME_PATH.'/Nginx/'.$item), 'Valet isolated PHP version'); + })->map(function ($item) { + return ['url' => $item]; + }); + } + /** * Use a specific version of PHP globally. * @@ -366,11 +382,7 @@ public function utilizedPhpVersions() return self::fpmSockName($this->normalizePhpVersion($version)); })->unique(); - return collect($this->files->scandir(VALET_HOME_PATH.'/Nginx')) - ->reject(function ($file) { - return starts_with($file, '.'); - }) - ->map(function ($file) use ($fpmSockFiles) { + return $this->nginx->configuredSites()->map(function ($file) use ($fpmSockFiles) { $content = $this->files->get(VALET_HOME_PATH.'/Nginx/'.$file); // Get the normalized PHP version for this config file, if it's defined diff --git a/cli/valet.php b/cli/valet.php index 507e7b5..663060b 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -528,7 +528,16 @@ */ $app->command('unisolate', function () { PhpFpm::unIsolateDirectory(basename(getcwd())); - })->descriptions('Stop customizing the version of PHP used by valet to serve the current working directory'); + })->descriptions('Stop customizing the version of PHP used by Valet to serve the current working directory'); + + /** + * List isolated sites + */ + $app->command('isolated', function () { + $sites = PhpFpm::isolatedDirectories(); + + table(['Path'], $sites->all()); + })->descriptions('List all sites using isolated versions of PHP.'); /** * Tail log file. diff --git a/tests/NginxTest.php b/tests/NginxTest.php index e2f7805..51f35a6 100644 --- a/tests/NginxTest.php +++ b/tests/NginxTest.php @@ -86,4 +86,26 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files() $site->shouldHaveReceived('resecureForNewConfiguration', [$data, $data]); } + + public function test_it_gets_configured_sites() + { + $files = Mockery::mock(Filesystem::class); + + $files->shouldReceive('scandir') + ->once() + ->with(VALET_HOME_PATH . '/Nginx') + ->andReturn(['.gitkeep', 'isolated-site-71.test', 'isolated-site-72.test', 'isolated-site-73.test']); + + swap(Filesystem::class, $files); + swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test', 'loopback' => VALET_LOOPBACK]])); + swap(Site::class, Mockery::mock(Site::class)); + + $nginx = resolve(Nginx::class); + $output = $nginx->configuredSites(); + + $this->assertEquals( + ['isolated-site-71.test', 'isolated-site-72.test', 'isolated-site-73.test'], + $output->values()->all() + ); + } } diff --git a/tests/PhpFpmTest.php b/tests/PhpFpmTest.php index fa7caf3..9247e64 100644 --- a/tests/PhpFpmTest.php +++ b/tests/PhpFpmTest.php @@ -64,6 +64,7 @@ public function test_utilized_php_versions() { $fileSystemMock = Mockery::mock(Filesystem::class); $brewMock = Mockery::mock(Brew::class); + $nginxMock = Mockery::mock(Nginx::class); $phpFpmMock = Mockery::mock(PhpFpm::class, [ $brewMock, @@ -71,7 +72,7 @@ public function test_utilized_php_versions() $fileSystemMock, resolve(Configuration::class), Mockery::mock(Site::class), - Mockery::mock(Nginx::class), + $nginxMock, ])->makePartial(); swap(PhpFpm::class, $phpFpmMock); @@ -85,12 +86,9 @@ public function test_utilized_php_versions() $brewMock->shouldReceive('getLinkedPhpFormula')->andReturn('php@7.3'); - $fileSystemMock->shouldReceive('scandir') + $nginxMock->shouldReceive('configuredSites') ->once() - ->with(VALET_HOME_PATH.'/Nginx') - ->andReturn(['.gitkeep', 'isolated-site-71.test', 'isolated-site-72.test', 'isolated-site-73.test']); - - $fileSystemMock->shouldNotReceive('get')->with(VALET_HOME_PATH.'/Nginx/.gitkeep'); + ->andReturn(collect(['isolated-site-71.test', 'isolated-site-72.test', 'isolated-site-73.test'])); $sites = [ [ @@ -114,6 +112,11 @@ public function test_utilized_php_versions() $this->assertEquals(['php@7.1', 'php@7.2', 'php@7.3'], resolve(PhpFpm::class)->utilizedPhpVersions()); } + public function test_it_lists_isolated_directories() + { + $this->markTestIncomplete('@todo'); + } + public function test_stop_unused_php_versions() { $brewMock = Mockery::mock(Brew::class);