How can I expand the package more than once?

In my Symfony2 application, I have a basic package called AnimalsBundle() with a very simple entity.

I can successfully extend this package by creating a new MammalsBundle() package through Package Inheritance . However, it is not possible to register yet another InsectsBundle() package, which also extends AnimalsBundle() . Whenever I try to do this, symfony throws

[LogicException] The AnimalsTextBundle package is directly extended by two bundles MammalsBundle and InsectsBundle.

So out of the box, this is clearly not allowed. First of all, I'm not quite sure why this is not allowed and - most importantly - how can I solve this?

+4
source share
3 answers

I know that more than a year has passed, but I just stumbled upon your question, and the answer may be useful to someone in any case.

Symfony does not allow a package to be expanded directly by more than one package, simply because if two packages override the same files, it is not possible to determine which package should be used. However, you can achieve what you want by doing the following:

AnimalsBundle <| ---- MammalsBundle <| ----- InsectsBundle

Thus, InsectsBundle indirectly has an AnimalBundle as a parent and can override files from it.

+2
source

I know this is the time of the log since the question was asked, but for those who need an answer, I suggest the following:

1) Try to avoid inheritance overlay, unless you are 100% sure what you need.

2). In this example, the best setup would look something like this: you have a CreatureBundle , which consists mainly of abstract classes and interfaces. To make each package of descendants depends on the CreatureBundle and implement each Creature special code with these abstract classes and interfaces in the CreatureBundle .

Based on personal experience, I can say that dependency management is much easier than managing inheritance issues if something goes wrong. If you ever need to change the ancestral binding logic, you will save time by not digging through the inherited code and not changing the same logic in each descendant stream.

Edit: Although my suggestions may lead to a closer connection and are largely contrary to the latest Symfony best practice guide (which says that “the only package for each application” is the best practice), in the end you will understand that this approach will end up simplifies code maintenance.

+1
source

I can’t come up with a use case where you might need to do this. Bundles are designed for nearly identical applications. You have a dependency injection container at your disposal if you need resources from another package.

Perhaps you should think about your project structure.

-one
source

Source: https://habr.com/ru/post/1394355/


All Articles