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

Skip to content

Service locator default_index_method support array output #62337

@web-fu

Description

@web-fu

Description

I could not find a way in the documentation to index the same service class with multiple names using default_index_method
This would be usefull whan you have to use complex strategy pattern

Example

class StrategyFooAction implements ActionInterface
{
    public static function getProviderName(): string
    {
        return 'Strategy/Foo/Action';
    }

    public static function action(): mixed
   {
       ... 
   }
}

class StrategyBarAction implements ActionInterface
{
    public static function getProviderName(): string
    {
        return 'Strategy/Bar/Action';
    }

    public static function action(): mixed
   {
       ... 
   }
}

If "Foo/Action" and "Bar/Action" must do the same thing it would useful to define:

class StrategyFooBarAction implements ActionInterface
{
    public static function getProviderName(): array
    {
        return [
            'Strategy/Foo/Action',
            'Strategy/Bar/Action',
        ];
    }

    public static function action(): mixed
   {
       ... 
   }
}

And when I require the service:

$actionService = $serviceLocator->get('Strategy/'.$case.'/Action');
$actionService->action($data);

I could get StrategyFooBarAction if $case are or Foo or Bar, but not for other cases.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions