1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-06 08:40:09 +01:00

Apply suggestions from code review

Co-authored-by: Matt Stauffer <mattstauffer@users.noreply.github.com>
This commit is contained in:
Nasir Uddin Nobin
2022-02-15 02:05:48 +06:00
committed by GitHub
parent 37b4af4113
commit acf190c57c
2 changed files with 35 additions and 34 deletions

View File

@@ -33,7 +33,7 @@ public function __construct(Brew $brew, CommandLine $cli, Filesystem $files)
/** /**
* Install and configure PhpFpm. * Install and configure PhpFpm.
* *
* @param null $phpVersion * @param string|null $phpVersion
* @return void * @return void
*/ */
public function install($phpVersion = null) public function install($phpVersion = null)
@@ -64,7 +64,7 @@ public function uninstall()
/** /**
* Update the PHP FPM configuration. * Update the PHP FPM configuration.
* *
* @param null $phpVersion * @param string|null $phpVersion
* @return void * @return void
*/ */
public function updateConfiguration($phpVersion = null) public function updateConfiguration($phpVersion = null)
@@ -181,7 +181,7 @@ public function stopRunning()
* *
* @param $version * @param $version
* @param bool $force * @param bool $force
* @param null $site * @param string|null $site
* @return string * @return string
*/ */
public function useVersion($version, $force = false, $site = null) public function useVersion($version, $force = false, $site = null)
@@ -202,21 +202,20 @@ public function useVersion($version, $force = false, $site = null)
$this->brew->ensureInstalled($version, [], $this->taps); $this->brew->ensureInstalled($version, [], $this->taps);
} }
// we need to unlink and link only for global php version change // Delete old Valet sock files, install the new version, and, if this is a global change, unlink and link PHP
if ($site) { if ($site) {
$this->cli->quietly('sudo rm '.VALET_HOME_PATH.'/'.$this->fpmSockName($version)); $this->cli->quietly('sudo rm '.VALET_HOME_PATH.'/'.$this->fpmSockName($version));
$this->install($version); $this->install($version);
} else { } else {
// Unlink the current php if there is one // Unlink the current global PHP if there is one installed
if ($this->brew->hasLinkedPhp()) { if ($this->brew->hasLinkedPhp()) {
$linkedPhp = $this->brew->linkedPhp(); $linkedPhp = $this->brew->linkedPhp();
// updating old fpm to use a custom sock // Update the old FPM to keep running, using a custom sock file, so existing isolated sites aren't broken
// so exising lokced php versioned sites doesn't mess up
$this->updateConfiguration($linkedPhp); $this->updateConfiguration($linkedPhp);
// check all custom nginx config files // Update existing custom Nginx config files; if they're using the old or new PHP version,
// look for the php version and update config files accordingly // update them to the new correct sock file location
$this->updateConfigurationForGlobalUpdate($version, $linkedPhp); $this->updateConfigurationForGlobalUpdate($version, $linkedPhp);
$currentVersion = $this->brew->getLinkedPhpFormula(); $currentVersion = $this->brew->getLinkedPhpFormula();
@@ -285,7 +284,7 @@ public function validateRequestedVersion($version)
} }
/** /**
* Get fpm sock file name from a given php version. * Get FPM sock file name for a given PHP version.
* *
* @param string|null $phpVersion * @param string|null $phpVersion
* @return string * @return string
@@ -298,16 +297,21 @@ public function fpmSockName($phpVersion = null)
} }
/** /**
* Preseve exising isolated PHP versioned sites, when running a gloabl php version update. Look for the php version and will adjust that custom nginx config. * Update all existing Nginx files when running a global PHP version update.
* If a given file is pointing to `valet.sock`, it's targeting the old global PHP version;
* update it to point to the new custom sock file for that version.
* If a given file is pointing the custom sock file for the new global version, that new
* version will now be hosted at `valet.sock`, so update the config file to point to that instead.
* *
* @param $newPhpVersion * @param $newPhpVersion
* @param $oldPhpVersion * @param $oldPhpVersion
* @return void
*/ */
public function updateConfigurationForGlobalUpdate($newPhpVersion, $oldPhpVersion) public function updateConfigurationForGlobalUpdate($newPhpVersion, $oldPhpVersion)
{ {
collect($this->files->scandir(VALET_HOME_PATH.'/Nginx')) collect($this->files->scandir(VALET_HOME_PATH.'/Nginx'))
->filter(function ($file) { ->reject(function ($file) {
return ! starts_with($file, '.'); return starts_with($file, '.');
}) })
->each(function ($file) use ($newPhpVersion, $oldPhpVersion) { ->each(function ($file) use ($newPhpVersion, $oldPhpVersion) {
$content = $this->files->get(VALET_HOME_PATH.'/Nginx/'.$file); $content = $this->files->get(VALET_HOME_PATH.'/Nginx/'.$file);
@@ -327,32 +331,29 @@ public function updateConfigurationForGlobalUpdate($newPhpVersion, $oldPhpVersio
} }
/** /**
* Get the PHP versions to perform restart. * Get a list of all PHP versions currently serving "isolated sites" (sites with custom Nginx
* configs pointing them to a specific PHP version).
* *
* @return array * @return array
*/ */
public function getPhpVersionsToPerformRestart() public function getPhpVersionsToPerformRestart()
{ {
// scan through custom nginx files
// look for config file, that is using custom .sock files (example: php74.sock)
// restart all those PHP FPM though valet
// to make sure all the custom php versioned sites keep running
$fpmSockFiles = $this->brew->supportedPhpVersions()->map(function ($version) { $fpmSockFiles = $this->brew->supportedPhpVersions()->map(function ($version) {
return $this->fpmSockName($this->normalizePhpVersion($version)); return $this->fpmSockName($this->normalizePhpVersion($version));
})->unique(); })->unique();
return collect($this->files->scandir(VALET_HOME_PATH.'/Nginx')) return collect($this->files->scandir(VALET_HOME_PATH.'/Nginx'))
->filter(function ($file) { ->reject(function ($file) {
return ! starts_with($file, '.'); return starts_with($file, '.');
}) })
->map(function ($file) use ($fpmSockFiles) { ->map(function ($file) use ($fpmSockFiles) {
$content = $this->files->get(VALET_HOME_PATH.'/Nginx/'.$file); $content = $this->files->get(VALET_HOME_PATH.'/Nginx/'.$file);
// Get the normalized PHP version for this config file, if it's defined
foreach ($fpmSockFiles as $sock) { foreach ($fpmSockFiles as $sock) {
if (strpos($content, $sock) !== false) { if (strpos($content, $sock) !== false) {
// find the PHP version number from .sock path // Extract the PHP version number from a custom .sock path;
// valet74.sock will outout php74 // for example, "valet74.sock" will output "php74"
$phpVersion = 'php'.str_replace(['valet', '.sock'], '', $sock); $phpVersion = 'php'.str_replace(['valet', '.sock'], '', $sock);
return $this->normalizePhpVersion($phpVersion); // example output php@7.4 return $this->normalizePhpVersion($phpVersion); // example output php@7.4

View File

@@ -191,14 +191,14 @@ public function proxies()
} }
/** /**
* Determine if the provided site is parked. * Determine if the provided site is a valid site, whether parked or linked.
* *
* @param $valetSite * @param string $valetSite
* @return bool * @return bool
*/ */
public function isValidSite($valetSite) public function isValidSite($valetSite)
{ {
// remove .tld to make the search a bit easier // Remove .tld from sitename if it was provided
$siteName = str_replace('.'.$this->config->read()['tld'], '', $valetSite); $siteName = str_replace('.'.$this->config->read()['tld'], '', $valetSite);
return $this->parked()->merge($this->links())->where('site', $siteName)->count() > 0; return $this->parked()->merge($this->links())->where('site', $siteName)->count() > 0;
@@ -488,7 +488,8 @@ public function secured()
*/ */
public function secure($url, $siteConf = null, $certificateExpireInDays = 396, $caExpireInYears = 20) public function secure($url, $siteConf = null, $certificateExpireInDays = 396, $caExpireInYears = 20)
{ {
$phpVersion = $this->extractPhpVersion($url); // let's try to preserve the isolated php version here. Example output: 74 // Extract in order to later preserve custom PHP version config when securing
$phpVersion = $this->extractPhpVersion($url);
$this->unsecure($url); $this->unsecure($url);
@@ -505,8 +506,7 @@ public function secure($url, $siteConf = null, $certificateExpireInDays = 396, $
$siteConf = $this->buildSecureNginxServer($url, $siteConf); $siteConf = $this->buildSecureNginxServer($url, $siteConf);
// if user had any isolated php version, let's swap the .sock file, // If they user had isolated the PHP version for this site, swap out .sock file
// so it still uses the old php version
if ($phpVersion) { if ($phpVersion) {
$siteConf = $this->replaceSockFile($siteConf, "valet{$phpVersion}.sock", $phpVersion); $siteConf = $this->replaceSockFile($siteConf, "valet{$phpVersion}.sock", $phpVersion);
} }
@@ -1020,7 +1020,7 @@ public function certificatesPath($url = null, $extension = null)
} }
/** /**
* Replace Loopback. * Replace Loopback configuration line in Valet site configuration file contents.
* *
* @param string $siteConf * @param string $siteConf
* @return string * @return string
@@ -1062,11 +1062,11 @@ public function extractPhpVersion($url)
} }
/** /**
* Replace .sock file form a Nginx site conf. * Replace .sock file in an Nginx site configuration file contents.
* *
* @param $siteConf * @param string $siteConf
* @param $sockFile * @param string $sockFile
* @param $phpVersion * @param string $phpVersion
* @return string * @return string
*/ */
public function replaceSockFile($siteConf, $sockFile, $phpVersion) public function replaceSockFile($siteConf, $sockFile, $phpVersion)