Skip to content

Commit a876175

Browse files
♻️ Refactoring resecureForNew* methods to resecureForNewConfiguration.
1 parent 7bb5326 commit a876175

File tree

5 files changed

+48
-60
lines changed

5 files changed

+48
-60
lines changed

cli/Valet/Nginx.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function replaceLoopback($siteConf)
9898
return $siteConf;
9999
}
100100

101-
$str = '#listen VALET_LOOPBACK:80 default_server; # valet loopback';
101+
$str = '#listen VALET_LOOPBACK:80; # valet loopback';
102102

103103
return str_replace(
104104
$str,
@@ -150,8 +150,9 @@ function rewriteSecureNginxFiles()
150150
$tld = $this->configuration->read()['tld'];
151151
$loopback = $this->configuration->read()['loopback'];
152152

153-
$this->site->resecureForNewTld($tld, $tld);
154-
$this->site->resecureForNewLoopback($loopback, $loopback);
153+
$config = compact('tld', 'loopback');
154+
155+
$this->site->resecureForNewConfiguration($config, $config);
155156
}
156157

157158
/**

cli/Valet/Site.php

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -328,28 +328,43 @@ function pruneLinks()
328328
}
329329

330330
/**
331-
* Resecure all currently secured sites with a fresh tld.
331+
* Resecure all currently secured sites with a fresh configuration.
332332
*
333-
* @param string $oldTld
334-
* @param string $tld
333+
* @param array $old
334+
* @param array $new
335335
* @return void
336336
*/
337-
function resecureForNewTld($oldTld, $tld)
337+
function resecureForNewConfiguration(array $old, array $new)
338338
{
339339
if (! $this->files->exists($this->certificatesPath())) {
340340
return;
341341
}
342342

343343
$secured = $this->secured();
344344

345+
$defaultTld = $this->config->read()['tld'];
346+
$oldTld = isset($old['tld']) && ! empty($old['tld']) ? $old['tld'] : $defaultTld;
347+
$tld = isset($new['tld']) && ! empty($new['tld']) ? $new['tld'] : $defaultTld;
348+
349+
$defaultLoopback = $this->config->read()['loopback'];
350+
$oldLoopback = isset($old['loopback']) && ! empty($old['loopback']) ? $old['loopback'] : $defaultLoopback;
351+
$loopback = isset($new['loopback']) && ! empty($new['loopback']) ? $new['loopback'] : $defaultLoopback;
352+
345353
foreach ($secured as $url) {
346354
$newUrl = str_replace('.'.$oldTld, '.'.$tld, $url);
347355
$siteConf = $this->getSiteConfigFileContents($url, '.'.$oldTld);
348356

349357
if (!empty($siteConf) && strpos($siteConf, '# valet stub: proxy.valet.conf') === 0) {
350358
// proxy config
351359
$this->unsecure($url);
352-
$this->secure($newUrl, $this->replaceOldDomainWithNew($siteConf, $url, $newUrl));
360+
$this->secure(
361+
$newUrl,
362+
$this->replaceOldLoopbackWithNew(
363+
$this->replaceOldDomainWithNew($siteConf, $url, $newUrl),
364+
$oldLoopback,
365+
$loopback
366+
)
367+
);
353368
} else {
354369
// normal config
355370
$this->unsecure($url);
@@ -384,36 +399,6 @@ function replaceOldDomainWithNew($siteConf, $old, $new)
384399
return $siteConf;
385400
}
386401

387-
/**
388-
* Resecure all currently secured sites with a fresh loopback address.
389-
*
390-
* @param string $oldLoopback
391-
* @param string $loopback
392-
* @return void
393-
*/
394-
function resecureForNewLoopback($oldLoopback, $loopback)
395-
{
396-
if (! $this->files->exists($this->certificatesPath())) {
397-
return;
398-
}
399-
400-
$secured = $this->secured();
401-
402-
foreach ($secured as $url) {
403-
$siteConf = $this->getSiteConfigFileContents($url);
404-
405-
if (!empty($siteConf) && strpos($siteConf, '# valet stub: proxy.valet.conf') === 0) {
406-
// proxy config
407-
$this->unsecure($url);
408-
$this->secure($url, $this->replaceOldLoopbackWithNew($siteConf, $oldLoopback, $loopback));
409-
} else {
410-
// normal config
411-
$this->unsecure($url);
412-
$this->secure($url);
413-
}
414-
}
415-
}
416-
417402
/**
418403
* Parse Nginx site config file contents to swap old loopback address to new.
419404
*
@@ -639,13 +624,16 @@ function buildCertificateConf($path, $url)
639624
function buildSecureNginxServer($url, $siteConf = null)
640625
{
641626
if ($siteConf === null) {
642-
$siteConf = $this->files->get(__DIR__.'/../stubs/secure.valet.conf');
627+
$siteConf = $this->replaceOldLoopbackWithNew(
628+
$this->files->get(__DIR__.'/../stubs/secure.valet.conf'),
629+
'VALET_LOOPBACK',
630+
$this->valetLoopback()
631+
);
643632
}
644633

645634
return str_replace(
646-
['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'],
635+
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'],
647636
[
648-
$this->valetLoopback(),
649637
$this->valetHomePath(),
650638
VALET_SERVER_PATH,
651639
VALET_STATIC_PREFIX,
@@ -731,11 +719,15 @@ function proxyCreate($url, $host)
731719
$url .= '.'.$tld;
732720
}
733721

734-
$siteConf = $this->files->get(__DIR__.'/../stubs/proxy.valet.conf');
722+
$siteConf = $this->replaceOldLoopbackWithNew(
723+
$this->files->get(__DIR__.'/../stubs/proxy.valet.conf'),
724+
'VALET_LOOPBACK',
725+
$this->valetLoopback()
726+
);
735727

736728
$siteConf = str_replace(
737-
['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PROXY_HOST'],
738-
[$this->valetLoopback(), $this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $host],
729+
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PROXY_HOST'],
730+
[$this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $host],
739731
$siteConf
740732
);
741733

@@ -811,7 +803,7 @@ function updatePlist($loopback)
811803
str_replace(
812804
'VALET_LOOPBACK',
813805
$loopback,
814-
$this->files->get(__DIR__.'/../stubs/ifconfig.plist')
806+
$this->files->get(__DIR__.'/../stubs/loopback.plist')
815807
)
816808
);
817809

@@ -836,7 +828,7 @@ function valetLoopback()
836828
*/
837829
function plistPath()
838830
{
839-
return '/Library/LaunchDaemons/com.laravel.valet.ifconfig.plist';
831+
return '/Library/LaunchDaemons/com.laravel.valet.loopback.plist';
840832
}
841833

842834
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<plist version="1.0">
44
<dict>
55
<key>Label</key>
6-
<string>com.laravel.valet.ifconfig</string>
6+
<string>com.laravel.valet.loopback</string>
77
<key>RunAtLoad</key>
88
<true/>
99
<key>ProgramArguments</key>

cli/valet.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,9 @@
6666
*/
6767
if (is_dir(VALET_HOME_PATH)) {
6868
/**
69-
* Upgrade helper: ensure the tld config exists
69+
* Upgrade helper: ensure the tld config exists or the loopback config exists
7070
*/
71-
if (empty(Configuration::read()['tld'])) {
72-
Configuration::writeBaseConfiguration();
73-
}
74-
75-
/**
76-
* Upgrade helper: ensure the loopback config exists
77-
*/
78-
if (empty(Configuration::read()['loopback'])) {
71+
if (empty(Configuration::read()['tld']) || empty(Configuration::read()['loopback'])) {
7972
Configuration::writeBaseConfiguration();
8073
}
8174

@@ -93,7 +86,7 @@
9386

9487
Configuration::updateKey('tld', $tld);
9588

96-
Site::resecureForNewTld($oldTld, $tld);
89+
Site::resecureForNewConfiguration(['tld' => $oldTld], ['tld' => $tld]);
9790
PhpFpm::restart();
9891
Nginx::restart();
9992

@@ -114,9 +107,9 @@
114107

115108
DnsMasq::refreshConfiguration();
116109
Site::aliasLoopback($oldLoopback, $loopback);
117-
Site::resecureForNewLoopback($oldLoopback, $loopback);
118-
Nginx::installServer();
110+
Site::resecureForNewConfiguration(['loopback' => $oldLoopback], ['loopback' => $loopback]);
119111
PhpFpm::restart();
112+
Nginx::installServer();
120113
Nginx::restart();
121114

122115
info('Your valet loopback address has been updated to ['.$loopback.']');

tests/NginxTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files()
8282
$nginx = resolve(Nginx::class);
8383
$nginx->installNginxDirectory();
8484

85-
$site->shouldHaveReceived('resecureForNewTld', ['test', 'test']);
85+
$data = ['tld' => 'test', 'loopback' => '127.0.0.1'];
86+
87+
$site->shouldHaveReceived('resecureForNewConfiguration', [$data, $data]);
8688
}
8789
}

0 commit comments

Comments
 (0)