Documentation

Sprout
in package

Useful functions for sprout in general

Table of Contents

Properties

$SEND_BUFFER_SIZE  : int
When reading out of a response object, process this many bytes at a time.

Methods

absRoot()  : mixed
Returns an absolute URL for the web root of this server
adminEmails()  : mixed
Return a list of admins to send emails to.
browserDragdropUploads()  : mixed
Return true if the browser supports drag-and-drop uploads.
checkInsertRate()  : bool
Attempts to put the handbrake on a script which is doing malicious inserts to the database
checkRedirect()  : mixed
Checks a URL that is to be used for redirection is valid.
colorisedDiff()  : mixed
Takes two strings of text (which will be stripped of HTML tags) and returns HTML which is a table showing the differences in a nice colourful way
condenseArray()  : mixed
Condenses an array into a string
determineFilePath()  : string|false
Determines the file path for a class, usually for autoloading
etag()  : mixed
Set the etag header, and some expiry headers.
extraPage()  : mixed
Load the text for an extra page.
formatMysqlDatetime()  : mixed
Takes a mysql DATETIME value (will probably also work with a TIME or DATE value) and formats it according to the format codes specified by the PHP date() function.
getEncrypt()  : EncryptInterface
Get an encryption tool instance. Falls back to looking for configs in core encryption config file
getInstalledVersion()  : string
Determine the version for a composer package.
getMemoryLimit()  : int
Returns the memory limit in bytes.
getSiteVersion()  : string
Determine the version for the current site, i.e. the 'root' package.
getVersion()  : string
Returns the current version of sprout
instance()  : object
Construct a new instance of a class with a given name
ipaddressInArray()  : bool
Check an IP against a list of IP addresses, with logic for CIDR ranges
iterableFirst()  : array<string|int, mixed>|null
Gets the first key value pair of an iterable
iterableFirstKey()  : mixed|null
Gets the first key of an iterable
iterableFirstValue()  : mixed|null
Gets the first value of an iterable
lastModified()  : string|null
Return the last-modified date of all pages on the (sub-)site Returns NULL on error.
linkChecker()  : mixed
Back-end for link-checking tool
moduleInstalled()  : bool
Is this module installed?
nsClass()  : string
Gets the full class name (including namespace) for a specified class, with a list of namespaces to search
nsFunc()  : string
Gets the full name (including namespaced class) for a specified function
nsNew()  : object
Creates an object of a class specified by a string name, with a list of possible namespaces to lookup if the specified class name doesn't contain a namespace
parseByteSize()  : int
Normalise a shorthand byte string to an integer.
passwordComplexity()  : mixed
randStr()  : mixed
Returns a string of random numbers and letters
readableVar()  : string
Converts a variable into something human readable
removeNs()  : string
Removes the namespaces from a class-like entity, e.g. Sprout\Helpers\Text => Text
send()  : void
Render out a response object.
simpleBacktrace()  : array<string|int, mixed>
Gets a simplified backtrace with fewer elements and no recursion
specialFileLinks()  : string
Adds classes, analytics and target-blank to file links.
timeAgo()  : mixed
Returns a time in 'x minutes ago' format.
translateLink()  : mixed
Translate an array which may contain a page_id, filename or url into the final URL to use

Properties

$SEND_BUFFER_SIZE

When reading out of a response object, process this many bytes at a time.

public static int $SEND_BUFFER_SIZE = 1024 * 1024

1 MiB

Methods

absRoot()

Returns an absolute URL for the web root of this server

public static absRoot([string $protocol = '' ]) : mixed

Example: 'http://thejosh.info/sprout_test/'

Parameters
$protocol : string = ''

Protocol to use. 'http' or 'https'. Defaults to server config option, which if not set, uses current request protocol.

adminEmails()

Return a list of admins to send emails to.

public static adminEmails() : mixed

The return value is an array of arrays. The inner arrays contains the keys "name" and "email".

browserDragdropUploads()

Return true if the browser supports drag-and-drop uploads.

public static browserDragdropUploads() : mixed

checkInsertRate()

Attempts to put the handbrake on a script which is doing malicious inserts to the database

public static checkInsertRate(string $table, string $column, string $value, int $limit[, int $time = 3600 ][, array<string|int, mixed> $conds = [] ]) : bool
Parameters
$table : string

The table name, not prefixed

$column : string

The column to check

$value : string

The value to check

$limit : int

The number of inserts allowed in the provided time

$time : int = 3600

The amount of time the limit applies for, in seconds. Default = 1 hour

$conds : array<string|int, mixed> = []

Additional conditions for the WHERE clause, formatted as per Pdb::buildClause

Return values
bool —

True if the insert rate is OK

checkRedirect()

Checks a URL that is to be used for redirection is valid.

public static checkRedirect(mixed $text) : mixed

Will allow remote URLs beginning with 'http://' and local URLs beginning with '/'

Parameters
$text : mixed

colorisedDiff()

Takes two strings of text (which will be stripped of HTML tags) and returns HTML which is a table showing the differences in a nice colourful way

public static colorisedDiff(mixed $orig, mixed $new) : mixed
Parameters
$orig : mixed
$new : mixed

condenseArray()

Condenses an array into a string

public static condenseArray(array<string|int, mixed> $arr) : mixed
Parameters
$arr : array<string|int, mixed>

determineFilePath()

Determines the file path for a class, usually for autoloading

public static determineFilePath(string $class) : string|false
Parameters
$class : string

The class, including namespace

Return values
string|false —

path or false if it couldn't be found

etag()

Set the etag header, and some expiry headers.

public static etag(mixed $etag, mixed $expires) : mixed

Checks if the etag matches - if it does, terminates the script with '304 Not Modified'.

ETag should be specified as a string. Expires should be specified as a number of seconds, after that time the URL will expire.

ETags should be something which is unique for that version of the URL. They should use something which is collission-resistant, such as MD5. They should vary based on the Accept-Encoding header, or any other 'accept' headers, if you are supporting them.

Parameters
$etag : mixed
$expires : mixed

extraPage()

Load the text for an extra page.

public static extraPage(int $type) : mixed

Returns NULL on error.

Parameters
$type : int

The page type, should be one of the type constants

formatMysqlDatetime()

Takes a mysql DATETIME value (will probably also work with a TIME or DATE value) and formats it according to the format codes specified by the PHP date() function.

public static formatMysqlDatetime(mixed $date[, mixed $format = 'd/m/Y g:i a' ]) : mixed

The format is optional, if omittted, uses 'd/m/Y g:i a' = '7/11/2010 5:27 pm'

Parameters
$date : mixed
$format : mixed = 'd/m/Y g:i a'

getEncrypt()

Get an encryption tool instance. Falls back to looking for configs in core encryption config file

public static getEncrypt([array<string|int, mixed>|string $config = 'default' ]) : EncryptInterface

This can be overriden by models or other places that need a different approach to encryption

Parameters
$config : array<string|int, mixed>|string = 'default'
Tags
throws
InvalidArgumentException
Return values
EncryptInterface

getInstalledVersion()

Determine the version for a composer package.

public static getInstalledVersion(string $package) : string
Parameters
$package : string
Return values
string —

e.g. 1.2.3 - #git-ref

getMemoryLimit()

Returns the memory limit in bytes.

public static getMemoryLimit() : int

If there is no limit, returns PHP_INT_MAX.

Return values
int —

Bytes

getSiteVersion()

Determine the version for the current site, i.e. the 'root' package.

public static getSiteVersion() : string

This doesn't include the semvar version but instead the package name. Often sites don't release a "version". Although perhaps identifying a deploy tag would be useful.

Return values
string —

e.g. package - #git-ref

getVersion()

Returns the current version of sprout

public static getVersion([bool $git_version = false ]) : string
Parameters
$git_version : bool = false

Optional flag to return git version, returns branding version by default

Return values
string —

e.g. 1.2.3 - #git-ref

instance()

Construct a new instance of a class with a given name

public static instance(class-string $class_name[, T>|array<string|int, class-string> $base_class_name = null ][, bool $assert_all = true ]) : object
Parameters
$class_name : class-string

The name of the class

$base_class_name : T>|array<string|int, class-string> = null

The base class or interface which the class must extend/implement. Can be a string for a single check, or an array for multiple checks. NULL disables this check.

$assert_all : bool = true

If true, all base classes/interfaces must be met.

Tags
example

$inst = Sprout::instance($widget_class, 'Sprout\Widgets\Widget');

template
throws
InvalidArgumentException

If the class does not exist

Return values
object —

The new instance

ipaddressInArray()

Check an IP against a list of IP addresses, with logic for CIDR ranges

public static ipaddressInArray(mixed $needle, mixed $haystack) : bool
Parameters
$needle : mixed
$haystack : mixed
Return values
bool —

True if the IP is in the list, false if it's not

iterableFirst()

Gets the first key value pair of an iterable

public static iterableFirst(iterable<string|int, mixed> $iter) : array<string|int, mixed>|null

use Arrays::firstPair

Parameters
$iter : iterable<string|int, mixed>

An array or Traversable

Tags
example

list ($key, $value) = Sprout::iterableFirst(['an' => 'array']);

Return values
array<string|int, mixed>|null —

An array of [key, value] or null if the iterable is empty

iterableFirstKey()

Gets the first key of an iterable

public static iterableFirstKey(iterable<string|int, mixed> $iter) : mixed|null

use Arrays::firstKey

Parameters
$iter : iterable<string|int, mixed>

An array or Traversable

Return values
mixed|null —

The value or null if the iterable is empty

iterableFirstValue()

Gets the first value of an iterable

public static iterableFirstValue(iterable<string|int, mixed> $iter) : mixed|null

use Arrays::first

Note, unlike the first key helper a null result here could be a valid value. You can check true emptiness using iterableFirst() or iterableFirstKey().

Parameters
$iter : iterable<string|int, mixed>

An array or Traversable

Return values
mixed|null —

The value or null if the iterable is empty

lastModified()

Return the last-modified date of all pages on the (sub-)site Returns NULL on error.

public static lastModified([string $date_format = 'd/m/Y' ]) : string|null

The date is formatted using the php date function. The default date format is "d/m/Y".

Parameters
$date_format : string = 'd/m/Y'

The date format to return the date in

Return values
string|null —

Last modified date, or null on error

linkChecker()

Back-end for link-checking tool

public static linkChecker() : mixed

moduleInstalled()

Is this module installed?

public static moduleInstalled(string $module_name) : bool

use Modules::isInstalled()

Parameters
$module_name : string

provided by ModuleInterface::getName()

Return values
bool —

if installed, otherwise false

nsClass()

Gets the full class name (including namespace) for a specified class, with a list of namespaces to search

public static nsClass(string $class, array<string|int, mixed> $possible_nses) : string
Parameters
$class : string

The class to instantiate, e.g. 'Fb'

$possible_nses : array<string|int, mixed>

Possible namespaces to try, e.g. ['Sprout\Helpers']

Tags
throws
Exception

if the class lookup failed

Return values
string

nsFunc()

Gets the full name (including namespaced class) for a specified function

public static nsFunc(string $func, array<string|int, mixed> $possible_nses) : string
Parameters
$func : string

The function to find, e.g. 'Fb::dropdown'

$possible_nses : array<string|int, mixed>

Possible namespaces to try, e.g. ['Sprout\Helpers']

Tags
throws
Exception

if the function lookup failed

Return values
string

nsNew()

Creates an object of a class specified by a string name, with a list of possible namespaces to lookup if the specified class name doesn't contain a namespace

public static nsNew(string $class, array<string|int, mixed> $possible_nses) : object
Parameters
$class : string

The class to instantiate

$possible_nses : array<string|int, mixed>

Possible namespaces to try

Tags
throws
Exception

if the class lookup failed

Return values
object

passwordComplexity()

public static passwordComplexity(mixed $str) : mixed

Use Security::passwordComplexity instead

Parameters
$str : mixed

randStr()

Returns a string of random numbers and letters

public static randStr([mixed $length = 16 ][, mixed $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890' ]) : mixed
Parameters
$length : mixed = 16
$chars : mixed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'

readableVar()

Converts a variable into something human readable

public static readableVar(mixed $var) : string
Parameters
$var : mixed
Return values
string

removeNs()

Removes the namespaces from a class-like entity, e.g. Sprout\Helpers\Text => Text

public static removeNs(string $classlike) : string
Parameters
$classlike : string
Return values
string

send()

Render out a response object.

public static send(ResponseInterface $response) : void

Note this doesn't fire any system events.

Parameters
$response : ResponseInterface

simpleBacktrace()

Gets a simplified backtrace with fewer elements and no recursion

public static simpleBacktrace([array<string|int, mixed> $trace = [] ]) : array<string|int, mixed>
Parameters
$trace : array<string|int, mixed> = []

If empty, the trace is automatically determined

Return values
array<string|int, mixed>

Adds classes, analytics and target-blank to file links.

public static specialFileLinks(string $html) : string

Also adds a random string, which prevents caching, solving some problems we were having with some clients.

Parameters
$html : string
Return values
string

timeAgo()

Returns a time in 'x minutes ago' format.

public static timeAgo(int $timediff) : mixed

Very small times (0, 1 seconds) are considered 'Just now'. Times are represneted in seconds, minutes, hours or days.

Parameters
$timediff : int

Amount of time that has passed, in seconds.

Translate an array which may contain a page_id, filename or url into the final URL to use

public static translateLink(mixed $row) : mixed
Parameters
$row : mixed
On this page

Search results