From 04b14cf9d12aacae89130795d002554e541af6be Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Fri, 2 Dec 2022 13:02:14 -0500 Subject: [PATCH] Call writer() in lifecycle hook Thanks to @dragoonis, we were able to simplify the calls to writer() down to a single lifecycle hook. Co-Authored-By: Jacob Baker-Kretzmar Co-Authored-By: Andy Newhouse Co-Authored-By: Tony Messias --- cli/app.php | 86 +++++++++------------------------------------------ composer.json | 3 +- 2 files changed, 16 insertions(+), 73 deletions(-) diff --git a/cli/app.php b/cli/app.php index 3bf613e..ff4d86c 100755 --- a/cli/app.php +++ b/cli/app.php @@ -2,9 +2,13 @@ use Illuminate\Container\Container; use Silly\Application; +use Symfony\Component\Console\ConsoleEvents; +use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\EventDispatcher\EventDispatcher; + use function Valet\info; use function Valet\output; use function Valet\table; @@ -38,6 +42,16 @@ $app = new Application('Laravel Valet', $version); + +$dispatcher = new EventDispatcher(); +$app->setDispatcher($dispatcher); + +$dispatcher->addListener( + ConsoleEvents::COMMAND, + function (ConsoleCommandEvent $event) { + writer($event->getOutput()); + }); + /** * Prune missing directories and symbolic links on every command. */ @@ -51,8 +65,6 @@ * Install Valet and any required services. */ $app->command('install', function (OutputInterface $output) { - writer($output); - Nginx::stop(); Configuration::install(); @@ -80,8 +92,6 @@ * Get or set the TLD currently being used by Valet. */ $app->command('tld [tld]', function (InputInterface $input, OutputInterface $output, $tld = null) { - writer($output); - if ($tld === null) { return output(Configuration::read()['tld']); } @@ -114,8 +124,6 @@ * Get or set the loopback address currently being used by Valet. */ $app->command('loopback [loopback]', function (InputInterface $input, OutputInterface $output, $loopback = null) { - writer($output); - if ($loopback === null) { return output(Configuration::read()['loopback']); } @@ -142,8 +150,6 @@ * Add the current working directory to the paths configuration. */ $app->command('park [path]', function (OutputInterface $output, $path = null) { - writer($output); - Configuration::addPath($path ?: getcwd()); info(($path === null ? 'This' : "The [{$path}]")." directory has been added to Valet's paths.", $output); @@ -153,8 +159,6 @@ * Get all the current sites within paths parked with 'park {path}'. */ $app->command('parked', function (OutputInterface $output) { - writer($output); - $parked = Site::parked(); table(['Site', 'SSL', 'URL', 'Path'], $parked->all()); @@ -164,8 +168,6 @@ * Remove the current working directory from the paths configuration. */ $app->command('forget [path]', function (OutputInterface $output, $path = null) { - writer($output); - Configuration::removePath($path ?: getcwd()); info(($path === null ? 'This' : "The [{$path}]")." directory has been removed from Valet's paths."); @@ -175,8 +177,6 @@ * Register a symbolic link with Valet. */ $app->command('link [name] [--secure]', function (OutputInterface $output, $name, $secure) { - writer($output); - $linkPath = Site::link(getcwd(), $name = $name ?: basename(getcwd())); info('A ['.$name.'] symbolic link has been created in ['.$linkPath.'].'); @@ -190,8 +190,6 @@ * Display all of the registered symbolic links. */ $app->command('links', function (OutputInterface $output) { - writer($output); - $links = Site::links(); table(['Site', 'SSL', 'URL', 'Path', 'PHP Version'], $links->all()); @@ -201,8 +199,6 @@ * Unlink a link from the Valet links directory. */ $app->command('unlink [name]', function (OutputInterface $output, $name) { - writer($output); - info('The ['.Site::unlink($name).'] symbolic link has been removed.'); })->descriptions('Remove the specified Valet link'); @@ -210,8 +206,6 @@ * Secure the given domain with a trusted TLS certificate. */ $app->command('secure [domain] [--expireIn=]', function (OutputInterface $output, $domain = null, $expireIn = 368) { - writer($output); - $url = Site::domain($domain); Site::secure($url, null, $expireIn); @@ -227,8 +221,6 @@ * Stop serving the given domain over HTTPS and remove the trusted TLS certificate. */ $app->command('unsecure [domain] [--all]', function (OutputInterface $output, $domain = null, $all = null) { - writer($output); - if ($all) { Site::unsecureAll(); @@ -248,8 +240,6 @@ * Display all of the currently secured sites. */ $app->command('secured', function (OutputInterface $output) { - writer($output); - $sites = collect(Site::secured())->map(function ($url) { return ['Site' => $url]; }); @@ -261,8 +251,6 @@ * Create an Nginx proxy config for the specified domain. */ $app->command('proxy domain host [--secure]', function (OutputInterface $output, $domain, $host, $secure) { - writer($output); - Site::proxyCreate($domain, $host, $secure); Nginx::restart(); })->descriptions('Create an Nginx proxy site for the specified host. Useful for docker, mailhog etc.', [ @@ -273,8 +261,6 @@ * Delete an Nginx proxy config. */ $app->command('unproxy domain', function (OutputInterface $output, $domain) { - writer($output); - Site::proxyDelete($domain); Nginx::restart(); })->descriptions('Delete an Nginx proxy config.'); @@ -283,8 +269,6 @@ * Display all of the sites that are proxies. */ $app->command('proxies', function (OutputInterface $output) { - writer($output); - $proxies = Site::proxies(); table(['Site', 'SSL', 'URL', 'Host'], $proxies->all()); @@ -294,8 +278,6 @@ * Determine which Valet driver the current directory is using. */ $app->command('which', function (OutputInterface $output) { - writer($output); - require __DIR__.'/drivers/require.php'; $driver = ValetDriver::assign(getcwd(), basename(getcwd()), '/'); @@ -311,8 +293,6 @@ * Display all of the registered paths. */ $app->command('paths', function (OutputInterface $output) { - writer($output); - $paths = Configuration::read()['paths']; if (count($paths) > 0) { @@ -326,8 +306,6 @@ * Open the current or given directory in the browser. */ $app->command('open [domain]', function (OutputInterface $output, $domain = null) { - writer($output); - $url = 'http://'.Site::domain($domain); CommandLine::runAsUser('open '.escapeshellarg($url)); })->descriptions('Open the site for the current (or specified) directory in your browser'); @@ -336,8 +314,6 @@ * Generate a publicly accessible URL for your project. */ $app->command('share', function (OutputInterface $output) { - writer($output); - warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.'); })->descriptions('Generate a publicly accessible URL for your project'); @@ -345,8 +321,6 @@ * Echo the currently tunneled URL. */ $app->command('fetch-share-url [domain]', function (OutputInterface $output, $domain = null) { - writer($output); - output(Ngrok::currentTunnelUrl(Site::domain($domain))); })->descriptions('Get the URL to the current Ngrok tunnel'); @@ -354,8 +328,6 @@ * Start the daemon services. */ $app->command('start [service]', function (OutputInterface $output, $service) { - writer($output); - switch ($service) { case '': DnsMasq::restart(); @@ -384,8 +356,6 @@ * Restart the daemon services. */ $app->command('restart [service]', function (OutputInterface $output, $service) { - writer($output); - switch ($service) { case '': DnsMasq::restart(); @@ -414,8 +384,6 @@ * Stop the daemon services. */ $app->command('stop [service]', function (OutputInterface $output, $service) { - writer($output); - switch ($service) { case '': PhpFpm::stopRunning(); @@ -439,8 +407,6 @@ * Uninstall Valet entirely. Requires --force to actually remove; otherwise manual instructions are displayed. */ $app->command('uninstall [--force]', function (InputInterface $input, OutputInterface $output, $force) { - writer($output); - if ($force) { warning('YOU ARE ABOUT TO UNINSTALL Nginx, PHP, Dnsmasq and all Valet configs and logs.'); $helper = $this->getHelperSet()->get('question'); @@ -534,8 +500,6 @@ * Determine if this is the latest release of Valet. */ $app->command('on-latest-version', function (OutputInterface $output) use ($version) { - writer($output); - if (Valet::onLatestVersion($version)) { output('Yes'); } else { @@ -548,8 +512,6 @@ * Install the sudoers.d entries so password is no longer required. */ $app->command('trust [--off]', function (OutputInterface $output, $off) { - writer($output); - if ($off) { Brew::removeSudoersEntry(); Valet::removeSudoersEntry(); @@ -569,8 +531,6 @@ * Allow the user to change the version of php Valet uses. */ $app->command('use [phpVersion] [--force]', function (OutputInterface $output, $phpVersion, $force) { - writer($output); - if (! $phpVersion) { $site = basename(getcwd()); $linkedVersion = Brew::linkedPhp(); @@ -602,8 +562,6 @@ * Allow the user to change the version of PHP Valet uses to serve the current site. */ $app->command('isolate [phpVersion] [--site=]', function (OutputInterface $output, $phpVersion, $site = null) { - writer($output); - if (! $site) { $site = basename(getcwd()); } @@ -622,8 +580,6 @@ * Allow the user to un-do specifying the version of PHP Valet uses to serve the current site. */ $app->command('unisolate [--site=]', function (OutputInterface $output, $site = null) { - writer($output); - if (! $site) { $site = basename(getcwd()); } @@ -637,8 +593,6 @@ * List isolated sites. */ $app->command('isolated', function (OutputInterface $output) { - writer($output); - $sites = PhpFpm::isolatedDirectories(); table(['Path', 'PHP Version'], $sites->all()); @@ -648,8 +602,6 @@ * Get the PHP executable path for a site. */ $app->command('which-php [site]', function (OutputInterface $output, $site) { - writer($output); - $phpVersion = Site::customPhpVersion( Site::host($site ?: getcwd()).'.'.Configuration::read()['tld'] ); @@ -667,8 +619,6 @@ * Proxy commands through to an isolated site's version of PHP. */ $app->command('php [command]', function (OutputInterface $output, $command) { - writer($output); - warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.'); })->descriptions("Proxy PHP commands with isolated site's PHP executable", [ 'command' => "Command to run with isolated site's PHP executable", @@ -678,8 +628,6 @@ * Proxy commands through to an isolated site's version of Composer. */ $app->command('composer [command]', function (OutputInterface $output, $command) { - writer($output); - warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.'); })->descriptions("Proxy Composer commands with isolated site's PHP executable", [ 'command' => "Composer command to run with isolated site's PHP executable", @@ -689,8 +637,6 @@ * Tail log file. */ $app->command('log [-f|--follow] [-l|--lines=] [key]', function (OutputInterface $output, $follow, $lines, $key = null) { - writer($output); - $defaultLogs = [ 'php-fpm' => BREW_PREFIX.'/var/log/php-fpm.log', 'nginx' => VALET_HOME_PATH.'/Log/nginx-error.log', @@ -759,8 +705,6 @@ * Configure or display the directory-listing setting. */ $app->command('directory-listing [status]', function (OutputInterface $output, $status = null) { - writer($output); - $key = 'directory-listing'; $config = Configuration::read(); @@ -781,8 +725,6 @@ * Output diagnostics to aid in debugging Valet. */ $app->command('diagnose [-p|--print] [--plain]', function (OutputInterface $output, $print, $plain) { - writer($output); - info('Running diagnostics... (this may take a while)'); Diagnose::run($print, $plain); diff --git a/composer.json b/composer.json index 7081349..7503fdb 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "symfony/console": "^3.0|^4.0|^5.0|^6.0", "symfony/process": "^3.0|^4.0|^5.0|^6.0", "tightenco/collect": "^5.3|^6.0|^7.0|^8.0", - "guzzlehttp/guzzle": "^6.0|^7.4" + "guzzlehttp/guzzle": "^6.0|^7.4", + "symfony/event-dispatcher": "^5.4" }, "require-dev": { "mockery/mockery": "^1.2.3",