velsym/universal-pack

Connector for velsym packages.

composer require velsym/universal-pack

Velsym\Communication

Request

A static class containing current request info.

Methods

  • method - returns name of the HTTP method used in the current request. e.g. GET, POST...

    Returns: string

  • url - returns info about url.

    Returns: UrlInfo


UrlInfo

A readonly class:

Properties

  • path - string value containing path e.g. /user/21.

  • query - a key-value array containing query string of the requested URI.

Methods

  • __construct(URI) - constructs UrlInfo.

    Returns: UrlInfo

    Params:

    • URI - string containing URI.


Response

A singleton which is used to set response content.

You shouldn't use any of the methods unless you're making a package or you know exactly what you're doing. If you are using Velsym on it's own, be aware that you can achieve all of it's capabilities in a more safe and elegant way.

Methods

  • getInstance() - returns instance of the Response.

    Returns: self

  • addHeader(header, value) - sets HTTP header.

    Returns: self

    Params:

    • header - name of the header.

    • value - value of the header (after colon).

  • addHeaders(headers[]) - add multiple headers at once using an array.

    Returns: self

    Params:

    • headers - key-value array. Key is name of the header and value is the value of the header.

  • getHeaders() - returns key-value array of all headers.

    Returns: array

  • setBody(body) - takes string as the body of the response.

    Returns: void

    Params:

    • body - body of the response.

  • getBody() - returns body.

    Returns: string

  • setResponseCode(responseCode) - sets HTTP status code.

    Returns: void

    Params:

    • responseCode - an integer of the response code.

  • getResponseCode() - returns integer representing HTTP status code.

    Returns: int

  • getRedirectRouteName() - returns name of redirect route if set.

    Returns: NULL|string

  • setRedirectRouteName(redirectRouteName) - sets the route that client should be redirected to.

    Returns: self

    Params:

    • redirectRouteName - string containing name of the route or NULL if want to unredirect.

Examples

addHeader(s) method
use Velsym\Communication\Response;

$response = Response::getInstance();
$response->addHeader("Location", "/home/");
// ^ Sets HTTP Header:   Location: /home/

$headers = [
    "Content-Encoding" => "gzip",
    "Connection" => "close"
];

$response->addHeaders($headers);

Velsym\Dependency

DependencyBuilder

Class simplifying construction of dependencies.

Methods

  • addDependency(dependency) - entrypoint for every dependency.

    Returns: self

    Params:

    • dependency - fully qualified class name that represents an underlying class.

  • setClass(class) - sets the real class.

    Returns: self

    Params:

    • class - fully qualified class name of class that will be resolved.

  • setParam(paramName, value) - when real class requires some kind of parameters in the constructor, you can set the default parameter value here.

    Returns: self

    Params:

    • paramName - name of the parameter defined in the constructor of the real class.

    • value - value that will be passed to the parameter of name paramName.

  • getDependencies() - returns an array that stores all of the dependencies and can be read by DependencyManager.

    Returns: array

Examples

How to make a dependency
use Velsym\Dependency\DependencyBuilder;

//Interface doesn't contain anything and acts as a label on which parameters to use.
interface Male
{
}

interface Female
{
}

class Person
{
    public function __construct(string $name, int $age)
    {
        // ...
    }
    // ...
}

$builder = (new DependencyBuilder("The awesome dependencies!"))
    ->addDependency(Male::class) // Interface 
        ->setClass(Person::class) // Real Class
            ->setParam("name", "John")
            ->setParam("age", 21)
            
    ->addDependency(Female::class)
        ->setClass(Person::class)
            ->setParam("name", "Julia")
            ->setParam("age", 19)

    ->addDependency("Abstract\\Class")
        ->setClass("Real\\Class")
            ->setParam("path", "/unix/path/to/file.php")
            ->setParam("array", ['the', 'array', 'number', 1])
    ;

Velsym\Routing

BaseMiddleware

This class is must be extended by every middleware.

Methods

  • handle() - method that is implemented by extended class. Router is responsble of executing this method. Do not call it unless you know what you're doing.

    Returns: void

  • redirect(path) - redirects client to another url.

    Returns: void

    Params:

    • path - string path to which client should be redirected.

Last updated