CLIRequest
extends Request
in package
Represents a request from the command-line. Provides additional tools to interact with that request since CLI requests are not static like HTTP requests might be.
Portions of this code were initially from the FuelPHP Framework, version 1.7.x, and used here under the MIT license they were originally made available under.
http://fuelphp.com
Tags
Table of Contents
Properties
- $args : array<string|int, mixed>
- Command line arguments (segments and options).
- $body : string|null
- Message body
- $config : App
- Configuration settings.
- $globals : array{get?: array, post?: array, request?: array, cookie?: array, server?: array}
- Stores values we've retrieved from PHP globals.
- $headerMap : array<string|int, mixed>
- Holds a map of lower-case header names and their normal-case key as it is in $headers.
- $headers : array<string, Header|array<int, Header>>
- List of all HTTP request headers.
- $ipAddress : string
- IP address of the current user.
- $method : string
- Set the expected HTTP verb
- $options : array<string|int, mixed>
- Command line options and their values.
- $protocolVersion : string
- Protocol version
- $segments : array<string|int, mixed>
- Stores the segments of our cli "URI" command.
- $uri : URI|null
- A URI instance.
- $validProtocolVersions : array<string|int, mixed>
- List of valid protocol versions
Methods
- __construct() : mixed
- Constructor
- addHeader() : $this
- Adds a header (not a header value) with the same name.
- appendBody() : $this
- Appends data to the body of the current message.
- appendHeader() : $this
- Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
- fetchGlobal() : array<string|int, mixed>|bool|float|int|object|string|null
- Fetches one or more items from a global, like cookies, get, post, etc.
- getArgs() : array<string|int, mixed>
- Returns an array of all CLI arguments (segments and options).
- getBody() : string|null
- Returns the Message's body.
- getCookie() : array<string|int, mixed>|null
- This is a place holder for calls from cookie_helper get_cookie().
- getEnv() : mixed
- Fetch an item from the $_ENV array.
- getGet() : array<string|int, mixed>|null
- Fetch an item from GET data.
- getGetPost() : array<string|int, mixed>|null
- Fetch an item from GET data with fallback to POST.
- getHeader() : array<string|int, mixed>|Header|null
- Returns a single header object. If multiple headers with the same name exist, then will return an array of header objects.
- getHeaderLine() : string
- Retrieves a comma-separated string of the values for a single header.
- getHeaders() : array<string, Header>
- Returns an array containing all headers.
- getIPAddress() : string
- Gets the user's IP address.
- getLocale() : string
- Gets the current locale, with a fallback to the default locale if none is set.
- getMethod() : string
- Retrieves the HTTP method of the request.
- getOption() : string|null
- Returns the value for a single CLI option that was passed in.
- getOptions() : array<string|int, mixed>
- Returns an associative array of all CLI options found, with their values.
- getOptionString() : string
- Returns the options as a string, suitable for passing along on the CLI to other commands.
- getPath() : string
- Returns the "path" of the request script so that it can be used in routing to the appropriate controller/method.
- getPost() : array<string|int, mixed>|null
- Fetch an item from POST.
- getPostGet() : array<string|int, mixed>|null
- Fetch an item from POST data with fallback to GET.
- getProtocolVersion() : string
- Returns the HTTP Protocol Version.
- getSegments() : array<string|int, mixed>
- Returns the path segments.
- getServer() : mixed
- Fetch an item from the $_SERVER array.
- getUri() : URI
- Retrieves the URI instance.
- hasHeader() : bool
- Determines whether a header exists.
- header() : Header|array<int, Header>|null
- Returns a single Header object. If multiple headers with the same name exist, then will return an array of header objects.
- headers() : array<string, Header|array<int, Header>>
- Returns an array containing all Headers.
- is() : bool
- Checks this request type.
- isCLI() : bool
- Determines if this request was made from the command line (CLI).
- populateHeaders() : void
- Populates the $headers array with any headers the server knows about.
- prependHeader() : $this
- Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
- removeHeader() : $this
- Removes a header from the list of headers we track.
- setBody() : $this
- Sets the body of the current message.
- setGlobal() : $this
- Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
- setHeader() : $this
- Sets a header and it's value.
- setMethod() : $this
- Sets the request method. Used when spoofing the request.
- setProtocolVersion() : $this
- Sets the HTTP protocol version.
- withMethod() : static
- Returns an instance with the specified method.
- withUri() : static
- Returns an instance with the provided URI.
- getHeaderName() : string
- Takes a header name in any case, and returns the normal-case version of the header.
- parseCommand() : void
- Parses the command line it was called from and collects all options and valid segments.
- populateGlobals() : void
- Saves a copy of the current state of one of several PHP globals, so we can retrieve them later.
- checkMultipleHeaders() : void
- getClientIP() : string|null
- Gets the client IP address from the HTTP header.
- getHostFromUri() : string
- hasMultipleHeaders() : bool
- isHostHeaderMissingOrEmpty() : bool
- returnNullOrEmptyArray() : array<string|int, mixed>|null
Properties
$args
Command line arguments (segments and options).
protected
array<string|int, mixed>
$args
= []
$body
Message body
protected
string|null
$body
$config
Configuration settings.
protected
App
$config
$globals
Stores values we've retrieved from PHP globals.
protected
array{get?: array, post?: array, request?: array, cookie?: array, server?: array}
$globals
= []
$headerMap
Holds a map of lower-case header names and their normal-case key as it is in $headers.
protected
array<string|int, mixed>
$headerMap
= []
Used for case-insensitive header access.
$headers
List of all HTTP request headers.
protected
array<string, Header|array<int, Header>>
$headers
= []
[name => Header] or [name => [Header1, Header2]]
$ipAddress
IP address of the current user.
Will become private in a future release
protected
string
$ipAddress
= ''
$method
Set the expected HTTP verb
protected
string
$method
= 'CLI'
$options
Command line options and their values.
protected
array<string|int, mixed>
$options
= []
$protocolVersion
Protocol version
protected
string
$protocolVersion
$segments
Stores the segments of our cli "URI" command.
protected
array<string|int, mixed>
$segments
= []
$uri
A URI instance.
protected
URI|null
$uri
$validProtocolVersions
List of valid protocol versions
protected
array<string|int, mixed>
$validProtocolVersions
= ['1.0', '1.1', '2.0', '3.0']
Methods
__construct()
Constructor
public
__construct(App $config) : mixed
Parameters
- $config : App
addHeader()
Adds a header (not a header value) with the same name.
public
addHeader(string $name, string $value) : $this
Use this only when you set multiple headers with the same name,
typically, for Set-Cookie
.
Parameters
- $name : string
- $value : string
Return values
$thisappendBody()
Appends data to the body of the current message.
public
appendBody(string $data) : $this
Parameters
- $data : string
Return values
$thisappendHeader()
Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
public
appendHeader(string $name, string|null $value) : $this
Parameters
- $name : string
- $value : string|null
Return values
$thisfetchGlobal()
Fetches one or more items from a global, like cookies, get, post, etc.
public
fetchGlobal(string $name[, array<string|int, mixed>|int|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : array<string|int, mixed>|bool|float|int|object|string|null
Can optionally filter the input when you retrieve it by passing in a filter.
If $type is an array, it must conform to the input allowed by the filter_input_array method.
http://php.net/manual/en/filter.filters.sanitize.php
Parameters
- $name : string
-
Supergrlobal name (lowercase)
- $index : array<string|int, mixed>|int|string|null = null
- $filter : int|null = null
-
Filter constant
- $flags : array<string|int, mixed>|int|null = null
-
Options
Tags
Return values
array<string|int, mixed>|bool|float|int|object|string|nullgetArgs()
Returns an array of all CLI arguments (segments and options).
public
getArgs() : array<string|int, mixed>
Return values
array<string|int, mixed>getBody()
Returns the Message's body.
public
getBody() : string|null
Return values
string|nullgetCookie()
This is a place holder for calls from cookie_helper get_cookie().
public
getCookie([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, mixed $flags = null ]) : array<string|int, mixed>|null
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to be fetched from $_COOKIE
- $filter : int|null = null
-
A filter name to be applied
- $flags : mixed = null
Return values
array<string|int, mixed>|nullgetEnv()
Fetch an item from the $_ENV array.
public
getEnv([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : mixed
This method does not work from the beginning. Use env()
.
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to be fetched from $_ENV
- $filter : int|null = null
-
A filter name to be applied
- $flags : array<string|int, mixed>|int|null = null
getGet()
Fetch an item from GET data.
public
getGet([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : array<string|int, mixed>|null
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to fetch from $_GET.
- $filter : int|null = null
-
A filter name to apply.
- $flags : array<string|int, mixed>|int|null = null
Return values
array<string|int, mixed>|nullgetGetPost()
Fetch an item from GET data with fallback to POST.
public
getGetPost([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : array<string|int, mixed>|null
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to be fetched from $_GET or $_POST
- $filter : int|null = null
-
A filter name to apply
- $flags : array<string|int, mixed>|int|null = null
Return values
array<string|int, mixed>|nullgetHeader()
Returns a single header object. If multiple headers with the same name exist, then will return an array of header objects.
public
getHeader(string $name) : array<string|int, mixed>|Header|null
Use Message::header() to make room for PSR-7
Parameters
- $name : string
Tags
Return values
array<string|int, mixed>|Header|nullgetHeaderLine()
Retrieves a comma-separated string of the values for a single header.
public
getHeaderLine(string $name) : string
This method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma.
NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating.
Parameters
- $name : string
Return values
stringgetHeaders()
Returns an array containing all headers.
public
getHeaders() : array<string, Header>
Use Message::headers() to make room for PSR-7
Tags
Return values
array<string, Header> —An array of the request headers
getIPAddress()
Gets the user's IP address.
public
getIPAddress() : string
Return values
string —IP address if it can be detected. If the IP address is not a valid IP address, then will return '0.0.0.0'.
getLocale()
Gets the current locale, with a fallback to the default locale if none is set.
public
getLocale() : string
Return values
stringgetMethod()
Retrieves the HTTP method of the request.
public
getMethod() : string
Return values
string —Returns the request method (always uppercase)
getOption()
Returns the value for a single CLI option that was passed in.
public
getOption(string $key) : string|null
Parameters
- $key : string
Return values
string|nullgetOptions()
Returns an associative array of all CLI options found, with their values.
public
getOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>getOptionString()
Returns the options as a string, suitable for passing along on the CLI to other commands.
public
getOptionString([bool $useLongOpts = false ]) : string
Example: $options = [ 'foo' => 'bar', 'baz' => 'queue some stuff' ];
getOptionString() = '-foo bar -baz "queue some stuff"'
Parameters
- $useLongOpts : bool = false
Return values
stringgetPath()
Returns the "path" of the request script so that it can be used in routing to the appropriate controller/method.
public
getPath() : string
The path is determined by treating the command line arguments as if it were a URL - up until we hit our first option.
Example: php index.php users 21 profile -foo bar
// Routes to /users/21/profile (index is removed for routing sake)
// with the option foo = bar.
Return values
stringgetPost()
Fetch an item from POST.
public
getPost([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : array<string|int, mixed>|null
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to fetch from $_POST.
- $filter : int|null = null
-
A filter name to apply
- $flags : array<string|int, mixed>|int|null = null
Return values
array<string|int, mixed>|nullgetPostGet()
Fetch an item from POST data with fallback to GET.
public
getPostGet([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : array<string|int, mixed>|null
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to fetch from $_POST or $_GET
- $filter : int|null = null
-
A filter name to apply
- $flags : array<string|int, mixed>|int|null = null
Return values
array<string|int, mixed>|nullgetProtocolVersion()
Returns the HTTP Protocol Version.
public
getProtocolVersion() : string
Return values
string —HTTP protocol version.
getSegments()
Returns the path segments.
public
getSegments() : array<string|int, mixed>
Return values
array<string|int, mixed>getServer()
Fetch an item from the $_SERVER array.
public
getServer([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : mixed
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to be fetched from $_SERVER
- $filter : int|null = null
-
A filter name to be applied
- $flags : array<string|int, mixed>|int|null = null
getUri()
Retrieves the URI instance.
public
getUri() : URI
Return values
URIhasHeader()
Determines whether a header exists.
public
hasHeader(string $name) : bool
Parameters
- $name : string
-
Case-insensitive header field name.
Return values
bool —Returns true if any header names match the given header name using a case-insensitive string comparison. Returns false if no matching header name is found in the message.
header()
Returns a single Header object. If multiple headers with the same name exist, then will return an array of header objects.
public
header(string $name) : Header|array<int, Header>|null
Parameters
- $name : string
Return values
Header|array<int, Header>|nullheaders()
Returns an array containing all Headers.
public
headers() : array<string, Header|array<int, Header>>
Return values
array<string, Header|array<int, Header>> —An array of the Header objects
is()
Checks this request type.
public
is(string $type) : bool
Parameters
- $type : string
-
HTTP verb or 'json' or 'ajax'
Tags
Return values
boolisCLI()
Determines if this request was made from the command line (CLI).
public
isCLI() : bool
Return values
boolpopulateHeaders()
Populates the $headers array with any headers the server knows about.
public
populateHeaders() : void
prependHeader()
Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
public
prependHeader(string $name, string $value) : $this
Parameters
- $name : string
- $value : string
Return values
$thisremoveHeader()
Removes a header from the list of headers we track.
public
removeHeader(string $name) : $this
Parameters
- $name : string
Return values
$thissetBody()
Sets the body of the current message.
public
setBody(string $data) : $this
Parameters
- $data : string
Return values
$thissetGlobal()
Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
public
setGlobal(string $name, mixed $value) : $this
Parameters
- $name : string
-
Supergrlobal name (lowercase)
- $value : mixed
Tags
Return values
$thissetHeader()
Sets a header and it's value.
public
setHeader(string $name, array<string|int, mixed>|string|null $value) : $this
Parameters
- $name : string
- $value : array<string|int, mixed>|string|null
Return values
$thissetMethod()
Sets the request method. Used when spoofing the request.
public
setMethod(string $method) : $this
Use withMethod() instead for immutability
Parameters
- $method : string
Tags
Return values
$thissetProtocolVersion()
Sets the HTTP protocol version.
public
setProtocolVersion(string $version) : $this
Parameters
- $version : string
Tags
Return values
$thiswithMethod()
Returns an instance with the specified method.
public
withMethod(string $method) : static
Parameters
- $method : string
Return values
staticwithUri()
Returns an instance with the provided URI.
public
withUri(URI $uri[, bool $preserveHost = false ]) : static
Parameters
- $uri : URI
-
New request URI to use.
- $preserveHost : bool = false
-
Preserve the original state of the Host header.
Return values
staticgetHeaderName()
Takes a header name in any case, and returns the normal-case version of the header.
protected
getHeaderName(string $name) : string
Parameters
- $name : string
Return values
stringparseCommand()
Parses the command line it was called from and collects all options and valid segments.
protected
parseCommand() : void
NOTE: I tried to use getopt but had it fail occasionally to find any options, where argv has always had our back.
populateGlobals()
Saves a copy of the current state of one of several PHP globals, so we can retrieve them later.
protected
populateGlobals(string $name) : void
Parameters
- $name : string
-
Superglobal name (lowercase)
Tags
checkMultipleHeaders()
private
checkMultipleHeaders(string $name) : void
Parameters
- $name : string
getClientIP()
Gets the client IP address from the HTTP header.
private
getClientIP(string $header) : string|null
Parameters
- $header : string
Return values
string|nullgetHostFromUri()
private
getHostFromUri(URI $uri) : string
Parameters
- $uri : URI
Return values
stringhasMultipleHeaders()
private
hasMultipleHeaders(string $name) : bool
Parameters
- $name : string
Return values
boolisHostHeaderMissingOrEmpty()
private
isHostHeaderMissingOrEmpty() : bool
Return values
boolreturnNullOrEmptyArray()
private
returnNullOrEmptyArray(array<string|int, mixed>|string|null $index) : array<string|int, mixed>|null
Parameters
- $index : array<string|int, mixed>|string|null