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

Merge pull request #241 from drbyte/tld-alias-for-domain-command

Rename `valet domain` command to `valet tld`
This commit is contained in:
Matt Stauffer
2018-08-23 11:52:52 -04:00
committed by GitHub
8 changed files with 59 additions and 48 deletions

View File

@@ -111,7 +111,15 @@ function createCertificatesDirectory()
function writeBaseConfiguration()
{
if (! $this->files->exists($this->path())) {
$this->write(['domain' => 'test', 'paths' => []]);
$this->write(['tld' => 'test', 'paths' => []]);
}
/**
* For upgrades, change config key 'domain' to 'tld'
*/
$config = $this->read();
if (isset($config['domain']) && !isset($config['tld'])) {
$this->updateKey('tld', $config['domain']);
}
}

View File

@@ -33,27 +33,29 @@ function __construct(Brew $brew, CommandLine $cli, Filesystem $files)
*
* @return void
*/
function install($domain = 'test')
function install($tld = 'test')
{
$this->brew->ensureInstalled('dnsmasq');
// For DnsMasq, we create our own custom configuration file which will be imported
// in the main DnsMasq file. This allows Valet to make changes to our own files
// without needing to modify the "primary" DnsMasq configuration files again.
$this->createCustomConfigFile($domain);
$this->createCustomConfigFile($tld);
$this->createDomainResolver($domain);
$this->createTldResolver($tld);
$this->brew->restartService('dnsmasq');
info('Valet is configured to serve for TLD [.'.$tld.']');
}
/**
* Append the custom DnsMasq configuration file to the main configuration file.
*
* @param string $domain
* @param string $tld
* @return void
*/
function createCustomConfigFile($domain)
function createCustomConfigFile($tld)
{
$customConfigPath = $this->customConfigPath();
@@ -61,7 +63,7 @@ function createCustomConfigFile($domain)
$this->appendCustomConfigImport($customConfigPath);
$this->files->putAsUser($customConfigPath, 'address=/.'.$domain.'/127.0.0.1'.PHP_EOL.'listen-address=127.0.0.1'.PHP_EOL);
$this->files->putAsUser($customConfigPath, 'address=/.'.$tld.'/127.0.0.1'.PHP_EOL.'listen-address=127.0.0.1'.PHP_EOL);
}
/**
@@ -107,30 +109,30 @@ function customConfigIsBeingImported($customConfigPath)
}
/**
* Create the resolver file to point the configured domain to 127.0.0.1.
* Create the resolver file to point the configured TLD to 127.0.0.1.
*
* @param string $domain
* @param string $tld
* @return void
*/
function createDomainResolver($domain)
function createTldResolver($tld)
{
$this->files->ensureDirExists($this->resolverPath);
$this->files->put($this->resolverPath.'/'.$domain, 'nameserver 127.0.0.1'.PHP_EOL);
$this->files->put($this->resolverPath.'/'.$tld, 'nameserver 127.0.0.1'.PHP_EOL);
}
/**
* Update the domain used by DnsMasq.
* Update the TLD/domain resolved by DnsMasq.
*
* @param string $oldDomain
* @param string $newDomain
* @param string $oldTld
* @param string $newTld
* @return void
*/
function updateDomain($oldDomain, $newDomain)
function updateTld($oldTld, $newTld)
{
$this->files->unlink($this->resolverPath.'/'.$oldDomain);
$this->files->unlink($this->resolverPath.'/'.$oldTld);
$this->install($newDomain);
$this->install($newTld);
}
/**

View File

@@ -130,9 +130,9 @@ function ($exitCode, $outputMessage) {
*/
function rewriteSecureNginxFiles()
{
$domain = $this->configuration->read()['domain'];
$tld = $this->configuration->read()['tld'];
$this->site->resecureForNewDomain($domain, $domain);
$this->site->resecureForNewTld($tld, $tld);
}
/**

View File

@@ -86,7 +86,8 @@ function getCertificates($path)
return collect($this->files->scandir($path))->filter(function ($value, $key) {
return ends_with($value, '.crt');
})->map(function ($cert) {
return substr($cert, 0, strripos($cert, '.', -5));
$certWithoutSuffix = substr($cert, 0, -4);
return substr($certWithoutSuffix, 0, strrpos($certWithoutSuffix, '.'));
})->flip();
}
@@ -105,7 +106,7 @@ function getLinks($path, $certs)
return [$site => $this->files->readLink($path.'/'.$site)];
})->map(function ($path, $site) use ($certs, $config) {
$secured = $certs->has($site);
$url = ($secured ? 'https': 'http').'://'.$site.'.'.$config['domain'];
$url = ($secured ? 'https': 'http').'://'.$site.'.'.$config['tld'];
return [$site, $secured ? ' X': '', $url, $path];
});
@@ -137,13 +138,13 @@ function pruneLinks()
}
/**
* Resecure all currently secured sites with a fresh domain.
* Resecure all currently secured sites with a fresh tld.
*
* @param string $oldDomain
* @param string $domain
* @param string $oldTld
* @param string $tld
* @return void
*/
function resecureForNewDomain($oldDomain, $domain)
function resecureForNewTld($oldTld, $tld)
{
if (! $this->files->exists($this->certificatesPath())) {
return;
@@ -156,7 +157,7 @@ function resecureForNewDomain($oldDomain, $domain)
}
foreach ($secured as $url) {
$this->secure(str_replace('.'.$oldDomain, '.'.$domain, $url));
$this->secure(str_replace('.'.$oldTld, '.'.$tld, $url));
}
}

View File

@@ -44,7 +44,7 @@
Configuration::install();
Nginx::install();
PhpFpm::install();
DnsMasq::install(Configuration::read()['domain']);
DnsMasq::install(Configuration::read()['tld']);
Nginx::restart();
Valet::symlinkToUsersBin();
@@ -56,25 +56,25 @@
*/
if (is_dir(VALET_HOME_PATH)) {
/**
* Get or set the domain currently being used by Valet.
* Get or set the TLD currently being used by Valet.
*/
$app->command('domain [domain]', function ($domain = null) {
if ($domain === null) {
return info(Configuration::read()['domain']);
$app->command('tld [tld]', function ($tld = null) {
if ($tld === null) {
return info('Valet is configured to serve for TLD: .'.Configuration::read()['tld']);
}
DnsMasq::updateDomain(
$oldDomain = Configuration::read()['domain'], $domain = trim($domain, '.')
DnsMasq::updateTld(
$oldTld = Configuration::read()['tld'], $tld = trim($tld, '.')
);
Configuration::updateKey('domain', $domain);
Configuration::updateKey('tld', $tld);
Site::resecureForNewDomain($oldDomain, $domain);
Site::resecureForNewTld($oldTld, $tld);
PhpFpm::restart();
Nginx::restart();
info('Your Valet domain has been updated to ['.$domain.'].');
})->descriptions('Get or set the domain used for Valet sites');
info('Your Valet TLD has been updated to ['.$tld.'].');
}, ['domain'])->descriptions('Get or set the TLD used for Valet sites. (Currently: .'.Configuration::read()['tld'].')');
/**
* Add the current working directory to the paths configuration.
@@ -129,7 +129,7 @@
* Secure the given domain with a trusted TLS certificate.
*/
$app->command('secure [domain]', function ($domain = null) {
$url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['domain'];
$url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
Site::secure($url);
@@ -144,7 +144,7 @@
* Stop serving the given domain over HTTPS and remove the trusted TLS certificate.
*/
$app->command('unsecure [domain]', function ($domain = null) {
$url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['domain'];
$url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
Site::unsecure($url);
@@ -187,7 +187,7 @@
* Open the current or given directory in the browser.
*/
$app->command('open [domain]', function ($domain = null) {
$url = "http://".($domain ?: Site::host(getcwd())).'.'.Configuration::read()['domain'];
$url = "http://".($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
CommandLine::runAsUser("open ".escapeshellarg($url));
})->descriptions('Open the site for the current (or specified) directory in your browser');

View File

@@ -18,9 +18,9 @@ function show_valet_404()
}
/**
* @param $domain string Domain to filter
*
* @return string Filtered domain (without wildcard dns feature (xip.io/nip.io))
* You may use wildcard DNS providers xip.io or nip.io as a tool for testing your site via an IP address.
* It's simple to use: First determine the IP address of your local computer (like 192.168.0.10).
* Then simply use http://project.your-ip.xip.io - ie: http://laravel.192.168.0.10.xip.io
*/
function valet_support_wildcard_dns($domain)
{
@@ -66,7 +66,7 @@ function valet_default_site_path($config)
$siteName = basename(
// Filter host to support wildcard dns feature
valet_support_wildcard_dns($_SERVER['HTTP_HOST']),
'.'.$valetConfig['domain']
'.'.$valetConfig['tld']
);
if (strpos($siteName, 'www.') === 0) {

View File

@@ -53,13 +53,13 @@ public function test_install_installs_and_places_configuration_files_in_proper_l
}
public function test_update_domain_removes_old_resolver_and_reinstalls()
public function test_update_tld_removes_old_resolver_and_reinstalls()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('quietly')->with('rm /etc/resolver/old');
$dnsMasq = Mockery::mock(DnsMasq::class.'[install]', [resolve(Brew::class), $cli, new Filesystem]);
$dnsMasq->shouldReceive('install')->with('new');
$dnsMasq->updateDomain('old', 'new');
$dnsMasq->updateTld('old', 'new');
}
}

View File

@@ -81,13 +81,13 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files()
$files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Nginx/.keep', "\n")->once();
swap(Filesystem::class, $files);
swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['domain' => 'test']]));
swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test']]));
swap(Site::class, $site = Mockery::spy(Site::class));
$nginx = resolve(Nginx::class);
$nginx->installNginxDirectory();
$site->shouldHaveReceived('resecureForNewDomain', ['test', 'test']);
$site->shouldHaveReceived('resecureForNewTld', ['test', 'test']);
}
}