Delegator service factories

Zend\ServiceManager can instantiate delegators of requested services, decorating them as specified in a delegate factory implementing the delegator factory interface.

The delegate pattern is useful in cases when you want to wrap a real service in a decorator, or generally intercept actions being performed on the delegate in an AOP fashioned way.

Delegator factory signature

A delegator factory has the following signature:

1
2
3
4
5
6
namespace Zend\ServiceManager;

interface DelegatorFactoryInterface
{
    public function createDelegatorWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName, $callback);
}

The parameters passed to the DelegatorFactoryInterface#createDelegatorWithName factory are the following:

  • $serviceLocator is the service locator that is used while creating the delegator for the requested service
  • $name is the canonical name of the service being requested
  • $requestedName is the name of the service as originally requested to the service locator
  • $callback is a callable that is responsible of instantiating the delegated service (the real service instance)

A Delegator factory use case

A typical use case for delegators is to handle logic before or after a method is called.

In the following example, an event is being triggered before Buzzer::buzz() is called and some output text is prepended.

The delegated object Buzzer (original object) is defined as following:

1
2
3
4
5
6
7
class Buzzer
{
    public function buzz()
    {
        return 'Buzz!';
    }
}

The delegator class BuzzerDelegator has the following structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
use Zend\EventManager\EventManagerInterface;

class BuzzerDelegator extends Buzzer
{
    protected $realBuzzer;
    protected $eventManager;

    public function __construct(Buzzer $realBuzzer, EventManagerInterface $eventManager)
    {
        $this->realBuzzer   = $realBuzzer;
        $this->eventManager = $eventManager;
    }

    public function buzz()
    {
        $this->eventManager->trigger('buzz', $this);

        return $this->realBuzzer->buzz();
    }
}

To use the BuzzerDelegator, you can run the following code:

1
2
3
4
5
6
7
8
$wrappedBuzzer = new Buzzer();
$eventManager  = new Zend\EventManager\EventManager();

$eventManager->attach('buzz', function () { echo "Stare at the art!\n"; });

$buzzer = new BuzzerDelegator($wrappedBuzzer, $eventManager);

echo $buzzer->buzz(); // "Stare at the art!\nBuzz!"

This logic is fairly simple as long as you have access to the instantiation logic of the $wrappedBuzzer object.

You may not always be able to define how $wrappedBuzzer is created, since a factory for it may be defined by some code to which you don’t have access, or which you cannot modify without introducing further complexity.

Delegator factories solve this specific problem by allowing you to wrap, decorate or modify any existing service.

A simple delegator factory for the buzzer service can be implemented as following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
use Zend\ServiceManager\DelegatorFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class BuzzerDelegatorFactory implements DelegatorFactoryInterface
{
    public function createDelegatorWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName, $callback)
    {
        $realBuzzer   = call_user_func($callback);
        $eventManager = $serviceLocator->get('EventManager');

        $eventManager->attach('buzz', function () { echo "Stare at the art!\n"; });

        return new BuzzerDelegator($realBuzzer, $eventManager);
    }
}

You can then instruct the service manager to handle the service buzzer as a delegate:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$serviceManager = new Zend\ServiceManager\ServiceManager();

$serviceManager->setInvokableClass('buzzer', 'Buzzer'); // usually not under our control

// as opposed to normal factory classes, a delegator factory is a
// service like any other, and must be registered:
$serviceManager->setInvokableClass('buzzer-delegator-factory', 'BuzzerDelegatorFactory');

// telling the service manager to use a delegator factory to handle service 'buzzer'
$serviceManager->addDelegator('buzzer', 'buzzer-delegator-factory');

// now, when fetching 'buzzer', we get a BuzzerDelegator instead
$buzzer = $serviceManager->get('buzzer');

$buzzer->buzz(); // "Stare at the art!\nBuzz!"

You can also call $serviceManager->addDelegator() multiple times, with the same or different delegator factory service names. Each call will add one decorator around the instantiation logic of that particular service.

Another way of configuring the service manager to use delegator factories is via configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$config = array(
    'invokables' => array(
        'buzzer'                   => 'Buzzer',
        'buzzer-delegator-factory' => 'BuzzerDelegatorFactory',
    ),
    'delegators' => array(
        'buzzer' => array(
             'buzzer-delegator-factory'
             // eventually add more delegators here
        ),
    ),
);