[英]How to get a list of services programmatically in Symfony?
I want to have a list of services and class names in my web application.我想在我的 Web 应用程序中有一个服务和类名的列表。 I can use this command in console:
我可以在控制台中使用这个命令:
php bin/console debug:container
And I get something like this:我得到这样的东西:
Symfony Container Public Services
=================================
-------------------------------------------------------------------- --------------------------------------------------------------------------------------------
Service ID Class name
-------------------------------------------------------------------- --------------------------------------------------------------------------------------------
annotation_reader Doctrine\Common\Annotations\CachedReader
app.annotations.softdelete.driver AppBundle\Doctrine\SoftDelete\Mapping\Driver\Annotation
app.annotations.translate.driver AppBundle\Doctrine\Mapping\Driver\TranslateDriver
app.be_auth_controller.listener AppBundle\EventListener\BeAuthControllerListener
I want to have this information on a web page using Symfony 3.我想在使用 Symfony 3 的网页上获得这些信息。
I created a service and I used:我创建了一个服务并使用了:
$this->container->getServiceIds();
which returns something like:它返回如下内容:
[
0 => "service_container"
1 => "annotation_reader"
2 => "annotations.reader"
3 => "app.annotations.softdelete.driver"
4 => "app.annotations.translate.driver"
...
]
I don't know, how to get the class names.我不知道,如何获得类名。
In any cases works this:在任何情况下都可以这样工作:
get_class($this->container->get($this->container->getServiceIds()[1]))
But in some other cases it throws different exceptions.但在其他一些情况下,它会抛出不同的异常。
Your attempt with get_class
is what came to mind as I was reading it, but whatever errors you are getting will come from improper fetching of those services.我在阅读它时想到了您对
get_class
尝试,但是无论您遇到什么错误,都将来自对这些服务的不当获取。 After all when you call $container->get(...)
, its at that moment instantiating those classes.毕竟,当您调用
$container->get(...)
,它正在实例化这些类。
To be honest the output you are looking to replicate can be reproduced based on the method used by that command.老实说,您要复制的输出可以根据该命令使用的方法进行复制。
https://github.com/symfony/framework-bundle/blob/master/Command/ContainerDebugCommand.php https://github.com/symfony/framework-bundle/blob/master/Command/ContainerDebugCommand.php
You'll just need to adapt it to work for you.您只需要调整它以适合您。
To get full definition of given service you can use ContainerBuilder and Symfony cache file.要获得给定服务的完整定义,您可以使用 ContainerBuilder 和 Symfony 缓存文件。
first create instance of ContainerBuilder:首先创建ContainerBuilder的实例:
$container = new ContainerBuilder();
then load cache file:然后加载缓存文件:
$cachedFile = $this->container->getParameter('debug.container.dump');
$loader = new XmlFileLoader($container, new FileLocator());
$loader->load($cachedFile);
now you can get full definition of your service like this:现在您可以像这样获得服务的完整定义:
$definition = $container->getDefinition('service_name')
$definition->getClass();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.