ellipse/resolvers-composite

This package is abandoned and no longer maintained. No replacement package was suggested.

The composite resolver component of the ellipse framework

dev-master / 1.0.x-dev 2017-07-27 13:56 UTC

This package is not auto-updated.

Last update: 2017-08-01 12:39:50 UTC


README

This package provides a composite middleware resolver.

It allows to combine multiple resolvers as one resolver to be used with ellipse middleware dispatcher.

Require php >= 7.1

Installation composer require ellipse/resolvers-composite

Run tests ./vendor/bin/peridot tests

Combining two resolvers

This package provides a Ellipse\Resolvers\CompositeResolver class which is an implementation of the interface Ellipse\Contracts\Resolver\ResolverInterface taking two instances of ResolverInterface as parameters. The resulting combined resolver will try to resolve values into middleware with the first one, then will try the second one when the first fail. This allows to combine any number of resolvers together.

<?php

use Ellipse\Resolvers\CompositeResolver;
use App\Resolvers\SomeResolver;
use App\Resolvers\SomeOtherResolver;
use App\Resolvers\YetSomeOtherResolver;

$resolver1 = new SomeResolver;
$resolver2 = new SomeOtherResolver;

$resolver = new CompositeResolver($resolver1, $resolver2);

// This dispatcher will use SomeResolver and SomeOtherResolver to resolve values as middleware.
$dispatcher = new Dispatcher(['element'], $resolver);

// Composite resolvers can be combined as well.
$resolver = new CompositeResolver($resolver, new YetSomeOtherResolver);

// This dispatcher will use SomeResolver, SomeOtherResolver and AgainSomeOtherResolver to resolve elements
// as middleware.
$dispatcher = new Dispatcher(['element'], $resolver);

If you want to create a package providing a resolver and automatically combine your resolver with the one already provided by the container, you can write an interop service provider defining a composite resolver with yours as the second parameter. This is the approach used by the ellipse resolvers.

<?php

namespace My\Package;

use Interop\Container\ServiceProvider;

use Ellipse\Contracts\Resolver\ResolverInterface;

use Ellipse\Resolvers\CompositeResolver;

class MyPackageServiceProvider implements ServiceProvider
{
    public function getServices()
    {
        return [
            MyResolver::class => function ($container) {

                // Construct an instance of MyResolver and returns it.

            },

            ResolverInterface::class => function ($container, $previous = null) {

                $resolver = $container->get(MyResolver::class);

                return ! is_null($previous)
                    ? new CompositeResolver($previous(), $resolver)
                    : $resolver;

            },
        ];
    }
}