diff --git a/cli/Valet/Hosts.php b/cli/Valet/Hosts.php index eee1eaa..f0992e5 100644 --- a/cli/Valet/Hosts.php +++ b/cli/Valet/Hosts.php @@ -48,11 +48,49 @@ public function link(string $siteName) return; } - $contents[] = "127.0.0.1 $url #" . self::comment . "\r\n"; + $contentsText = "127.0.0.1 $url"; + + for ($i = 0; $i < 24 - strlen($url); $i++) { + $contentsText .= ' '; + } + $contents[] = $contentsText . "#" . self::comment . "\r\n"; $this->files->put(self::hosts_path, implode('', $contents)); } + /** + * Clear all linked websites from the hosts file. + * + * @return void + */ + public function clear() + { + $domain = $this->configuration->get('domain'); + + $contents = file(self::hosts_path); + $contentsCount = count($contents); + + for ($i = 0; $i < $contentsCount; $i++) { + if (strpos($contents[$i], '.' . $domain) !== false) { + unset($contents[$i]); + } + } + + $this->files->put(self::hosts_path, implode('', $contents)); + } + + /** + * Add all the websites currently linked to the hosts file. + * + * @return void + */ + public function linkAll() + { + foreach ($this->files->scanDir(VALET_HOME_PATH . '/Sites') as $site) { + $this->link($site); + } + } + /** * Remove an existing record from the hosts file. * diff --git a/cli/Valet/Valet.php b/cli/Valet/Valet.php index e8110bb..dbb2918 100644 --- a/cli/Valet/Valet.php +++ b/cli/Valet/Valet.php @@ -114,13 +114,32 @@ public function getLatestVersion() * @return void */ public function environmentSetup() + { + $this->packageManagerSetup(); + $this->serviceManagerSetup(); + } + + /** + * Require admin rights. + * + * @return void + */ + public function checkAdminRights() { if ((!$this->files->writable(VALET_HOSTS_PATH) || !$this->files->readable(VALET_HOSTS_PATH))) { warning('You must start WSL as administrator.'); exit(); } - $this->packageManagerSetup(); - $this->serviceManagerSetup(); + } + + /** + * Check if the user has admin rights. + * + * @return bool + */ + public function hasAdminRights() + { + return $this->files->writable(VALET_HOSTS_PATH) && $this->files->readable(VALET_HOSTS_PATH); } /** diff --git a/cli/includes/helpers.php b/cli/includes/helpers.php index 7f9b384..44e003f 100644 --- a/cli/includes/helpers.php +++ b/cli/includes/helpers.php @@ -23,7 +23,7 @@ */ function info($output) { - output('' . $output . ''); + output('' . $output . ''); } /** @@ -38,6 +38,18 @@ function warning($output) output('' . $output . ''); } +/** + * Output the given text to the console. + * + * @param string $output + * + * @return void + */ +function success($output) +{ + output('' . $output . ''); +} + /** * Output a table to the console. * diff --git a/cli/valet.php b/cli/valet.php index 7ac8391..9ded9fd 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -67,16 +67,21 @@ return info(Configuration::read()['domain']); } + Valet::checkAdminRights(); + + Hosts::clear(); + $oldDomain = Configuration::read()['domain']; $domain = trim($domain, '.'); Configuration::updateKey('domain', $domain); + Hosts::linkAll(); Site::resecureForNewDomain($oldDomain, $domain); // Mailhog::updateDomain(); PhpFpm::restart(); Nginx::restart(); - info('Your Valet domain has been updated to [' . $domain . '].'); + success('Your Valet domain has been updated to [' . $domain . '].'); })->descriptions('Get or set the domain used for Valet sites'); /** @@ -105,7 +110,7 @@ PhpFpm::restart(); $protocol = $https ? 'HTTPS' : 'HTTP'; - info("Your Nginx {$protocol} port has been updated to [{$port}]."); + success("Your Nginx {$protocol} port has been updated to [{$port}]."); })->descriptions('Get or set the port number used for Valet sites'); /** @@ -134,7 +139,7 @@ Configuration::addPath($path ?: getcwd()); - info(($path === null ? 'This' : "The [{$path}]") . " directory has been added to Valet's paths."); + success(($path === null ? 'This' : "The [{$path}]") . " directory has been added to Valet's paths."); })->descriptions('Register the current working (or specified) directory with Valet'); /** @@ -146,7 +151,7 @@ Configuration::removePath($path ?: getcwd()); - info(($path === null ? 'This' : "The [{$path}]") . " directory has been removed from Valet's paths."); + success(($path === null ? 'This' : "The [{$path}]") . " directory has been removed from Valet's paths."); })->descriptions('Remove the current working (or specified) directory from Valet\'s list of paths'); /** @@ -161,11 +166,13 @@ * Register a symbolic link with Valet. */ $app->command('link [name]', function ($name) { + Valet::checkAdminRights(); + $linkPath = Site::link(getcwd(), $name = $name ?: basename(getcwd())); Hosts::link($name); - info('A [' . $name . '] symbolic link has been created in [' . $linkPath . '].'); + success('A [' . $name . '] symbolic link has been created in [' . $linkPath . '].'); })->descriptions('Link the current working directory to Valet'); /** @@ -181,43 +188,51 @@ * Unlink a link from the Valet links directory. */ $app->command('unlink [name]', function ($name) { + Valet::checkAdminRights(); + Site::unlink($name = $name ?: basename(getcwd())); Hosts::unlink($name); - info('The [' . $name . '] symbolic link has been removed.'); + success('The [' . $name . '] symbolic link has been removed.'); })->descriptions('Remove the specified Valet link'); /** * Secure the given domain with a trusted TLS certificate. */ $app->command('secure [domain]', function ($domain = null) { + Valet::checkAdminRights(); + $url = ($domain ?: Site::host(getcwd())) . '.' . Configuration::read()['domain']; Site::secure($url); PhpFpm::restart(); Nginx::restart(); - info('The [' . $url . '] site has been secured with a fresh TLS certificate.'); + success('The [' . $url . '] site has been secured with a fresh TLS certificate.'); })->descriptions('Secure the given domain with a trusted TLS certificate'); /** * Stop serving the given domain over HTTPS and remove the trusted TLS certificate. */ $app->command('unsecure [domain]', function ($domain = null) { + Valet::checkAdminRights(); + $url = ($domain ?: Site::host(getcwd())) . '.' . Configuration::read()['domain']; Site::unsecure($url); PhpFpm::restart(); Nginx::restart(); - info('The [' . $url . '] site will now serve traffic over HTTP.'); + success('The [' . $url . '] site will now serve traffic over HTTP.'); })->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate'); /** * Register a subdomain link. */ $app->command('subdomain:create [name] [--secure]', function ($name, $secure) { + Valet::checkAdminRights(); + $name = $name ?: 'www'; Site::link(getcwd(), $name . '.' . basename(getcwd())); @@ -226,17 +241,19 @@ } $domain = Configuration::read()['domain']; - info('Subdomain ' . $name . '.' . basename(getcwd()) . '.' . $domain . ' created'); + success('Subdomain ' . $name . '.' . basename(getcwd()) . '.' . $domain . ' created'); })->descriptions('Create a subdomains'); /** * Unregister a subdomain link. */ $app->command('subdomain:remove [name]', function ($name) { + Valet::checkAdminRights(); + $name = $name ?: 'www'; Site::unlink($name . '.' . basename(getcwd())); $domain = Configuration::read()['domain']; - info('Subdomain ' . $name . '.' . basename(getcwd()) . '.' . $domain . ' removed'); + success('Subdomain ' . $name . '.' . basename(getcwd()) . '.' . $domain . ' removed'); })->descriptions('Remove a subdomains'); /** @@ -256,9 +273,9 @@ $driver = ValetDriver::assign(getcwd(), basename(getcwd()), '/'); if ($driver) { - info('This site is served by [' . get_class($driver) . '].'); + success('This site is served by [' . get_class($driver) . '].'); } else { - warning('Valet could not determine which driver to use for this site.'); + info('Valet could not determine which driver to use for this site.'); } })->descriptions('Determine which Valet driver serves the current working directory'); @@ -268,13 +285,13 @@ $app->command('paths', function () { warning('Currently this command is disabled as we can\'t achieve dynamic domains without a DNS Proxy.'); exit(); - $paths = Configuration::read()['paths']; + // $paths = Configuration::read()['paths']; - if (count($paths) > 0) { - info(json_encode($paths, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); - } else { - warning('No paths have been registered.'); - } + // if (count($paths) > 0) { + // info(json_encode($paths, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + // } else { + // warning('No paths have been registered.'); + // } })->descriptions('Get all of the paths registered with Valet'); /** @@ -317,7 +334,7 @@ // Mailhog::restart(); Mysql::restart(); ValetRedis::restart(); - info('Valet services have been started.'); + success('Valet services have been started.'); return; } @@ -345,7 +362,7 @@ } } - info('Specified Valet services have been started.'); + success('Specified Valet services have been started.'); })->descriptions('Start the Valet services'); /** @@ -358,7 +375,7 @@ // Mailhog::restart(); Mysql::restart(); ValetRedis::restart(); - info('Valet services have been restarted.'); + success('Valet services have been restarted.'); return; } @@ -400,7 +417,7 @@ } } - info('Specified Valet services have been restarted.'); + success('Specified Valet services have been restarted.'); })->descriptions('Restart the Valet services'); /** @@ -416,7 +433,7 @@ // Elasticsearch::stop(); // RabbitMq::stop(); // Varnish::stop(); - info('Valet services have been stopped.'); + success('Valet services have been stopped.'); return; } @@ -458,20 +475,21 @@ } } - info('Specified Valet services have been stopped.'); + success('Specified Valet services have been stopped.'); })->descriptions('Stop the Valet services'); /** * Uninstall Valet entirely. */ $app->command('uninstall', function () { + Hosts::clear(); Nginx::uninstall(); PhpFpm::uninstall(); // Mailhog::uninstall(); Configuration::uninstall(); Valet::uninstall(); - info('Valet has been uninstalled.'); + success('Valet has been uninstalled.'); })->descriptions('Uninstall the Valet services'); /** @@ -481,11 +499,11 @@ $script = dirname(__FILE__) . '/scripts/update.sh'; if (Valet::onLatestVersion($version)) { - info('You have the latest version of Valet WSL'); + success('You have the latest version of Valet WSL'); passthru($script); } else { - warning('There is a new release of Valet WSL'); - warning('Updating now...'); + info('There is a new release of Valet WSL'); + info('Updating now...'); $latestVersion = Valet::getLatestVersion(); if ($latestVersion) { passthru($script . " update $latestVersion"); @@ -501,7 +519,7 @@ $app->command('use [preferedversion] [--update-cli]', function ($preferedversion = null, $updateCli = null) { info('Changing php-fpm version...'); PhpFpm::changeVersion($preferedversion, $updateCli); - info('php-fpm version successfully changed! 🎉'); + success('php-fpm version successfully changed! 🎉'); })->descriptions('Set the PHP-fpm version to use, enter "default" or leave empty to use version: ' . PhpFpm::getVersion(true), [ '--update-cli' => 'Updates CLI version as well', ]); @@ -582,7 +600,7 @@ return; } - info("Database [{$database_name}] reset successfully"); + success("Database [{$database_name}] reset successfully"); })->descriptions('Clear all tables for given database in MySQL'); /** @@ -622,7 +640,7 @@ info('Exporting database...'); $defaults = $input->getOptions(); $data = Mysql::exportDatabase($database_name, $defaults['sql']); - info("Database [{$data['database']}] exported into file {$data['filename']}"); + success("Database [{$data['database']}] exported into file {$data['filename']}"); })->descriptions('Export selected MySQL database'); /** diff --git a/valet b/valet index 9a5e569..6d8171f 100755 --- a/valet +++ b/valet @@ -43,7 +43,7 @@ fi # Valet CLI script which is written in PHP. Will use PHP to do it. if [ ! -f "$DIR/cli/valet.php" ] then - DIR=$(php -r "echo realpath('$DIR/../laravel/valet');") + DIR=$(php -r "echo realpath('$DIR/../eptic/valet-wsl');") fi # If the command is one of the commands that requires "sudo" privileges @@ -59,14 +59,14 @@ then fi fi -if [[ -n $2 && "domain port" =~ $1 ]] -then - if [[ "$EUID" -ne 0 ]] - then - sudo env HOME=$HOMEPATH $SOURCE "$@" - exit 0 - fi -fi +# if [[ -n $2 && "domain port" =~ $1 ]] +# then +# if [[ "$EUID" -ne 0 ]] +# then +# sudo env HOME=$HOMEPATH $SOURCE "$@" +# exit 0 +# fi +# fi # If the command is the "share" command we will need to resolve out any # symbolic links for the site. Before starting Ngrok, we will fire a