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

🐛 Fixing Nginx configuration.

 Adding alias generation and launch daemon generation.
This commit is contained in:
Mikaël Popowicz
2021-02-22 18:43:39 +01:00
parent 545c075b35
commit 0c0ab726c0
7 changed files with 132 additions and 15 deletions

View File

@@ -75,14 +75,12 @@ function installServer()
{
$this->files->ensureDirExists(BREW_PREFIX.'/etc/nginx/valet');
$loopback = $this->configuration->read()['loopback'];
$this->files->putAsUser(
BREW_PREFIX.'/etc/nginx/valet/valet.conf',
str_replace(
['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX'],
[$loopback, VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX],
$this->files->get(__DIR__.'/../stubs/valet.conf')
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX'],
[VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX],
$this->replaceLoopback($this->files->get(__DIR__.'/../stubs/valet.conf'))
)
);
@@ -92,6 +90,23 @@ function installServer()
);
}
function replaceLoopback($siteConf)
{
$loopback = $this->configuration->read()['loopback'];
if ($loopback === VALET_LOOPBACK) {
return $siteConf;
}
$str = '#listen VALET_LOOPBACK:80 default_server; # valet loopback';
return str_replace(
$str,
substr(str_replace('VALET_LOOPBACK', $loopback, $str), 1),
$siteConf
);
}
/**
* Install the Nginx configuration directory to the ~/.config/valet directory.
*

View File

@@ -424,15 +424,26 @@ function resecureForNewLoopback($oldLoopback, $loopback)
*/
function replaceOldLoopbackWithNew($siteConf, $old, $new)
{
$shouldComment = $new === VALET_LOOPBACK;
$lookups = [];
$lookups[] = '~listen .*:80;~';
$lookups[] = '~listen .*:443 ssl http2;~';
$lookups[] = '~listen .*:60;~';
$lookups[] = '~#?listen .*:80; # valet loopback~';
$lookups[] = '~#?listen .*:443 ssl http2; # valet loopback~';
$lookups[] = '~#?listen .*:60; # valet loopback~';
foreach ($lookups as $lookup) {
preg_match($lookup, $siteConf, $matches);
foreach ($matches as $match) {
$replaced = str_replace($old, $new, $match);
if ($shouldComment && strpos($replaced, '#') !== 0) {
$replaced = '#'.$replaced;
}
if (! $shouldComment) {
$replaced = ltrim($replaced, '#');
}
$siteConf = str_replace($match, $replaced, $siteConf);
}
}
@@ -752,6 +763,62 @@ function proxyDelete($url)
info('Valet will no longer proxy [https://'.$url.'].');
}
/**
* Remove old loopback interface alias and new one if necessary.
*
* @param string $oldLoopback
* @param string $loopback
* @return void
*/
function aliasLoopback($oldLoopback, $loopback)
{
if ($oldLoopback !== VALET_LOOPBACK) {
$this->cli->run(sprintf(
'sudo ifconfig lo0 -alias %s', $oldLoopback
));
info('Old loopback interface alias removed ['.$oldLoopback.']');
}
if ($loopback !== VALET_LOOPBACK) {
$this->cli->run(sprintf(
'sudo ifconfig lo0 alias %s', $loopback
));
info('New loopback interface alias added ['.$loopback.']');
}
$this->updatePlist($loopback);
}
/**
* Remove old LaunchDaemon and create a new one if necessary.
*
* @param string $loopback
* @return void
*/
function updatePlist($loopback)
{
if ($this->files->exists($this->plistPath())) {
$this->files->unlink($this->plistPath());
info('Old plist file removed ['.$this->plistPath().']');
}
if ($loopback !== VALET_LOOPBACK) {
$this->files->put(
$this->plistPath(),
str_replace(
'VALET_LOOPBACK',
$loopback,
$this->files->get(__DIR__.'/../stubs/ifconfig.plist')
)
);
info('New plist file added ['.$this->plistPath().']');
}
}
function valetHomePath()
{
return VALET_HOME_PATH;
@@ -762,6 +829,16 @@ function valetLoopback()
return $this->config->read()['loopback'];
}
/**
* Get the path to loopback LaunchDaemon.
*
* @return string
*/
function plistPath()
{
return '/Library/LaunchDaemons/com.laravel.valet.ifconfig.plist';
}
/**
* Get the path to Nginx site configuration files.
*/