Skip to content

Commit

Permalink
StyleCI & Tests updates for Multiple PHP Versions
Browse files Browse the repository at this point in the history
  • Loading branch information
NasirNobin authored Feb 6, 2022
1 parent 5c865d3 commit 3d73ef6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
30 changes: 14 additions & 16 deletions cli/Valet/PhpFpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function uninstall()
*/
public function updateConfiguration($phpVersion = null)
{
info(sprintf('Updating PHP configuration%s...', $phpVersion ? ' for ' . $phpVersion : ''));
info(sprintf('Updating PHP configuration%s...', $phpVersion ? ' for '.$phpVersion : ''));

$fpmConfigFile = $this->fpmConfigPath($phpVersion);

Expand Down Expand Up @@ -97,7 +97,7 @@ public function updateConfiguration($phpVersion = null)
}

if ($phpVersion) {
$contents = str_replace("valet.sock", $this->fpmSockName($phpVersion), $contents);
$contents = str_replace('valet.sock', $this->fpmSockName($phpVersion), $contents);
}

$this->files->put($fpmConfigFile, $contents);
Expand Down Expand Up @@ -150,7 +150,7 @@ public function stop()
*/
public function fpmConfigPath($phpVersion = null)
{
if(! $phpVersion){
if (! $phpVersion) {
$phpVersion = $this->brew->linkedPhp();
}

Expand Down Expand Up @@ -189,7 +189,7 @@ public function useVersion($version, $force = false, $site = null)
$version = $this->validateRequestedVersion($version);

try {
if ($this->brew->linkedPhp() === $version && !$force) {
if ($this->brew->linkedPhp() === $version && ! $force) {
output(sprintf('<info>Valet is already using version: <comment>%s</comment>.</info> To re-link and re-configure use the --force parameter.'.PHP_EOL,
$version));
exit();
Expand All @@ -204,7 +204,7 @@ public function useVersion($version, $force = false, $site = null)

// we need to unlink and link only for global php version change
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);
} else {
// Unlink the current php if there is one
Expand All @@ -216,8 +216,8 @@ public function useVersion($version, $force = false, $site = null)
$this->updateConfiguration($linkedPhp);

// check all custom nginx config files
// look for the php version and maybe delete that custom config
$this->swapConfigForGlobalPhpUpdate($version, $linkedPhp);
// look for the php version and update config files accordingly
$this->updateConfigurationForGlobalUpdate($version, $linkedPhp);

$currentVersion = $this->brew->getLinkedPhpFormula();
info(sprintf('Unlinking current version: %s', $currentVersion));
Expand Down Expand Up @@ -285,31 +285,29 @@ public function validateRequestedVersion($version)
}

/**
* Get fpm sock file name from a given php version
* Get fpm sock file name from a given php version.
*
* @param string|null $phpVersion
* @return string
*/
function fpmSockName($phpVersion = null)
public function fpmSockName($phpVersion = null)
{
$versionInteger = preg_replace('~[^\d]~', '', $phpVersion);

return "valet{$versionInteger}.sock";
}

/**
* 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
* 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.
*
* @param $newPhpVersion
* @param $oldPhpVersion
*/
private function swapConfigForGlobalPhpUpdate($newPhpVersion, $oldPhpVersion)
public function updateConfigurationForGlobalUpdate($newPhpVersion, $oldPhpVersion)
{
collect($this->files->scandir(VALET_HOME_PATH.'/Nginx'))
->filter(function ($file) {
return !starts_with($file, '.');
return ! starts_with($file, '.');
})
->each(function ($file) use ($newPhpVersion, $oldPhpVersion) {
$content = $this->files->get(VALET_HOME_PATH.'/Nginx/'.$file);
Expand All @@ -329,7 +327,7 @@ private function swapConfigForGlobalPhpUpdate($newPhpVersion, $oldPhpVersion)
}

/**
* Get the PHP versions to perform restart
* Get the PHP versions to perform restart.
*
* @return array
*/
Expand All @@ -346,7 +344,7 @@ public function getPhpVersionsToPerformRestart()

return collect($this->files->scandir(VALET_HOME_PATH.'/Nginx'))
->filter(function ($file) {
return !starts_with($file, '.');
return ! starts_with($file, '.');
})
->map(function ($file) use ($fpmSockFiles) {
$content = $this->files->get(VALET_HOME_PATH.'/Nginx/'.$file);
Expand Down
18 changes: 8 additions & 10 deletions cli/Valet/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ public function proxies()
}

/**
* Determine if the provided site is parked
* Determine if the provided site is parked.
*
* @param $valetSite
* @return bool
*/
function isValidSite($valetSite)
public function isValidSite($valetSite)
{
// remove .tld to make the search a bit easier
$siteName = str_replace('.'.$this->config->read()['tld'], '', $valetSite);
Expand Down Expand Up @@ -508,7 +508,6 @@ public function secure($url, $siteConf = null, $certificateExpireInDays = 396, $
// if user had any isolated php version, let's swap the .sock file,
// so it still uses the old php version
if ($phpVersion) {

$siteConf = $this->replaceSockFile($siteConf, "valet{$phpVersion}.sock", $phpVersion);
}

Expand Down Expand Up @@ -694,15 +693,14 @@ public function buildSecureNginxServer($url, $siteConf = null)
* @param string $valetSite
* @param string $fpmSockName
* @param $phpVersion
*
* @return void
*/
public function installSiteConfig($valetSite, $fpmSockName, $phpVersion)
{
if ($this->files->exists($this->nginxPath($valetSite))) {
$siteConf = $this->files->get($this->nginxPath($valetSite));
$siteConf = $this->replaceSockFile($siteConf, $fpmSockName, $phpVersion);
}else{
} else {
$siteConf = str_replace(
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PHP_FPM_SOCKET', 'VALET_ISOLATED_PHP_VERSION'],
[VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX, $valetSite, $fpmSockName, $phpVersion],
Expand Down Expand Up @@ -1022,7 +1020,7 @@ public function certificatesPath($url = null, $extension = null)
}

/**
* Replace Loopback
* Replace Loopback.
*
* @param string $siteConf
* @return string
Expand All @@ -1045,7 +1043,7 @@ public function replaceLoopback($siteConf)
}

/**
* Extract PHP version of exising nginx conifg
* Extract PHP version of exising nginx conifg.
*
* @param $url
* @return string|void
Expand All @@ -1057,13 +1055,13 @@ public function extractPhpVersion($url)

if (starts_with($siteConf, '# Valet isolated PHP version')) {
$firstLine = explode(PHP_EOL, $siteConf)[0];
return preg_replace("/[^\d]*/", "", $firstLine);
return preg_replace("/[^\d]*/", '', $firstLine);
}
}
}

/**
* Replace .sock file form a nginx site conf
* Replace .sock file form a Nginx site conf.
*
* @param $siteConf
* @param $sockFile
Expand All @@ -1072,7 +1070,7 @@ public function extractPhpVersion($url)
*/
public function replaceSockFile($siteConf, $sockFile, $phpVersion)
{
$siteConf = preg_replace("/valet[0-9]*.sock/", $sockFile, $siteConf);
$siteConf = preg_replace('/valet[0-9]*.sock/', $sockFile, $siteConf);

if (! starts_with($siteConf, '# Valet isolated PHP version')) {
$siteConf = '# Valet isolated PHP version : '.$phpVersion.PHP_EOL.$siteConf;
Expand Down
1 change: 0 additions & 1 deletion cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,6 @@
info(sprintf('Valet is now using %s.', $newVersion).PHP_EOL);
info('Note that you might need to run <comment>composer global update</comment> if your PHP version change affects the dependencies of global packages required by Composer.');
}

})->descriptions('Change the version of PHP used by valet', [
'phpVersion' => 'The PHP version you want to use, e.g [email protected]',
'--site' => 'Isolate PHP version of a specific valet site. e.g: --site=site.test',
Expand Down
3 changes: 2 additions & 1 deletion tests/PhpFpmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
resolve(Filesystem::class),
])->makePartial();
$phpFpmMock->shouldReceive('install');
$phpFpmMock->shouldReceive('updateConfigurationForGlobalUpdate');

$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
'[email protected]',
Expand All @@ -138,7 +139,7 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal

class StubForUpdatingFpmConfigFiles extends PhpFpm
{
public function fpmConfigPath()
public function fpmConfigPath($phpVersion = null)
{
return __DIR__.'/output/fpm.conf';
}
Expand Down

0 comments on commit 3d73ef6

Please sign in to comment.