Skip to content

IntegrationInterface Implementation

Rougin Gutib edited this page Dec 15, 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