Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

[ZF3] [RFC] Removal of contextual decision-making in factories #6081

Closed
Ocramius opened this issue Apr 2, 2014 · 2 comments
Closed

[ZF3] [RFC] Removal of contextual decision-making in factories #6081

Ocramius opened this issue Apr 2, 2014 · 2 comments

Comments

@Ocramius
Copy link
Member

Ocramius commented Apr 2, 2014

Strictly related to #5599

The Problem

As of now, many factories apply decisions based on the context or parameters (not the configuration) of the application.

Some examples are the Zend\Mvc\Service\ViewManagerFactory or the Zend\Mvc\Service\RequestFactory, which use the Console::isConsole() to decide what to build.
In these particular cases, the problem is also made worse by the fact that we're using the global scope to modify how our applications work, but in many apps I also see things like following:

class FooFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $sl)
    {
        return new Something($sl->get('Application')->getMvcEvent());
    }
}

The problem in this particular case is that Something returned by this factory can't be reused after an application dispatch, as it is bound to one particular run of the app itself.

Suggested solution

Remove any contextual switches (in factories) that aren't related to configuration. As an example taken from the factories that I linked above, we can make the Request object non-shared, and the ViewManager could decide whether to act as Console or HTTP view manager at runtime.

Banning this approach from new code (as a code-style rule or such) would also be suggested.

Affected factories

  • Zend\Mvc\Service\ConsoleViewManagerFactory
  • Zend\Mvc\Service\RequestFactory
  • Zend\Mvc\Service\ResponseFactory
  • Zend\Mvc\Service\RouterFactory
  • Zend\Mvc\Service\ViewManagerFactory

Not sure about sessions right now, but we could also get rid of internal PHP sessions in order to be able to use different sessions across multiple requests, but that's a different story.

@devosc
Copy link
Contributor

devosc commented Apr 2, 2014

Can you assign both of them to the 3.0 Milestone - it would be easier to find.

I'd suggest generating two separate configuration files for each context, even if it is just to swap out those contextual services (plus there is routes).

@Ocramius Ocramius added this to the 3.0.0 milestone Apr 2, 2014
@GeeH
Copy link

GeeH commented Jun 27, 2016

This issue has been closed as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html

@GeeH GeeH closed this as completed Jun 27, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants