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

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 <bakerkretzmar@users.noreply.github.com>
Co-Authored-By: Andy Newhouse <hi@andymnewhouse.me>
Co-Authored-By: Tony Messias <tonyzrp@gmail.com>
This commit is contained in:
Matt Stauffer
2022-12-02 13:02:14 -05:00
parent a0e1e02a47
commit 04b14cf9d1
2 changed files with 16 additions and 73 deletions

View File

@@ -2,9 +2,13 @@
use Illuminate\Container\Container; use Illuminate\Container\Container;
use Silly\Application; use Silly\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\EventDispatcher\EventDispatcher;
use function Valet\info; use function Valet\info;
use function Valet\output; use function Valet\output;
use function Valet\table; use function Valet\table;
@@ -38,6 +42,16 @@
$app = new Application('Laravel Valet', $version); $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. * Prune missing directories and symbolic links on every command.
*/ */
@@ -51,8 +65,6 @@
* Install Valet and any required services. * Install Valet and any required services.
*/ */
$app->command('install', function (OutputInterface $output) { $app->command('install', function (OutputInterface $output) {
writer($output);
Nginx::stop(); Nginx::stop();
Configuration::install(); Configuration::install();
@@ -80,8 +92,6 @@
* Get or set the TLD currently being used by Valet. * Get or set the TLD currently being used by Valet.
*/ */
$app->command('tld [tld]', function (InputInterface $input, OutputInterface $output, $tld = null) { $app->command('tld [tld]', function (InputInterface $input, OutputInterface $output, $tld = null) {
writer($output);
if ($tld === null) { if ($tld === null) {
return output(Configuration::read()['tld']); return output(Configuration::read()['tld']);
} }
@@ -114,8 +124,6 @@
* Get or set the loopback address currently being used by Valet. * Get or set the loopback address currently being used by Valet.
*/ */
$app->command('loopback [loopback]', function (InputInterface $input, OutputInterface $output, $loopback = null) { $app->command('loopback [loopback]', function (InputInterface $input, OutputInterface $output, $loopback = null) {
writer($output);
if ($loopback === null) { if ($loopback === null) {
return output(Configuration::read()['loopback']); return output(Configuration::read()['loopback']);
} }
@@ -142,8 +150,6 @@
* Add the current working directory to the paths configuration. * Add the current working directory to the paths configuration.
*/ */
$app->command('park [path]', function (OutputInterface $output, $path = null) { $app->command('park [path]', function (OutputInterface $output, $path = null) {
writer($output);
Configuration::addPath($path ?: getcwd()); Configuration::addPath($path ?: getcwd());
info(($path === null ? 'This' : "The [{$path}]")." directory has been added to Valet's paths.", $output); 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}'. * Get all the current sites within paths parked with 'park {path}'.
*/ */
$app->command('parked', function (OutputInterface $output) { $app->command('parked', function (OutputInterface $output) {
writer($output);
$parked = Site::parked(); $parked = Site::parked();
table(['Site', 'SSL', 'URL', 'Path'], $parked->all()); table(['Site', 'SSL', 'URL', 'Path'], $parked->all());
@@ -164,8 +168,6 @@
* Remove the current working directory from the paths configuration. * Remove the current working directory from the paths configuration.
*/ */
$app->command('forget [path]', function (OutputInterface $output, $path = null) { $app->command('forget [path]', function (OutputInterface $output, $path = null) {
writer($output);
Configuration::removePath($path ?: getcwd()); Configuration::removePath($path ?: getcwd());
info(($path === null ? 'This' : "The [{$path}]")." directory has been removed from Valet's paths."); info(($path === null ? 'This' : "The [{$path}]")." directory has been removed from Valet's paths.");
@@ -175,8 +177,6 @@
* Register a symbolic link with Valet. * Register a symbolic link with Valet.
*/ */
$app->command('link [name] [--secure]', function (OutputInterface $output, $name, $secure) { $app->command('link [name] [--secure]', function (OutputInterface $output, $name, $secure) {
writer($output);
$linkPath = Site::link(getcwd(), $name = $name ?: basename(getcwd())); $linkPath = Site::link(getcwd(), $name = $name ?: basename(getcwd()));
info('A ['.$name.'] symbolic link has been created in ['.$linkPath.'].'); info('A ['.$name.'] symbolic link has been created in ['.$linkPath.'].');
@@ -190,8 +190,6 @@
* Display all of the registered symbolic links. * Display all of the registered symbolic links.
*/ */
$app->command('links', function (OutputInterface $output) { $app->command('links', function (OutputInterface $output) {
writer($output);
$links = Site::links(); $links = Site::links();
table(['Site', 'SSL', 'URL', 'Path', 'PHP Version'], $links->all()); table(['Site', 'SSL', 'URL', 'Path', 'PHP Version'], $links->all());
@@ -201,8 +199,6 @@
* Unlink a link from the Valet links directory. * Unlink a link from the Valet links directory.
*/ */
$app->command('unlink [name]', function (OutputInterface $output, $name) { $app->command('unlink [name]', function (OutputInterface $output, $name) {
writer($output);
info('The ['.Site::unlink($name).'] symbolic link has been removed.'); info('The ['.Site::unlink($name).'] symbolic link has been removed.');
})->descriptions('Remove the specified Valet link'); })->descriptions('Remove the specified Valet link');
@@ -210,8 +206,6 @@
* Secure the given domain with a trusted TLS certificate. * Secure the given domain with a trusted TLS certificate.
*/ */
$app->command('secure [domain] [--expireIn=]', function (OutputInterface $output, $domain = null, $expireIn = 368) { $app->command('secure [domain] [--expireIn=]', function (OutputInterface $output, $domain = null, $expireIn = 368) {
writer($output);
$url = Site::domain($domain); $url = Site::domain($domain);
Site::secure($url, null, $expireIn); Site::secure($url, null, $expireIn);
@@ -227,8 +221,6 @@
* Stop serving the given domain over HTTPS and remove the trusted TLS certificate. * 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) { $app->command('unsecure [domain] [--all]', function (OutputInterface $output, $domain = null, $all = null) {
writer($output);
if ($all) { if ($all) {
Site::unsecureAll(); Site::unsecureAll();
@@ -248,8 +240,6 @@
* Display all of the currently secured sites. * Display all of the currently secured sites.
*/ */
$app->command('secured', function (OutputInterface $output) { $app->command('secured', function (OutputInterface $output) {
writer($output);
$sites = collect(Site::secured())->map(function ($url) { $sites = collect(Site::secured())->map(function ($url) {
return ['Site' => $url]; return ['Site' => $url];
}); });
@@ -261,8 +251,6 @@
* Create an Nginx proxy config for the specified domain. * Create an Nginx proxy config for the specified domain.
*/ */
$app->command('proxy domain host [--secure]', function (OutputInterface $output, $domain, $host, $secure) { $app->command('proxy domain host [--secure]', function (OutputInterface $output, $domain, $host, $secure) {
writer($output);
Site::proxyCreate($domain, $host, $secure); Site::proxyCreate($domain, $host, $secure);
Nginx::restart(); Nginx::restart();
})->descriptions('Create an Nginx proxy site for the specified host. Useful for docker, mailhog etc.', [ })->descriptions('Create an Nginx proxy site for the specified host. Useful for docker, mailhog etc.', [
@@ -273,8 +261,6 @@
* Delete an Nginx proxy config. * Delete an Nginx proxy config.
*/ */
$app->command('unproxy domain', function (OutputInterface $output, $domain) { $app->command('unproxy domain', function (OutputInterface $output, $domain) {
writer($output);
Site::proxyDelete($domain); Site::proxyDelete($domain);
Nginx::restart(); Nginx::restart();
})->descriptions('Delete an Nginx proxy config.'); })->descriptions('Delete an Nginx proxy config.');
@@ -283,8 +269,6 @@
* Display all of the sites that are proxies. * Display all of the sites that are proxies.
*/ */
$app->command('proxies', function (OutputInterface $output) { $app->command('proxies', function (OutputInterface $output) {
writer($output);
$proxies = Site::proxies(); $proxies = Site::proxies();
table(['Site', 'SSL', 'URL', 'Host'], $proxies->all()); table(['Site', 'SSL', 'URL', 'Host'], $proxies->all());
@@ -294,8 +278,6 @@
* Determine which Valet driver the current directory is using. * Determine which Valet driver the current directory is using.
*/ */
$app->command('which', function (OutputInterface $output) { $app->command('which', function (OutputInterface $output) {
writer($output);
require __DIR__.'/drivers/require.php'; require __DIR__.'/drivers/require.php';
$driver = ValetDriver::assign(getcwd(), basename(getcwd()), '/'); $driver = ValetDriver::assign(getcwd(), basename(getcwd()), '/');
@@ -311,8 +293,6 @@
* Display all of the registered paths. * Display all of the registered paths.
*/ */
$app->command('paths', function (OutputInterface $output) { $app->command('paths', function (OutputInterface $output) {
writer($output);
$paths = Configuration::read()['paths']; $paths = Configuration::read()['paths'];
if (count($paths) > 0) { if (count($paths) > 0) {
@@ -326,8 +306,6 @@
* Open the current or given directory in the browser. * Open the current or given directory in the browser.
*/ */
$app->command('open [domain]', function (OutputInterface $output, $domain = null) { $app->command('open [domain]', function (OutputInterface $output, $domain = null) {
writer($output);
$url = 'http://'.Site::domain($domain); $url = 'http://'.Site::domain($domain);
CommandLine::runAsUser('open '.escapeshellarg($url)); CommandLine::runAsUser('open '.escapeshellarg($url));
})->descriptions('Open the site for the current (or specified) directory in your browser'); })->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. * Generate a publicly accessible URL for your project.
*/ */
$app->command('share', function (OutputInterface $output) { $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.'); 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'); })->descriptions('Generate a publicly accessible URL for your project');
@@ -345,8 +321,6 @@
* Echo the currently tunneled URL. * Echo the currently tunneled URL.
*/ */
$app->command('fetch-share-url [domain]', function (OutputInterface $output, $domain = null) { $app->command('fetch-share-url [domain]', function (OutputInterface $output, $domain = null) {
writer($output);
output(Ngrok::currentTunnelUrl(Site::domain($domain))); output(Ngrok::currentTunnelUrl(Site::domain($domain)));
})->descriptions('Get the URL to the current Ngrok tunnel'); })->descriptions('Get the URL to the current Ngrok tunnel');
@@ -354,8 +328,6 @@
* Start the daemon services. * Start the daemon services.
*/ */
$app->command('start [service]', function (OutputInterface $output, $service) { $app->command('start [service]', function (OutputInterface $output, $service) {
writer($output);
switch ($service) { switch ($service) {
case '': case '':
DnsMasq::restart(); DnsMasq::restart();
@@ -384,8 +356,6 @@
* Restart the daemon services. * Restart the daemon services.
*/ */
$app->command('restart [service]', function (OutputInterface $output, $service) { $app->command('restart [service]', function (OutputInterface $output, $service) {
writer($output);
switch ($service) { switch ($service) {
case '': case '':
DnsMasq::restart(); DnsMasq::restart();
@@ -414,8 +384,6 @@
* Stop the daemon services. * Stop the daemon services.
*/ */
$app->command('stop [service]', function (OutputInterface $output, $service) { $app->command('stop [service]', function (OutputInterface $output, $service) {
writer($output);
switch ($service) { switch ($service) {
case '': case '':
PhpFpm::stopRunning(); PhpFpm::stopRunning();
@@ -439,8 +407,6 @@
* Uninstall Valet entirely. Requires --force to actually remove; otherwise manual instructions are displayed. * Uninstall Valet entirely. Requires --force to actually remove; otherwise manual instructions are displayed.
*/ */
$app->command('uninstall [--force]', function (InputInterface $input, OutputInterface $output, $force) { $app->command('uninstall [--force]', function (InputInterface $input, OutputInterface $output, $force) {
writer($output);
if ($force) { if ($force) {
warning('YOU ARE ABOUT TO UNINSTALL Nginx, PHP, Dnsmasq and all Valet configs and logs.'); warning('YOU ARE ABOUT TO UNINSTALL Nginx, PHP, Dnsmasq and all Valet configs and logs.');
$helper = $this->getHelperSet()->get('question'); $helper = $this->getHelperSet()->get('question');
@@ -534,8 +500,6 @@
* Determine if this is the latest release of Valet. * Determine if this is the latest release of Valet.
*/ */
$app->command('on-latest-version', function (OutputInterface $output) use ($version) { $app->command('on-latest-version', function (OutputInterface $output) use ($version) {
writer($output);
if (Valet::onLatestVersion($version)) { if (Valet::onLatestVersion($version)) {
output('Yes'); output('Yes');
} else { } else {
@@ -548,8 +512,6 @@
* Install the sudoers.d entries so password is no longer required. * Install the sudoers.d entries so password is no longer required.
*/ */
$app->command('trust [--off]', function (OutputInterface $output, $off) { $app->command('trust [--off]', function (OutputInterface $output, $off) {
writer($output);
if ($off) { if ($off) {
Brew::removeSudoersEntry(); Brew::removeSudoersEntry();
Valet::removeSudoersEntry(); Valet::removeSudoersEntry();
@@ -569,8 +531,6 @@
* Allow the user to change the version of php Valet uses. * Allow the user to change the version of php Valet uses.
*/ */
$app->command('use [phpVersion] [--force]', function (OutputInterface $output, $phpVersion, $force) { $app->command('use [phpVersion] [--force]', function (OutputInterface $output, $phpVersion, $force) {
writer($output);
if (! $phpVersion) { if (! $phpVersion) {
$site = basename(getcwd()); $site = basename(getcwd());
$linkedVersion = Brew::linkedPhp(); $linkedVersion = Brew::linkedPhp();
@@ -602,8 +562,6 @@
* Allow the user to change the version of PHP Valet uses to serve the current site. * 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) { $app->command('isolate [phpVersion] [--site=]', function (OutputInterface $output, $phpVersion, $site = null) {
writer($output);
if (! $site) { if (! $site) {
$site = basename(getcwd()); $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. * 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) { $app->command('unisolate [--site=]', function (OutputInterface $output, $site = null) {
writer($output);
if (! $site) { if (! $site) {
$site = basename(getcwd()); $site = basename(getcwd());
} }
@@ -637,8 +593,6 @@
* List isolated sites. * List isolated sites.
*/ */
$app->command('isolated', function (OutputInterface $output) { $app->command('isolated', function (OutputInterface $output) {
writer($output);
$sites = PhpFpm::isolatedDirectories(); $sites = PhpFpm::isolatedDirectories();
table(['Path', 'PHP Version'], $sites->all()); table(['Path', 'PHP Version'], $sites->all());
@@ -648,8 +602,6 @@
* Get the PHP executable path for a site. * Get the PHP executable path for a site.
*/ */
$app->command('which-php [site]', function (OutputInterface $output, $site) { $app->command('which-php [site]', function (OutputInterface $output, $site) {
writer($output);
$phpVersion = Site::customPhpVersion( $phpVersion = Site::customPhpVersion(
Site::host($site ?: getcwd()).'.'.Configuration::read()['tld'] Site::host($site ?: getcwd()).'.'.Configuration::read()['tld']
); );
@@ -667,8 +619,6 @@
* Proxy commands through to an isolated site's version of PHP. * Proxy commands through to an isolated site's version of PHP.
*/ */
$app->command('php [command]', function (OutputInterface $output, $command) { $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.'); 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", [ })->descriptions("Proxy PHP commands with isolated site's PHP executable", [
'command' => "Command to run 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. * Proxy commands through to an isolated site's version of Composer.
*/ */
$app->command('composer [command]', function (OutputInterface $output, $command) { $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.'); 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", [ })->descriptions("Proxy Composer commands with isolated site's PHP executable", [
'command' => "Composer command to run with isolated site's PHP executable", 'command' => "Composer command to run with isolated site's PHP executable",
@@ -689,8 +637,6 @@
* Tail log file. * Tail log file.
*/ */
$app->command('log [-f|--follow] [-l|--lines=] [key]', function (OutputInterface $output, $follow, $lines, $key = null) { $app->command('log [-f|--follow] [-l|--lines=] [key]', function (OutputInterface $output, $follow, $lines, $key = null) {
writer($output);
$defaultLogs = [ $defaultLogs = [
'php-fpm' => BREW_PREFIX.'/var/log/php-fpm.log', 'php-fpm' => BREW_PREFIX.'/var/log/php-fpm.log',
'nginx' => VALET_HOME_PATH.'/Log/nginx-error.log', 'nginx' => VALET_HOME_PATH.'/Log/nginx-error.log',
@@ -759,8 +705,6 @@
* Configure or display the directory-listing setting. * Configure or display the directory-listing setting.
*/ */
$app->command('directory-listing [status]', function (OutputInterface $output, $status = null) { $app->command('directory-listing [status]', function (OutputInterface $output, $status = null) {
writer($output);
$key = 'directory-listing'; $key = 'directory-listing';
$config = Configuration::read(); $config = Configuration::read();
@@ -781,8 +725,6 @@
* Output diagnostics to aid in debugging Valet. * Output diagnostics to aid in debugging Valet.
*/ */
$app->command('diagnose [-p|--print] [--plain]', function (OutputInterface $output, $print, $plain) { $app->command('diagnose [-p|--print] [--plain]', function (OutputInterface $output, $print, $plain) {
writer($output);
info('Running diagnostics... (this may take a while)'); info('Running diagnostics... (this may take a while)');
Diagnose::run($print, $plain); Diagnose::run($print, $plain);

View File

@@ -32,7 +32,8 @@
"symfony/console": "^3.0|^4.0|^5.0|^6.0", "symfony/console": "^3.0|^4.0|^5.0|^6.0",
"symfony/process": "^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", "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": { "require-dev": {
"mockery/mockery": "^1.2.3", "mockery/mockery": "^1.2.3",