public function testCreateWithInitializerObject() { $this->serviceManager->addInitializer(new TestAsset\FooInitializer(array('foo' => 'bar'))); $this->serviceManager->setFactory('foo', function () { return new \stdClass(); }); $obj = $this->serviceManager->get('foo'); $this->assertEquals('bar', $obj->foo); }
public function createServiceManager(array $config) { // create ServiceManager $serviceManager = new ServiceManager(); // set an initializer for ServiceLocatorAwareInterface $serviceManager->addInitializer(function ($instance, ServiceLocatorInterface $serviceLocator) { if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($serviceLocator); } }); // add $serviceManger to the config keys so we can configure it $this->configKeys = ['service_manager' => $serviceManager] + $this->configKeys; // add the pluginManagers to the ServiceManager foreach ($this->pluginManagers as $key => $className) { $serviceManager->setInvokableClass($key, $className); } // add Zend\Form's view helpers to the ViewHelperManager $viewHelperManager = $serviceManager->get('ViewHelperManager'); $vhConfig = new \Zend\Form\View\HelperConfig(); $vhConfig->configureServiceManager($viewHelperManager); // apply configuration $this->applyConfig($serviceManager, $config); return $serviceManager; }
/** * Configure the provided service manager instance with the configuration * in this class. * * In addition to using each of the internal properties to configure the * service manager, also adds an initializer to inject ServiceManagerAware * and ServiceLocatorAware classes with the service manager. * * @param ServiceManager $serviceManager */ public function configureServiceManager(ServiceManager $serviceManager) { foreach ($this->invokables as $name => $class) { $serviceManager->setInvokableClass($name, $class); } foreach ($this->factories as $name => $factoryClass) { $serviceManager->setFactory($name, $factoryClass); } foreach ($this->abstractFactories as $factoryClass) { $serviceManager->addAbstractFactory($factoryClass); } foreach ($this->aliases as $name => $service) { $serviceManager->setAlias($name, $service); } foreach ($this->shared as $name => $value) { $serviceManager->setShared($name, $value); } $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof EventManagerAwareInterface) { if ($instance->getEventManager() instanceof EventManagerInterface) { $instance->getEventManager()->setSharedManager($serviceManager->get('SharedEventManager')); } else { $instance->setEventManager($serviceManager->get('EventManager')); } } }); $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof ServiceManagerAwareInterface) { $instance->setServiceManager($serviceManager); } }); $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($serviceManager); } }); $serviceManager->setService('ServiceManager', $serviceManager); }
public function testShouldRaiseExceptionIfInitializerClassIsNotAnInitializerInterfaceImplementation() { $this->setExpectedException('Zend\ServiceManager\Exception\InvalidArgumentException'); $result = $this->serviceManager->addInitializer(get_class($this)); }
/** * Creates a service manager instnace * * @param array $config * @return \Zend\ServiceManager\ServiceManager */ public static function createServiceManager(array $config = []) { $serviceManager = new ServiceManager(new Config($config)); $serviceManager->addInitializer(function ($instance, ServiceLocatorInterface $serviceLocator) { if ($instance instanceof ModelManagerAwareInterface) { $instance->setModelManager($serviceLocator->get('modelmanager')); } }); $serviceManager->addInitializer(function ($instance, ServiceLocatorInterface $serviceLocator) { if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($serviceLocator); } }); return $serviceManager; }
public static function createServiceManager($config = []) { $serviceManager = new ServiceManager(new Config($config)); $serviceManager->addInitializer(function ($instance, ServiceLocatorInterface $serviceLocator) { if ($instance instanceof DocumentManagerAwareInterface) { $instance->setDocumentManager($serviceLocator->get($serviceLocator->get('manifest')['document_manager'])); } }); $serviceManager->addInitializer(function ($instance, ServiceLocatorInterface $serviceLocator) { if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($serviceLocator); } }); return $serviceManager; }