zendframework / zend-servicemanager

ServiceManager component from Zend Framework
BSD 3-Clause "New" or "Revised" License
188 stars 89 forks source link

Allow delegators to decorate the `$options` array #276

Closed tux-rampage closed 5 years ago

tux-rampage commented 5 years ago

This PR allows delegator factories to decorate the $options array that is passed to the actual factory.

Use case:


class MyService
{
    public function __construct(array $types)
    { /* ... */ }
}

$serviceConfig = [
    'factories' => [
        MyService::class => fuction($container, $name, $options) {
            return new MyService($options['types'] ?? []);
        }
    ],
    'delegators' => [
        MyService::class => [
            function($container, $name, $factory, $options) {
                $options['types'] = array_merge(
                    $options['types'] ?? [],
                    [ 'string' => 1 ]
                );
                $factory($options);
            },
        ],
    ],
]; 
tux-rampage commented 5 years ago

@weierophinney It was just an idea and maybe feature creep indeed. I had some idea in correlation with zend-di's auto wire factory, but I don't remeber it anymore tbh. Of course there are better solutions and I might have come up with a better one finally.

Maybe it's better to drop the $options array for service manager altogether?

Feel free to close if nobody has a need for this use case.

weierophinney commented 5 years ago

Maybe it's better to drop the $options array for service manager altogether?

That's one of the things I'd like to do for zend-servicemanager v4.

I'll close this for now; if you run across the problem you were trying to solve, and find that this is the best way to solve it, re-open and update the PR to detail it. :smile: