0

複数のデータベースを使用しています。したがって、global.phpファイルには次のような構成があります。

return [
'db' => [
    ...
    ],
    'adapters' => [
        'adapter1' => [
            ...
            ]
        ],
        'adapter2' => [
            ...
            ]
        ]
    ]
]
];

私の目標は、すべてのアダプターの構成を 1 つの命令で取得することです。それが可能かどうかはわかりません。1 つのアダプターを取得するには、次の手順があります。

$sm->get('adapter1');

しかし、私はそのようなものが欲しいです:

$sm->get('adapters');

今のところ解決策が見つからなかったので、ここで質問することにしました...ファイルに単純な配列が含まれているだけなので、これは可能だと思います...

あなたのもの

編集

私は試した

$sm->get('config')['db']['adapters'];

ただし、文字列の配列を返します。実際には、オブジェクトの配列が必要です。アダプターのオブジェクト。この方法でアダプターを取得するのと同じ方法:

$sm->get('adapter1');

あなたのもの

4

2 に答える 2

0

アダプターオブジェクトのコレクションが必要な場合は、これをサービス マネージャーに登録するサービスとしてモデル化できます。

このサービスは、 と呼ばれる単純なコレクション クラスDbAdapterCollectionです。各アダプター オブジェクトは、その名前で格納および参照されます。

class DbAdapterCollection
{
    protected $adapters = [];

    public function __construct(array $adapters = [])
    {
        $this->setAdapters($adapters);
    }

    public function hasAdapter($name)
    {
        return isset($this->adapters[$name]);
    }

    public function getAdapters()
    {
        return $this->adapters;
    }

    public function setAdapters(array $adapters)
    {
        $this->adapters = [];

        foreach ($adapters as $name => $adapter) {
            $this->setAdapter($name, $adapter);
        }
    }

    public function getAdapter($name)
    {
        if (! $this->hasAdapter($name)) {
            throw new \RuntimeException('An adapter with the name \'' . $name . '\' could not be found.');
        }
        return $this->adapters[$name];
    }

    public function addAdapter($name, \Zend\Db\Adapter $adapter)
    {
        $this->adapters[$name] = $adapter;
    }
}

このクラスにアダプタオブジェクトを設定するには、サービス ファクトリを使用してそれらをクラスに注入します__construct。サービス マネージャーを使用して必要な構成を読み込むこともできるため、どのアダプターをコレクション サービスに追加する必要があるかがわかります。

class DbAdapterCollectionFactory
{
    public function __invoke(ServiceManager $serviceManager, $name, $requestedName)
    {
        $config  = $serviceManager->get('config');

        $options  = [];
        $adapters = [];

        if (isset($config['my_app']['services'][$requestedName])) {
            $options = $config['my_app']['services'][$requestedName]; 
        }

        if (! empty($options['adapters']) && is_array($options['adapters'])) {
            foreach ($options['adapters'] as $name => $adapter) {
                if (is_string($adapter) && $serviceManager->has($adapter)) {
                    $adapter = $serviceManager->get($adapter);
                }       
                if ($adapter instanceof \Zend\Db\Adapter) {
                    $adapters[$name] = $adapter;
                }
            }
        }

        return new DbAdapterCollection($adapters);
    }
}

この例の構成は次のようになります。

'service_manager' => [
    'factories' => [
        'DbAdapterCollection' => 'MyApp\\Db\\DbAdapterCollectionFactory'
    ],
],

// Example app config
'my_app' => [
    'services' => [
        'DbAdapterCollection' => [
            // adapterName => dbServiceName,
            'adapters' => [ 
                'adapter1' => 'MyApp\\Db\\Adapter1'
                'adapter2' => 'adapter2'
                'adapter3' => 'adapter3'
            ],
        ]
    ],
],

あとは、サービス マネージャーからサービスを呼び出すだけです。

$adapterCollection = $serviceManager->get('DbAdapterCollection');
if ($adapterCollection->has('adapter1')) {
    $adapter = $adapterCollection->get('adapter1');
} 
于 2016-11-12T13:51:32.307 に答える