Skip to content
Rougin Gutib edited this page Aug 10, 2024 · 8 revisions

← Integrating Components | Container Component →

The Collection class is a utility class that handles the setting up of Slytherin's components. The said class was previously named as Components and was mainly used prior to the official implementation of the PSR-11 standard.

Example Code

Since the Components class implements the ContainerInterface, the said class can be used as an argument to the Application class:

// app/web/index.php

use Rougin\Slytherin\Application;
use Rougin\Slytherin\Components;
use Rougin\Slytherin\Http\Response;
use Rougin\Slytherin\Http\ServerRequest;
use Rougin\Slytherin\Routing\Dispatcher;
use Rougin\Slytherin\Routing\Router;

require 'vendor/autoload.php';

$components = new Components;

// Define HTTP objects that is compliant to PSR-07 standards ---
$request = new ServerRequest((array) $_SERVER);
$components->setHttpRequest($request);

$response = new Response(http_response_code());
$components->setHttpResponse($response);
// -------------------------------------------------------------

// Create a new router to add an HTTP route... ---
$router = new Router;

$router->get('/', function ()
{
    return 'Hello world!';
});
// -----------------------------------------------

// ...then define it to a dispatcher ---
$dispatcher = new Dispatcher($router);

$components->setDispatcher($dispatcher);
// -------------------------------------

// Lastly, run the application ------
(new Application($components))->run();
// ----------------------------------

Class Reference

namespace Rougin\Slytherin\Component;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Container\ContainerInterface;
use Rougin\Slytherin\Debug\ErrorHandlerInterface;
use Rougin\Slytherin\Middleware\DispatcherInterface as Middleware;
use Rougin\Slytherin\Routing\DispatcherInterface as Routing;
use Rougin\Slytherin\Template\RendererInterface;

class Collection implements ContainerInterface
{
    /**
     * Returns the dispatcher.
     *
     * @return \Rougin\Slytherin\Dispatching\DispatcherInterface
     */
    public function getDispatcher();

    /**
     * Returns the error handler.
     *
     * @return \Rougin\Slytherin\Debug\ErrorHandlerInterface
     */
    public function getErrorHandler();

    /**
     * Returns the HTTP request and response.
     *
     * @return mixed
     */
    public function getHttp();

    /**
     * Returns the HTTP request.
     *
     * @return \Psr\Http\Message\ServerRequestInterface
     */
    public function getHttpRequest();

    /**
     * Returns the HTTP response.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getHttpResponse();

    /**
     * Returns the middleware.
     *
     * @return \Rougin\Slytherin\Middleware\DispatcherInterface
     */
    public function getMiddleware();

    /**
     * Returns the template.
     *
     * @return \Rougin\Slytherin\Template\RendererInterface
     */
    public function getTemplate();

    /**
     * Sets the dispatcher.
     *
     * @param  \Rougin\Slytherin\Dispatching\DispatcherInterface $dispatcher
     * @return self
     */
    public function setDispatcher(Routing $dispatcher);

    /**
     * Sets the error handler.
     *
     * @param  \Rougin\Slytherin\Debug\ErrorHandlerInterface $debugger
     * @return self
     */
    public function setErrorHandler(ErrorHandlerInterface $debugger);

    /**
     * Sets the HTTP components.
     *
     * @param  \Psr\Http\Message\ServerRequestInterface $request
     * @param  \Psr\Http\Message\ResponseInterface      $response
     * @return self
     */
    public function setHttp(ServerRequestInterface $request, ResponseInterface $response);

    /**
     * Sets the HTTP request.
     *
     * @param  \Psr\Http\Message\ServerRequestInterface $request
     * @return self
     */
    public function setHttpRequest(ServerRequestInterface $request);

    /**
     * Sets the HTTP response.
     *
     * @param  \Psr\Http\Message\ResponseInterface $response
     * @return self
     */
    public function setHttpResponse(ResponseInterface $response);

    /**
     * Sets the middleware.
     *
     * @param  \Rougin\Slytherin\Middleware\DispatcherInterface $middleware
     * @return self
     */
    public function setMiddleware(Middleware $middleware);

    /**
     * Sets the template.
     *
     * @param  \Rougin\Slytherin\Template\RendererInterface $template
     * @return self
     */
    public function setTemplate(RendererInterface $template);
}

← Integrating Components | Container Component →