symfony使用自由对象

I want to build several objects in symfony, which will do specific work. For example one object Parser will have a public method parse($text) and will return parsed text. All other methods are private to manage the parsing.

Within this parsing methods, I want to exclude some work to other objects. For example Clean and Highlight.

I want to include Parser as service. But do I also need to create services of the other Objects to include them? I do not plan to use them in controllers. Within the Parser object I can call them just with

new Highlight();

But now I get a problem, because I want to use the Doctrine entity manager within Highlight. Within a service I would inject it via the service arguments, but because Highlight is no service that is not possible.

How should I inject Doctrine into this "free" Object Highlight. And generally, is it ok to use objects in like this in symfony?

No need to create a service for each object called from Parser unless you will independently need these sub-objects (Clean, Highlight). If you need to pass other things to your sub-objects, just pass them from the calling object (Parser). Just inject the entity manager into Parser, then pass that on to the sub-objects.

From Parser:

new Highlight($em);

Alternatively, you could use a variation of this by defining an abstract service where common dependencies are defined in the parent (abstract) service and then extend that service in dependent objects. See docs here for details.