2014-10-29 20 views
0

我想構建自己的可調用列表並從AbstractFactory訪問它。如何定義自己的可調用列表並從AbstractFactory訪問此列表

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

工廠應該檢查此列表以查看別名是否在foo-invokable列表中。

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

在此先感謝。

+1

如果「請求名稱」已經是「invokables」配置中,那麼你應該從來沒有獲得到'AbstractFactory'。如果服務管理器找不到具有該名稱的任何註冊服務,則服務管理器將僅使用抽象工廠作爲回退。 – AlexP 2014-10-29 12:36:10

回答

1

你可以做到這一點,因爲這是簡單的:

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']); 
    } 

    //... 
} 
+0

不完全是我以後。不過謝謝。 – user3639761 2014-10-30 12:11:58

相關問題