We are currently in the process of updating our company internal framework okapi to use several of the symfony components. The service container component seems to be the one that poses the biggest challenges since there are several conceptual issues to address. As such I am looking for other implementations of the service container to see how things work there. I have posted this on the symfony-components mailinglist a while back, but got no reply (probably because the list wasn't really announced properly), so I am reposting this in my blog to reach a larger audience, but also to bring some attention to the components mailinglist. I guess most of my concerns are not really all that much about dependency injection but the service container approach that symfony provides to make dependency injection use a bit more convenient. We have managed to somewhat answer our questions, but some still remain open: Where to keep the SC instance? How to deal with shared instances that need to be reloaded? How to deal with Service container caching and auto regeneration?
1) Where to keep the SC instance?
First up we were unsure how to deal with optional dependencies and also how to deal with services that load other services. So the first approach that came to mind is that we basically inject the service container with everything. But that seemed ugly and magical. Upon further consideration it became clear that this wasnt needed at all. We now make an instance of the SC container inside the front controller and we pass it into the controller and store it there. Thats all.
There is now a syntax for optional dependencies (though still missing from the non 5.3 version last I checked). If a service really needs to load additional services (say for example various modules who's names are pulled out of the database during the request), then that service should simply create a new service container instance.
2) How to deal with shared instances that need to be reloaded
We have faced this situation in our unit testing framework. Since we are faking a request we need to effectively be able to reload a clean version with new parameters. Obviously we could add some method to do this cleanup, but it seems error prone and adding this feature seems trivial.
3) How to deal with Service container caching and auto regeneration?
We have run into a few issues when caching the service container config into a PHP file. For one there seem to be some issues in regards to handling of parameters. There is also the issue that if you want to automatically want to update the cache when underlying files have changed, that there is no easy way to find out which files were read (this is how symfony 1.3 works in development mode). For example an extension (aka plugin) could define a few services. These config files are bundled with the extension and therefore do not reside in the global configuration directory and imported inside the yaml files via the "imports" directive. Therefore its no sufficient to just scan that directory for the last updated file. Therefore we asked to get the list of files read during a call to load() so that we can cache this information for later reference when determining if the cached file needs to be updated.