如何定义自己的可调用程序列表并从AbstractFactory访问它


How to define your own list of invokables and access this from a AbstractFactory

我想构建自己的可调用程序列表,并从我的AbstractFactory访问它。

/**
 * Get the service config
 */
public function getServiceConfig()
{
    return array(
        'invokables' => array(
        ),
        'foo-invokables' => array(
            'FooService' => 'Foo'Service'FooService',
         )
    );
}

然后,工厂应该检查这个列表,以查看别名是否在foo可调用程序列表中。

public function canCreateServiceWithName(ServiceLocatorInterface $objServiceManager, $sCanonicalName, $sRequestedName) {
    // TODO check if the $sRequestedName is contained with in the foo-invokables return true
}

提前谢谢。

你可以这么简单地完成:

class Module implements ConfigProviderInterface //...
{
    //...
    public function getConfig()
    {
        return [
            'my_invokables' => [
                'MyInvokables'Invokable1',
                'MyInvokables'Invokable2',
            ]
        ];
    }
    //...
}
class AbstractMyInvokablesFactory implements AbstractFactoryInterface
{
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        $config = $serviceLocator->get('config');
        return in_array($requestedName, $config['my_invokables']);
    }
    //...
}