CLI
in package
Class CLI: Provides a command-line interface for generating and checking API keys.
This class offers static methods to handle command-line arguments,
display help information, and execute specific actions such as
generating new API keys, checking the validity of existing keys,
and running internal tests. It relies on the ApiKeyFS
class for
the underlying API key storage and validation logic.
Tags
Table of Contents
Properties
- $options : array<string|int, mixed>
Methods
- display_help() : void
- Displays the help message for the CLI tool.
- handle_check() : void
- Handles the `check` command to verify the validity of an API key.
- handle_generate() : void
- Handles the `generate` command to create and store a new API key.
- handle_test() : void
- Handles the `test` command to run various tests.
- parse() : void
- Parses the command-line arguments and stores them in the `$options` array.
- run() : void
- Runs the CLI application.
- test() : void
- Runs tests specifically for the `CLI` class.
Properties
$options
public
static array<string|int, mixed>
$options
= []
An associative array to store parsed command-line options.
The keys of the array are the option names (without the '--' prefix),
and the values are the corresponding option values. Boolean flags
will have a value of true
.
Tags
Methods
display_help()
Displays the help message for the CLI tool.
public
static display_help() : void
This function outputs the usage instructions, version information, available commands, and supported options to the console. It also provides examples of how to use the tool.
Tags
handle_check()
Handles the `check` command to verify the validity of an API key.
public
static handle_check() : void
This function retrieves the required options (--app-key
, --path
, --token
)
from the $options
array. It then defines the API_KEY_PATH
and APP_KEY
constants and calls the ApiKeyFS::check()
method to validate the provided
API key token. Optional parameters like --key-length
and --algo
are also
handled. The function then prints whether the provided API key token is valid
or invalid. In case of any error during the validation process, an error
message is displayed, and the script exits with an error code.
Tags
handle_generate()
Handles the `generate` command to create and store a new API key.
public
static handle_generate() : void
This function retrieves the required options (--app-key
, --path
, --label
)
from the $options
array. It then defines the API_KEY_PATH
and APP_KEY
constants and calls the ApiKeyFS::make()
method to generate and store
the new API key. Optional parameters like --ip
, --ttl
, --key-length
, and
--algo
are also handled. If the --verbose
option is enabled, additional
information about the generated key and its storage location is printed.
In case of any error during the key generation process, an error message
is displayed, and the script exits with an error code.
Tags
handle_test()
Handles the `test` command to run various tests.
public
static handle_test() : void
This function calls the test()
methods of the Key
, ApiKeyMemory
,
ApiKeyFS
, and CLI
classes. The verbosity of the test output can be
controlled using the --verbose
option. After all tests are executed,
it prints "ok" to indicate successful completion.
Tags
parse()
Parses the command-line arguments and stores them in the `$options` array.
public
static parse() : void
This function iterates through the command-line arguments (excluding the
script name and the command) and extracts options in the format --key=value
or boolean flags like --verbose
. The parsed options are stored as key-value
pairs in the static $options
array. If the --verbose
option is not
present, it defaults to false
.
Tags
run()
Runs the CLI application.
public
static run() : void
This is the main entry point for the CLI tool. It retrieves the command
from the command-line arguments, parses the options, and then calls the
appropriate handler function based on the provided command. If no command
is provided or if the command is help
, it calls the display_help()
function. Finally, it exits with a success code (0).
Tags
test()
Runs tests specifically for the `CLI` class.
public
static test([bool $debug = false ]) : void
This function performs various tests to ensure the correct behavior of the
CLI
class, including handling of missing required options for the generate
and check
commands, as well as successful execution of these commands.
It uses the exec()
function to simulate command-line calls and assert()
to verify the expected output and return codes. The $debug
parameter can
be used to enable verbose output of the test execution.
Parameters
- $debug : bool = false
-
Optional. If
true
, prints additional debugging information during the test execution. Defaults tofalse
.