Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created April 9, 2012 21:27
Show Gist options
  • Save weierophinney/2346660 to your computer and use it in GitHub Desktop.
Save weierophinney/2346660 to your computer and use it in GitHub Desktop.
Ideas for the zf2 instance manager
<?php
// Use case 1:
// Providing additional constructor arguments
$im->composeService('foo', $myservice);
class MyService extends ComposedServiceFactory
{
public function __invoke($im, $name)
{
// Add constructor args:
// Assume that annotation manager is an optional constructor argument
// usually
$annotationManager = $im->get('AnnotationManager');
$serviceFactory = $im->getFactory($name);
$serviceFactory->setAnnotationManager($annotationManager);
// Return the factory, indicating we have not yet instantiated the service
return $serviceFactory;
}
}
// Use case 2: manipulating the instance after it's been retrieved
$im->processService('AggregateResolver', $yourService);
class YourService implements ServiceFactoryProcessor
{
public function __invoke($im, $name)
{
// Let's add a resolver to the stack
$myResolver = new MyResolver();
// which means we need the instance
$service = $im->get($name);
$service->add($myResolver);
// Return the service, indicating we're working on the instance
return $service;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment