From 6bd3cf660afdb020ed10517ea3e1aa2578894944 Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Tue, 20 Dec 2022 20:04:28 -0500 Subject: [PATCH 1/7] Clean up helpers --- cli/includes/helpers.php | 158 ++++++++++++++++++--------------------- 1 file changed, 74 insertions(+), 84 deletions(-) diff --git a/cli/includes/helpers.php b/cli/includes/helpers.php index f04db5d..a4f6df5 100644 --- a/cli/includes/helpers.php +++ b/cli/includes/helpers.php @@ -33,7 +33,7 @@ * Set or get a global console writer. * * @param null|Symfony\Component\Console\Output\ConsoleOutputInterface $writer - * @return void|Symfony\Component\Console\Output\ConsoleOutputInterface + * @return Symfony\Component\Console\Output\ConsoleOutputInterface */ function writer($writer = null) { @@ -47,7 +47,7 @@ function writer($writer = null) return $container->make('writer'); } - Container::getInstance()->instance('writer', $writer); + return Container::getInstance()->instance('writer', $writer); } /** @@ -109,17 +109,15 @@ function output($output) writer()->writeln($output); } -if (! function_exists('resolve')) { - /** - * Resolve the given class from the container. - * - * @param string $class - * @return mixed - */ - function resolve($class) - { - return Container::getInstance()->make($class); - } +/** + * Resolve the given class from the container. + * + * @param string $class + * @return mixed + */ +function resolve($class) +{ + return Container::getInstance()->make($class); } /** @@ -134,33 +132,31 @@ function swap($class, $instance) Container::getInstance()->instance($class, $instance); } -if (! function_exists('retry')) { - /** - * Retry the given function N times. - * - * @param int $retries - * @param callable $retries - * @param int $sleep - * @return mixed - */ - function retry($retries, $fn, $sleep = 0) - { - beginning: - try { - return $fn(); - } catch (Exception $e) { - if (! $retries) { - throw $e; - } - - $retries--; - - if ($sleep > 0) { - usleep($sleep * 1000); - } - - goto beginning; +/** + * Retry the given function N times. + * + * @param int $retries + * @param callable $retries + * @param int $sleep + * @return mixed + */ +function retry($retries, $fn, $sleep = 0) +{ + beginning: + try { + return $fn(); + } catch (Exception $e) { + if (! $retries) { + throw $e; } + + $retries--; + + if ($sleep > 0) { + usleep($sleep * 1000); + } + + goto beginning; } } @@ -176,60 +172,54 @@ function should_be_sudo() } } -if (! function_exists('tap')) { - /** - * Tap the given value. - * - * @param mixed $value - * @param callable $callback - * @return mixed - */ - function tap($value, callable $callback) - { - $callback($value); +/** + * Tap the given value. + * + * @param mixed $value + * @param callable $callback + * @return mixed + */ +function tap($value, callable $callback) +{ + $callback($value); - return $value; - } + return $value; } -if (! function_exists('ends_with')) { - /** - * Determine if a given string ends with a given substring. - * - * @param string $haystack - * @param string|array $needles - * @return bool - */ - function ends_with($haystack, $needles) - { - foreach ((array) $needles as $needle) { - if (substr($haystack, -strlen($needle)) === (string) $needle) { - return true; - } +/** + * Determine if a given string ends with a given substring. + * + * @param string $haystack + * @param string|array $needles + * @return bool + */ +function ends_with($haystack, $needles) +{ + foreach ((array) $needles as $needle) { + if (substr($haystack, -strlen($needle)) === (string) $needle) { + return true; } - - return false; } + + return false; } -if (! function_exists('starts_with')) { - /** - * Determine if a given string starts with a given substring. - * - * @param string $haystack - * @param string|string[] $needles - * @return bool - */ - function starts_with($haystack, $needles) - { - foreach ((array) $needles as $needle) { - if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) { - return true; - } +/** + * Determine if a given string starts with a given substring. + * + * @param string $haystack + * @param string|string[] $needles + * @return bool + */ +function starts_with($haystack, $needles) +{ + foreach ((array) $needles as $needle) { + if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) { + return true; } - - return false; } + + return false; } /** From 72212fb7b6a308cefa086c87b5da047d97523a1b Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Wed, 21 Dec 2022 18:43:37 -0500 Subject: [PATCH 2/7] Require passing a version number to isolate command --- cli/app.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cli/app.php b/cli/app.php index fcf8d15..a728540 100644 --- a/cli/app.php +++ b/cli/app.php @@ -563,8 +563,15 @@ function (ConsoleCommandEvent $event) { $site = basename(getcwd()); } - if (is_null($phpVersion) && $phpVersion = Site::phpRcVersion($site)) { + if (is_null($phpVersion)) { info("Found '{$site}/.valetphprc' specifying version: {$phpVersion}"); + if ($phpVersion = Site::phpRcVersion($site)) { + info("Found '{$site}/.valetphprc' specifying version: {$phpVersion}"); + } else { + info("\nPlease provide a version number. E.g.:"); + info("valet isolate php@8.2"); + exit; + } } PhpFpm::isolateDirectory($site, $phpVersion); From 6fbe5893040a337f7a5c1154b9f4d3642159810c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 21 Dec 2022 23:43:48 +0000 Subject: [PATCH 3/7] Apply fixes from StyleCI --- cli/app.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/app.php b/cli/app.php index a728540..285c7dd 100644 --- a/cli/app.php +++ b/cli/app.php @@ -569,7 +569,7 @@ function (ConsoleCommandEvent $event) { info("Found '{$site}/.valetphprc' specifying version: {$phpVersion}"); } else { info("\nPlease provide a version number. E.g.:"); - info("valet isolate php@8.2"); + info('valet isolate php@8.2'); exit; } } From 0f91d88bc4b2134d029889e95d56acc9a2c3ea3d Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Thu, 22 Dec 2022 00:07:21 -0500 Subject: [PATCH 4/7] Clean up NullWriter references --- cli/includes/helpers.php | 8 +++++--- tests/UsesNullWriter.php | 16 +++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cli/includes/helpers.php b/cli/includes/helpers.php index a4f6df5..4f44121 100644 --- a/cli/includes/helpers.php +++ b/cli/includes/helpers.php @@ -32,8 +32,8 @@ /** * Set or get a global console writer. * - * @param null|Symfony\Component\Console\Output\ConsoleOutputInterface $writer - * @return Symfony\Component\Console\Output\ConsoleOutputInterface + * @param null|OutputInterface $writer + * @return OutputInterface|\NullWriter|null */ function writer($writer = null) { @@ -47,9 +47,11 @@ function writer($writer = null) return $container->make('writer'); } - return Container::getInstance()->instance('writer', $writer); + $container->instance('writer', $writer); + return null; } + /** * Output the given text to the console. * diff --git a/tests/UsesNullWriter.php b/tests/UsesNullWriter.php index 0b1a71b..d0316ae 100644 --- a/tests/UsesNullWriter.php +++ b/tests/UsesNullWriter.php @@ -6,12 +6,14 @@ trait UsesNullWriter { public function setNullWriter() { - Container::getInstance()->instance('writer', new class - { - public function writeLn($msg) - { - // do nothing - } - }); + Container::getInstance()->instance('writer', new NullWriter); + } +} + +class NullWriter +{ + public function writeLn($msg) + { + // do nothing } } From 9ed585c92d8463f33c03b6a61bdd75c0e47f4fc4 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 22 Dec 2022 05:07:44 +0000 Subject: [PATCH 5/7] Apply fixes from StyleCI --- cli/includes/helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/includes/helpers.php b/cli/includes/helpers.php index 4f44121..8de7daf 100644 --- a/cli/includes/helpers.php +++ b/cli/includes/helpers.php @@ -48,10 +48,10 @@ function writer($writer = null) } $container->instance('writer', $writer); + return null; } - /** * Output the given text to the console. * From b200022041b79d361738a18771ff8336d7d8b0c6 Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Thu, 22 Dec 2022 10:30:50 -0500 Subject: [PATCH 6/7] Add set-ngrok-token command --- cli/Valet/Ngrok.php | 17 +++++++++++++++++ cli/app.php | 7 +++++++ tests/NgrokTest.php | 5 +++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cli/Valet/Ngrok.php b/cli/Valet/Ngrok.php index 42238a7..6a89f89 100644 --- a/cli/Valet/Ngrok.php +++ b/cli/Valet/Ngrok.php @@ -8,11 +8,17 @@ class Ngrok { + public $cli; public $tunnelsEndpoints = [ 'http://127.0.0.1:4040/api/tunnels', 'http://127.0.0.1:4041/api/tunnels', ]; + public function __construct(CommandLine $cli) + { + $this->cli = $cli; + } + /** * Get the current tunnel URL from the Ngrok API. * @@ -65,4 +71,15 @@ public function findHttpTunnelUrl($tunnels, $domain) } } } + + /** + * Set the Ngrok auth token. + * + * @param string $token + * @return string + */ + public function setToken($token) + { + return $this->cli->runAsUser('./bin/ngrok authtoken '.$token); + } } diff --git a/cli/app.php b/cli/app.php index a728540..c380c03 100644 --- a/cli/app.php +++ b/cli/app.php @@ -321,6 +321,13 @@ function (ConsoleCommandEvent $event) { output(Ngrok::currentTunnelUrl(Site::domain($domain))); })->descriptions('Get the URL to the current Ngrok tunnel'); + /** + * Set the ngrok auth token. + */ + $app->command('set-ngrok-token [token]', function (OutputInterface $output, $token = null) { + output(Ngrok::setToken($token)); + })->descriptions('Set the Ngrok auth token'); + /** * Start the daemon services. */ diff --git a/tests/NgrokTest.php b/tests/NgrokTest.php index 97319aa..e7f8374 100644 --- a/tests/NgrokTest.php +++ b/tests/NgrokTest.php @@ -1,6 +1,7 @@ assertEquals('http://right-one.ngrok.io/', $ngrok->findHttpTunnelUrl($tunnels, 'mysite')); } @@ -63,7 +64,7 @@ public function test_it_checks_against_lowercased_domain() ], ]; - $ngrok = new Ngrok; + $ngrok = new Ngrok(new CommandLine); $this->assertEquals('http://right-one.ngrok.io/', $ngrok->findHttpTunnelUrl($tunnels, 'MySite')); } } From 6824065a228afe5f4afed12da448c1be4c6d605a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 22 Dec 2022 15:31:00 +0000 Subject: [PATCH 7/7] Apply fixes from StyleCI --- cli/Valet/Ngrok.php | 2 +- cli/app.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/Valet/Ngrok.php b/cli/Valet/Ngrok.php index 6a89f89..f144b95 100644 --- a/cli/Valet/Ngrok.php +++ b/cli/Valet/Ngrok.php @@ -75,7 +75,7 @@ public function findHttpTunnelUrl($tunnels, $domain) /** * Set the Ngrok auth token. * - * @param string $token + * @param string $token * @return string */ public function setToken($token) diff --git a/cli/app.php b/cli/app.php index c380c03..4f8dd8f 100644 --- a/cli/app.php +++ b/cli/app.php @@ -576,7 +576,7 @@ function (ConsoleCommandEvent $event) { info("Found '{$site}/.valetphprc' specifying version: {$phpVersion}"); } else { info("\nPlease provide a version number. E.g.:"); - info("valet isolate php@8.2"); + info('valet isolate php@8.2'); exit; } }