gpt4 book ai didi

symfony - 我应该从 Symfony 2 的服务容器中删除未使用的服务吗?

转载 作者:行者123 更新时间:2023-12-02 14:07:06 25 4
gpt4 key购买 nike

我不知道这是否有一些好处:是否应该从服务容器中删除未使用的服务?

例如,在我的 bundle 配置中,我可以禁用 lang 选项提供程序:

my_bundle:
providers:
lang:
enabled: false

启用 lang 提供程序时,应添加 provider 标记。该服务在 services.xml 文件中定义,并通过扩展加载:

<service id="my_bundle.provider.lang" class="My\Provider\LangOptionsProvider">

那么,删除这个定义是个好主意,为什么?

public function load(array $configs, ContainerBuilder $container)
{
// ...

$loader->load('services.xml'); // Loads "my_bundle.provider.lang"

if($config['providers']['lang']['enabled']) {
// lang provider is enabled, add the tag
$container->getDefinition('my_bundle.provider.lang')->addTag('provider');
}
else { // Is this really needed?
// Remove lang provider definition (or just the tag?!)
$container->removeDefinition('my_bundle.provider.lang');
}
}

最佳答案

开销很小,因为实例化是在请求服务时发生的,因此是否删除它并不重要。

When you ask for the my_mailer service from the container, the container constructs the object and returns it. This is another major advantage of using the service container. Namely, a service is never constructed until it's needed. If you define a service and never use it on a request, the service is never created. This saves memory and increases the speed of your application. This also means that there's very little or no performance hit for defining lots of services. Services that are never used are never constructed.

http://symfony.com/doc/current/book/service_container.html

关于symfony - 我应该从 Symfony 2 的服务容器中删除未使用的服务吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14160043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com