helpers
Table of Contents
Functions
- assertDomain() : bool
- Validates a domain name according to RFC rules and common DNS restrictions.
- clearConsole() : false|string
- Clears the terminal console screen, depending on the operating system.
- domainExists() : bool
- Checks whether the given domain has valid DNS records (A, AAAA, or CNAME).
- makeCommand() : string
- Generates a complete shell command string, optionally including a pipeline.
- silent() : string|null
- Appends a redirection string to suppress output from a shell command.
Functions
assertDomain()
Validates a domain name according to RFC rules and common DNS restrictions.
assertDomain(string $domain[, bool $throw = true ][, bool $requireTld = true ]) : bool
Parameters
- $domain : string
-
The domain name to validate (e.g. "example.com").
- $throw : bool = true
-
Whether to throw an exception on invalid domain.
- $requireTld : bool = true
-
Whether to require a TLD (default true).
Tags
Return values
bool —True if valid (or silent if $throw = true and valid).
clearConsole()
Clears the terminal console screen, depending on the operating system.
clearConsole([bool $clearable = true ]) : false|string
On Windows systems, it runs cls
; on Unix-based systems (Linux/macOS), it runs clear
.
If $clearable
is set to false
, the function does nothing and returns false
.
Note: The command relies on the system's availability of
cls
orclear
. Use with caution in restricted or sandboxed environments.
Parameters
- $clearable : bool = true
-
Whether or not to perform the console clear.
Tags
Return values
false|string —Returns the last line from the shell output, or false on failure or if $clearable
is false.
domainExists()
Checks whether the given domain has valid DNS records (A, AAAA, or CNAME).
domainExists(string $domain) : bool
Parameters
- $domain : string
-
The domain to check (e.g. "example.com").
Tags
Return values
bool —True if DNS records exist for the domain, false otherwise.
makeCommand()
Generates a complete shell command string, optionally including a pipeline.
makeCommand(array<string|int, string>|string|null $command[, array<string|int, string>|string|null $args = null ][, CommandOptions|null $options = null ][, string|null $previous = null ][, string|null $post = null ]) : string
This function builds a shell command from a central main command, optional
arguments/options, and optional commands to prepend ($previous
) or append ($post
)
in a pipeline (|
).
The main command can be provided as a string or an array (which will be joined by spaces). Arguments and options can also be strings or arrays of strings.
The $options
parameter allows adding global prefixes such as sudo
or running
the command as another user.
If the main command is null, only the pipeline between $previous
and $post
is generated, and arguments or options are ignored.
Parameters
- $command : array<string|int, string>|string|null
-
The main command to run, either as a string (e.g. 'ls') or an array of parts (e.g. ['wp', 'post', 'list']).
- $args : array<string|int, string>|string|null = null
-
Arguments and options for the main command (e.g. '-la' or ['--format=ids']).
- $options : CommandOptions|null = null
-
Global command options such as sudo or user context.
- $previous : string|null = null
-
Command to prepend before the main command, for pipelines (e.g. 'cat file.txt').
- $post : string|null = null
-
Command to append after the main command, for pipelines (e.g. 'grep "error"').
Tags
Return values
string —The final full command string to execute, including any pipelines.
silent()
Appends a redirection string to suppress output from a shell command.
silent(string|null &$command[, bool $silent = false ]) : string|null
This helper function modifies the given command string in-place by appending a platform-appropriate silence directive:
- On Unix/Linux/macOS:
> /dev/null 2>&1
- On Windows:
> NUL 2>&1
This is typically used to hide output when executing shell commands.
Parameters
- $command : string|null
-
The command string to modify by reference.
- $silent : bool = false
-
Whether to append the silence directive to the command.
Tags
Return values
string|null —The updated command string (or null if input was null).