title: zf2 view helper factory / service locator parameters
body: <p>I have a view helper that acts as a factory by returning an entity-specific renderer.</p>
  
  <p>I would like the factory to implement the FactoryInterface and MutableCreationOptionsInterface, so i can return different renderers depending on the type of object passed to it, eg:</p>
  
  <pre><code>$serviceLocator-&gt;get('entityRenderer', ['entity' =&gt; $user]); // returns UserRenderer
  $serviceLocator-&gt;get('entityRenderer', ['entity' =&gt; $admin]); // returns AdminRenderer
  $serviceLocator-&gt;get('entityRenderer'); // returns DefaultRenderer
  </code></pre>
  
  <p>However, there is no access to the servicelocator from within a view, and the factory view helper i have created is called using it's __invoke method. This means the type check is occuring here and returning the specific renderer without using the service manager, which is not desirable. eg</p>
  
  <pre><code>class EntityRendererFactory extends AbstractHelper{
      public function __invoke(Entity $entity){
          if($entity instanceof User){
              $renderer =  new UserRenderer($entity);
              $renderer-&gt;setView($this-&gt;view);
          }
  
          if($entity instanceof Admin){
              $renderer = new AdminRenderer($entity);
              $renderer-&gt;setView($this-&gt;view);
          }
  
          if($renderer){
              return $renderer;
          }
      }
  }
  </code></pre>
  
  <p>Note how this "factory" is having to extend AbstractHelper (view) simply just to pass on the instance of the current view. </p>
  
  <p>My "ideal" would be something like this (proof of concept, not working code):</p>
  
  <pre><code>use Zend\ServiceManager\FactoryInterface;
  use Zend\ServiceManager\MutableCreationOptionsInterface;
  class EntityRendererFactory implements FactoryInterface, MutableCreationOptionsInterface{
          protected $options = [];
          protected $renderers = [];
  
          public function createService(ServiceLocatorInterface $serviceLocator){
              $this-&gt;addRenderer($serviceLocator-&gt;get('ViewHelperManager')-&gt;get('UserRenderer'), User::class);
              $this-&gt;addRenderer($serviceLocator-&gt;get('ViewHelperManager')-&gt;get('AdminRenderer'), Admin::class);
              $this-&gt;addRenderer($serviceLocator-&gt;get('ViewHelperManager')-&gt;get('DefaultRenderer'), 'default');
  
              if(!array_key_exists('entity', $this-&gt;options)){
                  return $this-&gt;getRenderer('default');
              }
  
              $entity = $this-&gt;options['entity'];
  
              foreach($this-&gt;getRenderers() as $renderer){
                  if($renderer-&gt;canRender($entity)){
                      return $renderer;
                  }
              }
  
              //Alternatively, more specific hard-coding interface type check
              if($entity instanceof User){
                  return $serviceLocator-&gt;get('ViewHelperManager')-&gt;get('UserRenderer');
              }
              //etc.
          }
  
          public function setCreationOptions(array $options){
              $this-&gt;options = $options;
          }
      }
  </code></pre>
  
  <p>...but with the above demonstration, i would be unsure how to call it from within the view (as view helpers are typically called from their __invoke method and not from the service manager)?</p>
  
  <p>(With an eye to migrating to ZF3, i do not want to use the ServiceLocatorAwareInterface).</p>