Skip to content

IntegrationInterface Implementation

Rougin Gutib edited this page Dec 21, 2023 · 18 revisions

The IntegrationInterface implementation is another way of organizing the application code and make it reusable. Its concept is similar to the Components class but their difference is that the IntegrationInterface is not being used exclusively for Slytherin's core components. With this, it is possible for defining the instances needed to an application first prior receiving the HTTP request.

Basic Example

The example below is an implementation of the RoutingIntegration. This integration will create a class that is based on the DispatcherInterface of the Routing component and will accept a RouterInterface from the configuration:

namespace Rougin\Slytherin\Routing;

use Rougin\Slytherin\Container\ContainerInterface;
use Rougin\Slytherin\Integration\Configuration;
use Rougin\Slytherin\Integration\IntegrationInterface;
use Rougin\Slytherin\System;

class RoutingIntegration implements IntegrationInterface
{
    // ...

    /**
     * Defines the specified integration.
     *
     * @param  \Rougin\Slytherin\Container\ContainerInterface $container
     * @param  \Rougin\Slytherin\Integration\Configuration    $config
     * @return \Rougin\Slytherin\Container\ContainerInterface
     */
    public function define(ContainerInterface $container, Configuration $config)
    {
        $dispatcher = new Dispatcher;

        $router = $config->get('app.router', new Router);

        if ($this->wants('fastroute'))
        {
            $dispatcher = new FastRouteDispatcher;
        }

        if ($this->wants('phroute'))
        {
            $dispatcher = new PhrouteDispatcher;
        }

        $container->set(System::DISPATCHER, $dispatcher);

        return $container->set(System::ROUTER, $router);
    }

    // ...
}

Sample Implementation

To add an integration, use the integrate method from the Application class prior to the run method:

 use Rougin\Slytherin\Application;
 use Rougin\Slytherin\Components;
+use Rougin\Slytherin\Configuration;
 use Rougin\Slytherin\Http\Response;
 use Rougin\Slytherin\Http\ServerRequest;
-use Rougin\Slytherin\Routing\Dispatcher;
 use Rougin\Slytherin\Routing\Router;
+use Rougin\Slytherin\Routing\RoutingIntegration;

 require 'vendor/autoload.php';

 $components = new Components;

+$config = new Configuration;
+
 // Define HTTP objects that is compliant to PSR-07 standards ---
 $request = new ServerRequest((array) $_SERVER);
 $components->setHttpRequest($request);
@@ -28,14 +31,16 @@ $router->get('/', function ()
 {
     return 'Hello world!';
 });
+
+$config->set('app.router', $router);
 // -----------------------------------------------

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

 // Lastly, run the application ------
-(new Application($components))->run();
+$app = new Application($components);
+
+$app->integrate($items, $config)->run();
 // ----------------------------------

Based on the sample code above, the IntegrationInterface implementation can coexist with the previously mentioned implementations. It only depends on the proposed folder structure or implementation of an application.

Integrating custom Slytherin Components

As using IntegrationInterface is an alternative for setting Slytherin's components, the important part is defining the custom implementations using Slytherin's defined interfaces. A guide for the list of Slytherin's interfaces can be found in the Using Interfaces page.