Skip to content

Components Implementation

Rougin Gutib edited this page Aug 10, 2024 · 9 revisions

← "ContainerInterface" Implementation | "IntegrationInterface" Implementation →

Prior to the concept of PSR-11, the Components class is used for handling Slytherin's core components. In the current version of Slytherin, the said class is also implemented to the ContainerInterface:

 // app/web/index.php

-use Psr\Http\Message\ResponseInterface;
-use Psr\Http\Message\ServerRequestInterface;
 use Rougin\Slytherin\Application;
-use Rougin\Slytherin\Container\Container;
+use Rougin\Slytherin\Components;
 use Rougin\Slytherin\Http\Response;
 use Rougin\Slytherin\Http\ServerRequest;
 use Rougin\Slytherin\Routing\Dispatcher;
-use Rougin\Slytherin\Routing\DispatcherInterface;
 use Rougin\Slytherin\Routing\Router;

 // ...

+$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);
 // -------------------------------------------------------------

 // ...

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

-// Add the above objects through a container ------------
-$container = new Container;
-
-$container->set(ServerRequestInterface::class, $request);
-
-$container->set(ResponseInterface::class, $response);
-
-$container->set(DispatcherInterface::class, $dispatcher);
-// ------------------------------------------------------

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

Usage of Collection class

The Components class is only another name for the Collection class. More information for this class can be found in its own Collection page.

Using the IntegrationInterface

As the previously discussed implementation is created prior to the ContainerInterface, there is another way for abstracting application code which is the IntegrationInterface implementation. A guide for the said implementation can be found in its own "IntegrationInterface" Implementation page.

← "ContainerInterface" Implementation | "IntegrationInterface" Implementation →