[Twig Component] How to determine if a component is installed from within a bundle #2098
-
I have a ux component that displays a datatable/grid with tabular data (https://github.com/survos/SurvosSimpleDatatablesBundle/) It has a stimulus component and some css, it's an easy way to convert a set of data to a table. I have another bundle that interacts with a service (BunnyCDN) and gets the data. And it has a simple admin page where it'd like to display the files on the CDN. But I'd like to make it optional -- the admin page is a bonus but may not be relevant for many applications, so I don't want to require the datatable bundle, but make it optional. BUT if someone does go to the controller and says "list all the zones", it can show the message saying "Hey, run composer req survos/simple-datatables-bundle" to access this page. But I can't quite tell how to determine if the bundle/component is installed. I don't want to check at the bunny bundle level (because it's not necessary to include the datatables if you're not using the admin controller), but I imagine I want to inject some service into the controller, then I can check if that service exists and throw the error. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I figured it out. In the bundle... public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
// get all bundles
$bundles = $builder->getParameter('kernel.bundles');
$hasSimpleDatatables = in_array(SurvosSimpleDatatablesBundle::class, array_values($bundles)); It is somewhat confusing that the Container is sometimes called $container and other times called $builder, depending on the context: // src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
public function prepend(ContainerBuilder $container): void
{
// get all bundles
$bundles = $container->getParameter('kernel.bundles');
// determine if AcmeGoodbyeBundle is registered``` |
Beta Was this translation helpful? Give feedback.
I figured it out. In the bundle...
It is somewhat confusing that the Container is sometimes called $container and other times called $builder, depending on the context: