Explanation:

This product is intended for RESTful webservices or websites. 

The purpose of this class is to Map URLs to Controller classes. The main class is the RestServer, wich is responsible for holding the Request, the Response, the Url map and to make decisions about what to load. 

You can map a URL by calling the method addMap() on a RestServer instance, as above:

$rest = new RestServer ;
$rest->addMap("GET","/user","UserController");

Instead of processing the requested URL, you may provide one by passing it to
the RestServer:

$rest = new RestServer($_GET["url"]);

The addMap() method takes three arguments, as follow:
1- The Request Method, ie:
1.1- GET, POST, PUT, DELETE, OPTION and others.
2- The URL pattern is a pearl compatible regular expression, but without delimiters.  Here are some examples:
2.1- "/","/user","/device/[a-z0-9]+","/post/[0-9]+/comments","/article/[0-9]*/photo/[0-9]*".
3- A controller/view class, that must implement RestController or RestView.  There are no naming conventions imposed by RestServer.
3.1- Can also specify a class method such as "Profile::full".  It will not be called staticly.

The Controller(preferred) or View to receive the request must implement RestController or RestView. The RestController should implement the execute() method receiving a RestServer as a parameter, as follow:

class Anything implements RestController {
    function execute(RestServer $rest) {
        // Logic here
        return $rest ;
    }
}

And the RestView implementation must use show() method also receiving a RestServer, as follow:

class Anyview implements RestView {
    function show(RestServer $rest) {
        //Logic here
        return $rest;
    }
}

Both need to have a parameter-free public constructor(or no constructor at all). Alternative methods must also receive the RestServer as a parameter. Ideally it should always return something, but that's not mandatory. One can return the RestServer, ending the session, or a RestAction(which is any RestController or RestView) to be taken next. Example:

class Anything implements RestController {
    function execute(RestServer $rest) {
        return new Anyview ;
    }
}

class Anyview implements RestView {
    function show(RestServer $rest) {
        return $rest;
    }
}

It also adds a convinience GenericView class, for basic templating on php scripts, as follow:

class Homepage implements RestController {
    function execute(RestServer $rest) {
        // Logic in here
        return new GenericView("path_to_php_script");
    }
}

For recovering information about the request taken, the RestServer provides the RestRequest object. You can get it by calling the getRequest() method on the RestServer. There are a few methods available on the RestRequest:

isGet() // If the request is a GET
isPost() // If the request is a POST
isPut() // If the request is a PUT
isDelete() // If the request is a Delete
getGet($k=null) // Return the $_GET
getPost($k=null) // Return the $_POST
getFiles($k=null) // Return the $_FILES
getPut($k=null) // Return the uploaded content
getAuthData() // Return the Authentication data for DIGEST auth
getUser() // Return the User for Basic authentication
getPassword() // Return the Password for Basic authentication
getMethod() // Return the Request Method(get, put, post)
setMethod($m) // Set the Request Method, for action forwarding
getRequestURI() // Get the URI
getURIpart($i) // Get Part of the URL, divided by "/"
getURI($i) // same as above
getExtension() // Get the URI extension(html, xml, json)
acceptMime($mime) // If Request accept given MIME type

To build up a response, the RestServer provide the Response Object.  Access it by calling the method getResponse() on the RestServer. The RestResponse gives you the following methods:

cleanResponse() // Erase any response
addHeader($header) // Adds an HTTP Header
cleanHeader() // Erase any header
showHeader() // Show headers
headerSent() // Returns true if headers were sent
setResponse($response) // Set the response content.  It could be a string(like an HTML document) or a RestAction
addResponse($response) // Append a response, string only
getResponse() // Get the Response content(or class, if RestAction)

Also the RestServer provides a few methods itself:

setParameter($key,$value) // Set a RestServer class parameter
getParameter($key) // Retrieves a parameter
setQuery($value,$k=null) // Set the query(uri) to deal with, instead of the Resquest RequestURI
addMap($method,$uri,$class) // Already covered
isAuth() // Return true if the object is authenticated
requireAuth($bol) // If is required
unAuth() // As for credentials
setAuth($bool) // Set the RestServer authentication
getQuery($k=null) // Get part of the Query(URI)
getBaseUrl() // Get the base URL of the service
getResponse() // Get the RestResponse object
getRequest() // Get the RestRequest object

See the examples for more information.

This was developed by Diogo Souza da Silva <manifesto@manifesto.blog.br>.