Using Interfaces - rougin/slytherin GitHub Wiki
← Using Application as a Router | Integrating Components →
In Slytherin, object interfaces are used to integrate its defined components. Below are the available components of Slytherin with their respective interface names:
Container
- The most important component in the Slytherin core as it is responsible for preparing its dependencies.
- Containers store the instances and dependencies of an application for later use.
- The package must be implemented in
Rougin\Slytherin\Container\ContainerInterface:
namespace Rougin\Slytherin\Container;
use Psr\Container\ContainerInterface as PsrContainerInterface;
interface ContainerInterface extends PsrContainerInterface
{
/**
* Sets a new instance to the container.
*
* @param string $id
* @param mixed $concrete
* @return self
*/
public function set($id, $concrete);
}
[!NOTE] The said
ContainerInterfaceis an extended implementation of the PSR-11 standard (psr/container).
- Third-party implementations for the
Containercomponent can be found in its Container page.
Routing
- One of the required components of Slytherin that dispatches defined HTTP routes to different handlers.
- To implement a custom
Routingcomponent, it must be implemented inRougin\Slytherin\Routing\DispatcherInterface:
namespace Rougin\Slytherin\Routing;
interface DispatcherInterface
{
/**
* Dispatches against the provided HTTP method verb and URI.
*
* @param string $method
* @param string $uri
* @return \Rougin\Slytherin\Routing\RouteInterface
*
* @throws \BadMethodCallException
*/
public function dispatch($method, $uri);
/**
* Sets the router and parse its available routes if needed.
*
* @param \Rougin\Slytherin\Routing\RouterInterface $router
* @return self
*
* @throws \UnexpectedValueException
*/
public function setRouter(RouterInterface $router);
}
- Its own Routing page shows more information for the said component and its supported third-party implementations.
Http
- A required component for Slytherin that should provide a nice object-oriented interface for handling HTTP variables.
- To integrate a custom
Httpcomponent to Slytherin, must be implemented in a PSR-07 standard (psr/http-message):
namespace Psr\Http\Message;
interface ServerRequestInterface extends RequestInterface
{
}
namespace Psr\Http\Message;
interface ResponseInterface extends MessageInterface
{
}
- For creating third-party implementations for this component, kindly check the Http page in this documentation.
Error Handler
- An optional component for Slytherin that detects and displays error messages in a beautiful web page.
- Writing custom error handlers in Slytherin must be implemented in
Rougin\Slytherin\Debug\ErrorHandlerInterface:
namespace Rougin\Slytherin\Debug;
interface ErrorHandlerInterface
{
/**
* Registers the instance as an error handler.
*
* @return void
*/
public function display();
}
- Kindly check the Error Handler page for using alternative third-party implementations for the said component.
Middleware
- It is an optional component to Slytherin that acts as a middle layer to manipulate an HTTP request and HTTP response.
- Writing middlewares must be implemented in PSR-07 and PSR-15 standards (
psr/http-messageandpsr/http-server-middlewareorhttp-interop/http-middleware) or throughRougin\Slytherin\Middleware\MiddlewareInterface:
namespace Rougin\Slytherin\Middleware;
use Psr\Http\Message\ServerRequestInterface;
interface MiddlewareInterface
{
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, HandlerInterface $handler);
}
- When writing custom middleware dispatchers for Slytherin, it must be implemented in
Rougin\Slytherin\Middleware\DispatcherInterface:
namespace Rougin\Slytherin\Middleware;
interface DispatcherInterface extends MiddlewareInterface
{
/**
* Returns the list of added middlewares.
*
* @return \Rougin\Slytherin\Middleware\MiddlewareInterface[]
*/
public function getStack();
/**
* Add a new middleware to the end of the stack.
*
* @param mixed $middleware
* @return self
*/
public function push($middleware);
/**
* Sets a new stack of middlewares.
*
* @param mixed[] $stack
* @return self
*/
public function setStack($stack);
}
- More details and information in writing middlewares and its dispatchers in the Middleware page.
Template
- It is an optional component for Slytherin for abstracting the PHP's built-in template engine and to separate the concerns between the template and its respective data.
- Writing custom implementations of the said components requires an implementation of
Rougin\Slytherin\Template\RendererInterface:
namespace Rougin\Slytherin\Template;
interface RendererInterface
{
/**
* Renders a template.
*
* @param string $template
* @param array<string, mixed> $data
* @return string
*/
public function render($template, array $data = array());
}
- An example on how to use template renderers can be found in the Template page.
Implementing a Custom Component
Once the custom components were implemented in one of the defined interfaces from Slytherin, the said components must be defined using ContainerInterface. A guide for integrating the said components to Slytherin can be found in the Integrating Components page.