根据捆绑扩展类中的配置更改服务(ClientManager) [英] Alter service (ClientManager) based on configuration inside bundle extension class
问题描述
我有一个名为ApiBundle"的包.在这个包中,我有一个ServiceManager"类,这个类负责检索特定的 Service 对象.那些Service对象需要根据一些配置创建,所以在我的bundle扩展类中的这段代码之后:
I have a bundle named: "ApiBundle". In this bundle I have the class "ServiceManager", this class is responsible for retrieving a specific Service object. Those Service objects needs to be created based on some configuration, so after this piece of code in my bundle extension class:
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
// Create Service objects...
我在处理完配置后立即创建那些 Service 对象,如下所示:
I create those Service objects right after I have processed the configuration, something like this:
foreach ($services as $name => $service) {
$service = new Service();
$service->setName($name);
$manager = $container->get($this->getAlias() . '.service_manager');
$manager->add($service);
}
不幸的是,这不起作用,可能是因为容器尚未编译.所以我尝试通过以下方式添加那些 Service 对象:
Unfortunately, this does not work, probably because the container isn't compiled yet. So I tried to add those Service objects the following way:
$manager = $container->getDefinition($this->getAlias() . '.service_manager');
$manager->addMethodCall('add', array($service));
但同样,这会引发以下异常:RuntimeException:如果参数是对象或资源,则无法转储服务容器.
But again, this throws the following exception: RuntimeException: Unable to dump a service container if a parameter is an object or a resource.
我似乎无法掌握如何正确使用服务容器.有人知道如何将那些 Service 对象添加到包扩展类中的 ServiceManager(这是一个服务)?
I can't seem to get a grasp on how to use the service container correctly. Does someone knows how I can add those Service objects to the ServiceManager (which is a service) inside the bundle extension class?
bundle 的配置如下所示:
This is how the configuration of the bundle looks like:
api_client:
services:
some_api:
endpoint: http://api.yahoo.com
some_other_api:
endpoint: http://api.google.com
每个服务"都是一个单独的服务对象.
Every 'service' will be a seperate Service object.
我希望我解释得足够好,如果我的英语不正确,我很抱歉.
I hope I explained it well enough, my apologies if my english is incorrect.
史蒂芬
编辑
我想我可能已经解决了这个问题,我制作了一个编译器通行证来使用以下内容操作容器:
I think I may have solved the problem, I made a Compiler Pass to manipulate the container there with the following:
public function process(ContainerBuilder $container)
{
$services = $container->getParameter('mango_api.services');
foreach ($services as $name => $service) {
$clientManager = $container->getDefinition('mango_api.client_manager');
$client = new Definition('Mango\Bundle\ApiBundle\Client\Client', array($name, 'client', 'secret'));
$container->setDefinition('mango_api.client.' .$name, $client);
$clientManager->addMethodCall('add', array($client));
}
}
这样合适吗?
推荐答案
我想我可能已经解决了这个问题,我制作了一个 Compiler Pass 来操作那里的容器,如下所示:
I think I may have solved the problem, I made a Compiler Pass to manipulate the container there with the following:
public function process(ContainerBuilder $container)
{
$services = $container->getParameter('mango_api.services');
foreach ($services as $name => $service) {
$clientManager = $container->getDefinition('mango_api.client_manager');
$client = new Definition('Mango\Bundle\ApiBundle\Client\Client', array($name, 'client', 'secret'));
$client->setPublic(false);
$container->setDefinition('mango_api.client.' .$name, $client);
$clientManager->addMethodCall('add', array($client));
}
}
这篇关于根据捆绑扩展类中的配置更改服务(ClientManager)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!