Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Factory Parameters #362

@bradynpoulsen

Description

@bradynpoulsen

It would be really nice if I could define specific parameters in my factory.

Take the following example where I want to build an array of delegates for a handler

class SomeHandlerFactory
{
    public function create(SomeHandler $handler, array $delegates)
    {
        $handler->doSomethingCool();
        array_map([$handler, 'pushDelegate'], $delegates);
        return $handler;
    }
}

I would like to define the array of delegates to isolate accessing the container directly to my config.php

// config.php
return [
    'SomeHandler' => factory(['SomeHandlerFactory', 'create'])->param('delegates', get('handlerDelegates')),
    'handlerDelegates' => function (ContainerInterface $container) {
        $delegates = [];
        if (Config::get('someSetting')) {
            array_push($delegates, $container->get('DelegateA'));
        }
        if (Config::get('awesomeMode')) {
            array_push($delegates, $container->get('ImpeachTheOffice'));
        }
        return $delegates;
    }
];

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions