/** * Iterates through all services of a service provider, seeding the container. * * For each service it: * * - adds the specified factory, if the service does not already exist. * - adds a delegator factory otherwise. * * @param ServiceProvider $provider * @param ServiceManager $container * @return ServiceManager * @throws RuntimeException if any factory listed is not callable. */ private function marshalServiceProvider(ServiceProvider $provider, ServiceManager $container) { foreach ($provider->getServices() as $service => $factory) { $callable = $this->marshalCallable($provider, $factory, $service); if ($container->has($service)) { $container->addDelegator($service, $this->createDelegator($callable)); continue; } $container->setFactory($service, $this->createFactory($callable)); } return $container; }
public function testDelegatorFromCallback() { $realService = $this->getMock('stdClass', array(), array(), 'RealService'); $delegator = $this->getMock('stdClass', array(), array(), 'Delegator'); $delegatorFactoryCallback = function ($serviceManager, $cName, $rName, $callback) use($delegator) { $delegator->real = call_user_func($callback); return $delegator; }; $this->serviceManager->setFactory('foo-service', function () use($realService) { return $realService; }); $this->serviceManager->addDelegator('foo-service', $delegatorFactoryCallback); $service = $this->serviceManager->create('foo-service'); $this->assertSame($delegator, $service); $this->assertSame($realService, $service->real); }
public function testAutoGenerateAndEvaluateProxies() { $serviceManager = new ServiceManager(); $namespace = 'ZendTestProxy' . uniqid(); $serviceManager->setService('Config', array('lazy_services' => array('class_map' => array('foo' => __CLASS__), 'proxies_namespace' => $namespace))); $serviceManager->setFactory('foo-delegator', 'Zend\\ServiceManager\\Proxy\\LazyServiceFactoryFactory'); $serviceManager->setInvokableClass('foo', __CLASS__); $serviceManager->addDelegator('foo', 'foo-delegator'); /* @var $proxy self|\ProxyManager\Proxy\ValueHolderInterface|\ProxyManager\Proxy\LazyLoadingInterface */ $proxy = $serviceManager->create('foo'); $proxyClassName = get_class($proxy); $this->assertInstanceOf('ProxyManager\\Proxy\\LazyLoadingInterface', $proxy); $this->assertInstanceOf(__CLASS__, $proxy); $this->assertStringMatchesFormat($namespace . '\\__PM__\\ZendTest\\ServiceManager\\Proxy\\LazyServiceFactoryFactoryTest%s', $proxyClassName); $this->assertFileNotExists(sys_get_temp_dir() . '/' . str_replace('\\', '', $proxyClassName) . '.php'); $this->assertFalse($proxy->isProxyInitialized()); $this->assertEquals($this->invalidConfigProvider(), $proxy->invalidConfigProvider()); $this->assertTrue($proxy->isProxyInitialized()); }