dev-master
9999999-devThe lightweight core of SPF - Simon's PHP Framework
MIT
The Requires
The Development Requires
by Simon Downes
framework php spf
Wallogit.com
2017 © Pedro Peláez
The lightweight core of SPF - Simon's PHP Framework
The lightweight core of SPF (Si's PHP Framework). Provides error and exception handling and commonly used helper functions., (*1)
This library requires only PHP 7.2 or later and the SPF Contracts package (simon-downes/spf-contracts)., (*2)
It is installable and autoloadable via Composer as simon-downes/spf-core., (*3)
Alternatively, download a release or clone this repository, and add the \spf namespace to an autoloader., (*4)
SPF Core is open-sourced software licensed under the MIT license. See LICENSE.md for details., (*5)
SPF Core provides a basic exception and error handling wrapper for blocks of code, be they command-line scripts, complete web apps or simple functions., (*6)
Running code with SPF is a simple as calling the static run() method with a callable parameter., (*7)
use spf\SPF;
// Using a closure
SPF::run(function() {
echo "Hello World";
});
// Using a function name
function hello() {
echo "Hello World";
}
SPF::run('hello');
// Using an object...
class Foo {
public static function hello() {
echo "Hello World";
}
public function helloWorld() {
echo "Hello World";
}
public function __invoke() {
$this->helloWorld();
}
}
// ...static callback
SPF::run(['Foo', 'hello']);
// ...instance callback
$o = new Foo();
SPF::run([$o, 'helloWorld']);
// ...invokable object
SPF::run($o);
SPF Core provides default error and exception handlers:
* Errors are converted to ErrorExceptions and passed to the exception handler if the error matches the current error_reporting level.
* Exceptions are handled by the specified exception handler or a default one if none is specified., (*8)
You can specify a custom exception handler by passing a callable to the appropriate method:, (*9)
use spf\SPF; SPF::setExceptionHandler($callback);
SPF provides a debug flag accessible via:, (*10)
use spf\SPF; // enable/disable debug flag SPF::setDebug(true); // Return current setting of debug flag SPF::isDebug();
Usage of the debug flag is left almost entirely to clients. The only uses within the framework are:
* to determine whether to display a detailed error page (if debug flag is set) or a simple static error page
* calls to the d() and dd() dump functions are ignored if the debug flag is not set, (*11)
The default simple static error page can be overriden: by passing the path and file name to the ```, (*12)
use spf\SPF; SPF::setErrorPage($path_to_file);
SPF provides an enhanced var_dump() implementation that can output detailed variable information in plain text., (*13)
use spf\SPF; SPF:dump($var);
SPF also defines two shortcut functions for accessing the variable dumping functionality:
* d() - calls SPF::dump() for each passed argument
* dd() - same as d() but will call die() once the arguments have been dumped, (*14)
These shortcut methods do nothing if the debug flag is not set., (*15)
SPF provides a variety of helper functions and more can be easily added. Helper functions are implemented as static
class methods and registered either by passing the class name to the SPF::registerHelper() method (registers all
public static methods) or by passing a class and method name to the SPF::addHelperMethod() (registers a single method).
Once registered helper functions can be called via static method call to SPF., (*16)
use spf\SPF;
class MyHelper {
public static function foo() {
echo 'foo';
}
public static function bar() {
echo 'bar';
}
}
SPF::registerHelper('\\MyHelper');
SPF::foo();
SPF::bar();
isCLI() - determines if the script is running in a CLI environmentuniqueIntegers() - return an array of unique integersisTraversable() - determines if a variable can be interated over using foreach
isAssoc() - determine if an array is associative or notfilterObjects() - filter an array to instances of a specific classget() - return an item from an array or object or a default value if the item doesn't existgetNullItems() - filter an array to items that are nullpluck() - extract a single field from an array of arrays of objectssum() - calculate the sum of the specified item from an array of arrays or objectsmin() - calculate the min of the specified item from an array of arrays or objectsmax() - calculate the max of the specified item from an array of arrays or objectsimplodeAssoc() - implode an associative array into an array of key/value pairsmakeComparer() - create a comparison function for sorting multi-dimensional arraysmakeTimestamp - convert a value into a timestampseconds() - convert a string representation containing one or more of hours, minutes and seconds into a total number of secondsparseURL() - parse a url into an array of it's componentsrandomHex() - generate a random hex string of a specific lengthrandomString() - generate a random string of a specific lengthuncamelise() - convert a camel-cased string to lower case with underscoresslugify() - convert a string into a form suitable for urlsremoveAccents() - convert accented characters to their regular counterpartslatin1() - convert a UTF-8 string into Latin1 (ISO-8859-1)utf8() - convert a Latin1 (ISO-8859-1) into UTF-8ordinal() - return the ordinal suffix (st, nd, rd, th) of a numbersizeFormat() - convert a number of bytes to a human-friendly string using the largest suitable unit, (*17)
xssClean() - remove XSS vulnerabilities from a string, (*18)
stripControlChars() - remove control characters from a stringpluralise() - Determine the plural form of a word (English only)singularise() - Determine the single form of a word (English only)The lightweight core of SPF - Simon's PHP Framework
MIT
framework php spf