zendframework / zend-mvc

Mvc component from Zend Framework
BSD 3-Clause "New" or "Revised" License
105 stars 89 forks source link

Instantiating only required services? #17

Closed SwissEngine closed 8 years ago

SwissEngine commented 9 years ago

Hello,

I am currently trying to create the lightest ZF2 CLI app to get the most performance out of it. When trying to use zend-mvc as my only dependency, I ran into the fact that I was missing MANY dependencies for a simple Application::init.

I dug a bit deeper and realized that the module manager https://github.com/zendframework/zend-mvc/blob/master/src/Service/ModuleManagerFactory.php is instantiating EVERY service whether or not it has been defined in the config or not.

I'm using a CLI app, I don't necessarily need form_elements to be instantiated, the same goes for hydrators, serializers, etc...

I'd like some feedback on this, could we make those optional if they aren't set in the config ? I don't reckon we need to have the services created if they are not used/needed. Maybe I don't have the whole picture here, could someone enlighten me ?

Once I have more info on this I can submit a pull request.

Thanks a lot !

bakura10 commented 9 years ago

This is currently a main problem of Zend\Mvc, that has a lot of hidden dependencies, and prevent from using it as a standalone. Whenever I write a module that rely on Zend\Mvc, I'm forced to include all the framework in test dependencies, otherwise there are often subtle bugs.

Zend\Mvc should likely undergo a deep refactoring to make it really independant. I'm not sure where Zend is going with the introduction of Zend\Stratigility and the new Zend\Expressive. I'm not sure if the plan is to provide a more modular and lightweight replacement of Zend\Mvc.

Maks3w commented 9 years ago

You may override the Composer's package definition in your root composer.json package[] however this only will effect if you plan redistribute your CLI tool as a PHAR file.

weierophinney commented 9 years ago

@SwissEngine I would argue that if you're doing a CLI application, you shouldn't use the MVC. While it seems easy, this is one of the many issues you run into.

We created zf-console to simplify creation of console applications. If you go that route, you can setup an alternate configuration for your console applications that has only the configuration necessary for the code you plan to invoke. I've found that code targeting zf-console tends to be far smaller, more focused, and have fewer dependencies.

SwissEngine commented 9 years ago

@weierophinney Well yes I could always go for Symfony\Console or zf-console but most of my apps are accessible from both the web and in CLI. This doesn't change the fact that IMO this component should undergo a deep refactoring, like bakura said :)

weierophinney commented 8 years ago

Addressed with the various refactors that culminated in #128, and to release with v3.