39
loading...
This website collects cookies to deliver better user experience
ping-cli
and require the minicli/minicli
package.composer require minicli/minicli
.gitignore
to make sure the /vendor
folder doesn't get added to source control.vendor/
composer.lock
bin
folder and a file named ping-cli
.mkdir bin && touch bin/ping-cli
bash
and the minicli
package to create our new tool. Start by using a shebang to load our new ping-cli
file in a php
environment, and add out opening php
tags.#!/usr/bin/env php
<?php declare(strict_types = 1);
declare
if you don't want strict typing.minicli
, we must first make sure we're calling this file from the CLI. We do this with the php_sapi_name
functions, which returns the string of our current interface.if (php_sapi_name() !== 'cli') {
exit;
}
minicli
we include the composer
autoloader and instantiate a new Minicli
class.$root
to hold the parent directory of the directory bin/ping-cli
. Adding a check to see if we can grab the autoloader using that path. Should that operation be false, we will set the $root
to be 4
levels up from our parent directory.vendor/bin/ping-cli
, we use the autoloader generated by the outside project correctly.Minicli\App
.$root = dirname(__DIR__);
if (! is_file($root . '/vendor/autoload.php')) {
$root = dirname(__DIR__, 4);
}
require $root . '/vendor/autoload.php';
use Minicli\App;
$app = new App();
runCommand
method on our $app
. Add the following line to the end of our ping-cli
file.$app->runCommand($argv);
$argv
variable is a reserved variable provided to us by PHP, you can read more about it in the documentation../minicli help
to the console, try it yourself by running the command../bin/ping-cli
./minicli help
signature ran when running our tool with no input.setSignature
method on the minicli
$app
.$app->setSignature(<<<EOD
_ ___
____ (_)___ ____ _ _____/ (_)
/ __ \/ / __ \/ __ `/_____/ ___/ / /
/ /_/ / / / / / /_/ /_____/ /__/ / /
/ .___/_/_/ /_/\__, / \___/_/_/
/_/ /____/ \e[0;32m Ver. 0.0.1
EOD);
ping
command. Since this tool will only have a single command, we will forgo registering a namespace and just register a single command with the registerCommand
method.name
and the callable
which will get passed the $input
. For convenience we will also use the $app
in out callable
.use Minicli\Command\CommandCall;
$app->registerCommand('ping', function (CommandCall $input) use ($app) {
$app->getPrinter()->success('We have lift-off! 🚀');
});
getPrinter
method access the output handler, which we then use to print a success
message to the console../bin/ping-cli ping
will now print back We have lift-off! 🚀 in our console.ping
command.url
parameter, in either of two formats, HTTP or HTTPS. With this parameter, we will send a request to the url
and see if it's accessible.getParam
on our $input
and specify which parameter we want. Let's store our url
parameter in a $url
variable, or default to null
.error
output using getPrinter
and exit early if no url
was provided.$url = $input->getParam('url') ?? null;
if (! $url) {
$app->getPrinter()->error('Parameter <url> was not provided');
return 1;
}
$url
parameter. To do that we will use RegEx and filter_var
.$url
provided starts with a valid HTTP(s) protocol. Returning early with a message if that fails.if (! filter_var($url, FILTER_VALIDATE_URL)) {
$app->getPrinter()->error('Parameter <url> not a valid URL.');
return 1;
}
if (! preg_match('#^https?://#i', $url)) {
$app->getPrinter()->error('Parameter <url> has to include a valid HTTP protocol.');
return 1;
}
$url
, it'll do for a simple ping command.$url
provided, we need to create a stream context for file_get_contents
to use. In this context we will set the HTTP method and some HTTP headers.getStreamContext
where we'll create our new context.function getStreamContext() {
$headers = implode("\r\n", [
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_16_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9',
'Accept-Language: en-US,en;q=0.9',
'Upgrade-Insecure-Requests: 1',
'X-Application: PingCLI/0.0.1',
'Sec-GPC: 1',
]);
return stream_context_create([
'http' => [
'method' => 'GET',
'header' => $headers,
]
]);
}
$url
we're accessing thinks the site is being used from a Chrome browser running on a Mac.file_get_contents
to ping the $url
. Since we won't use the returning content for anything, we can use file_get_contents
directly in our if statement.if (! @file_get_contents($url, context: getStreamContext())) {
$app->getPrinter()->error('Parameter <url> could not be reached.');
return 1;
}
file_get_contents
might not be able to access, that will give of warnings unless silenced.callable
.$app->getPrinter()->success(sprintf('URL <%s> is up.', $url));
./bin/ping-cli ping url=https://devdojo.com
microtime
function and some subtraction. We'll Add the following right above our file_get_contents
check.$start_time = microtime(as_float: true);
$end_time = microtime(as_float: true);
file_get_contents
we should get a fairly close estimate.$app->getPrinter()->display(sprintf('Est. ping time: %.02f sec', ($end_time - $start_time)));
minicli
on their GitHub page, or their documentation, also follow the creator Erika Heidi on twitter.ping-cli
repository.chmod
, use the equivalent command for windows.ping-cli ~ chmod +x bin/ping-cli