How to Use Symfony Bundles in Nette Without Rewriting DI Logic
To be specific:
- In Nette you use Nette\DI package and make an extension.
- In Symfony, you have to use Symfony\DependencyInjection package and create a bundle.
So when you hear somebody saying:
"I saw that in Symfony bundle and want to use in Nette"
you know it won't be easy.
That's why you see "double" integrations for 3rd party packages like:
And so on. Let's ignore the syntax sugar that every programmer adds to his own integrations.
- lot of duplicated code (register event subscribers, commands and all the services from 3rd party package)
- lot of duplicated maintenance (fix compatibility with new version of 3rd party package)
- greater possibility of burnout syndrome
Well, if you look closer to Nette and Symfony DICs, you see they are quite similar. So answer is NO.
Both Nette and Symfony have 3 basic operations dealing with DIC:
Common for all packages.
Add setter, pass arguments, add reference to other service, collect services of certain type. Used less often, yet still very useful.
Usually workarounds, hacks, tweaks or performance tuning. Quite rare.
- Symfony: specific
Okay, okay... These are last few lines before the code, I promise.
Thanks to step 1. and 2. I could create an extension, that will take any Symfony bundle and register its services into the Nette application: TomasVotruba/NetteAdapterForSymfonyBundles
composer require symplify/nette-adapter-for-symfony-bundles
# app/config/config.neon extensions: symfonyBundles: Symplify\NetteAdapterForSymfonyBundles\DI\NetteAdapterForSymfonyBundlesExtension
# app/config/config.neon symfonyBundles: bundles: - League\Tactician\Bundle\TacticianBundle
And that's it!
For further use, just check Readme for Symplify/NetteAdapterForSymfonyBundles.
So next time you see a Symfony bundle you would like to use in Nette, stop thinking about writing brand new duplicated extension and try this bundle first. You might save yourself great amount of time :)
Missing some feature or found a bug? Let me know. I want to make this package suit your needs and work as best as possible.
❤️️ Do you like what I write about? Or do you hate it but enjoy discussion? 😠
You can support my writing by throwing a couple bucks at my Patreon