public function configureServiceManager(\Zend\ServiceManager\ServiceManager $serviceManager)
 {
     $serviceManager->setFactory('A', function () {
         return new A();
     });
     $serviceManager->setShared('A', true);
     $serviceManager->setFactory('B', function ($serviceManager) {
         return new B($serviceManager->get('A'));
     });
     $serviceManager->setShared('B', false);
 }
 public function configureServiceManager(\Zend\ServiceManager\ServiceManager $serviceManager)
 {
     $serviceManager->setFactory('A', function () {
         return new A();
     });
     $serviceManager->setShared('A', false);
     $serviceManager->setFactory('B', function ($serviceManager) {
         return new B($serviceManager->get('A'));
     });
     $serviceManager->setShared('B', false);
     $serviceManager->setFactory('C', function ($serviceManager) {
         return new C($serviceManager->get('B'));
     });
     $serviceManager->setShared('C', false);
     $serviceManager->setFactory('D', function ($serviceManager) {
         return new D($serviceManager->get('C'));
     });
     $serviceManager->setShared('D', false);
     $serviceManager->setFactory('E', function ($serviceManager) {
         return new E($serviceManager->get('D'));
     });
     $serviceManager->setShared('E', false);
     $serviceManager->setFactory('F', function ($serviceManager) {
         return new F($serviceManager->get('E'));
     });
     $serviceManager->setShared('F', false);
     $serviceManager->setFactory('G', function ($serviceManager) {
         return new G($serviceManager->get('F'));
     });
     $serviceManager->setShared('G', false);
     $serviceManager->setFactory('H', function ($serviceManager) {
         return new H($serviceManager->get('G'));
     });
     $serviceManager->setShared('H', false);
     $serviceManager->setFactory('I', function ($serviceManager) {
         return new I($serviceManager->get('H'));
     });
     $serviceManager->setShared('I', false);
     $serviceManager->setFactory('J', function ($serviceManager) {
         return new J($serviceManager->get('I'));
     });
     $serviceManager->setShared('J', false);
 }
Exemplo n.º 3
0
 /**
  * @covers Zend\ServiceManager\ServiceManager::get
  */
 public function testGetUsesIndivualSharedSettingWhenSetAndDeviatesFromShareByDefaultSetting()
 {
     $this->serviceManager->setAllowOverride(true);
     $this->serviceManager->setShareByDefault(false);
     $this->serviceManager->setInvokableClass('foo', 'ZendTest\\ServiceManager\\TestAsset\\Foo');
     $this->serviceManager->setShared('foo', true);
     $this->assertSame($this->serviceManager->get('foo'), $this->serviceManager->get('foo'));
     $this->serviceManager->setShareByDefault(true);
     $this->serviceManager->setInvokableClass('foo', 'ZendTest\\ServiceManager\\TestAsset\\Foo');
     $this->serviceManager->setShared('foo', false);
     $this->assertNotSame($this->serviceManager->get('foo'), $this->serviceManager->get('foo'));
 }
Exemplo n.º 4
0
 /**
  * Registers a factory wrapping a Slim factory into a ZF2 factory
  *
  * @param $key
  * @param callable $callable
  * @param bool $shared
  */
 protected function registerFactory($key, callable $callable, $shared = true)
 {
     $this->sm->setFactory($key, new CallbackWrapper($this, $callable));
     $this->sm->setShared($key, $shared);
 }
Exemplo n.º 5
0
 /**
  * @covers Zend\ServiceManager\ServiceManager::setShared
  */
 public function testSetSharedThrowsExceptionOnUnregisteredService()
 {
     $this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
     $this->serviceManager->setShared('foo', true);
 }
Exemplo n.º 6
0
 /**
  * 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);
 }
Exemplo n.º 7
0
 /**
  * @param $config
  * @param array $data
  * @return array|object|ViewModel
  */
 public function build($config, array $data = array())
 {
     $allOptions = $this->config->getOptions();
     $i = 0;
     $queue = new \SplQueue();
     $queue->enqueue($config);
     while ($queue->count() > 0) {
         $options = $queue->dequeue();
         $options = $this->config->applyInheritance($options);
         if (isset($options['viewModel'])) {
             $this->serviceLocator->setShared($options['viewModel'], false);
             $viewModel = $this->serviceLocator->get($options['viewModel']);
         } else {
             $viewModel = new ZendViewModel();
         }
         if ($i == 0) {
             $rootViewModel = $viewModel;
         }
         if (isset($options['template'])) {
             $viewModel->setTemplate($options['template']);
         }
         if (isset($options['capture'])) {
             $viewModel->setCaptureTo($options['capture']);
         } elseif (isset($options['id'])) {
             $viewModel->setCaptureTo($options['id']);
         }
         if (isset($options['data']['static'])) {
             $viewModel->setVariables($options['data']['static']);
         }
         if (isset($options['data']['fromGlobal'])) {
             $globalVar = $options['data']['fromGlobal'];
             if (is_array($globalVar)) {
                 foreach ($globalVar as $globalVarName => $viewVarName) {
                     $globalVarValue = $this->getVarValue($globalVarName, $data);
                     $viewModel->setVariable($viewVarName, $globalVarValue);
                 }
             } else {
                 $globalVarValue = $this->getVarValue($globalVar, $data);
                 $viewModel->setVariable($globalVar, $globalVarValue);
             }
         }
         if (isset($options['parent'])) {
             /** @var ViewModel $parent */
             $parent = $options['parent'];
             $parent->addChild($viewModel, $viewModel->captureTo(), true);
             if (isset($options['data']['fromParent'])) {
                 $varFromParent = $options['data']['fromParent'];
                 $parentVars = $parent->getVariables();
                 if (is_array($varFromParent)) {
                     foreach ($varFromParent as $varFromParentName => $viewVarName) {
                         $fromParentVal = $this->getVarValue($varFromParentName, $parentVars);
                         if ($fromParentVal === null) {
                             $fromParentVal = $parent->getVariable($varFromParentName);
                         }
                         if (is_array($viewVarName)) {
                             $dataFromParent = [];
                             foreach ($viewVarName as $varName) {
                                 $dataFromParent[$varName] = $fromParentVal;
                             }
                         } else {
                             $dataFromParent = [$viewVarName => $fromParentVal];
                         }
                         $viewModel->setVariables($dataFromParent);
                     }
                 } else {
                     $viewVarName = $options['data']['fromParent'];
                     $fromParentVal = $this->getVarValue($viewVarName, $parentVars);
                     if ($fromParentVal === null) {
                         $fromParentVal = $parent->getVariable($viewVarName);
                     }
                     $viewModel->setVariables([$viewVarName => $fromParentVal]);
                 }
             }
         }
         if (!empty($options['children'])) {
             foreach ($options['children'] as $childId => $child) {
                 if (is_string($child)) {
                     $childId = $child;
                     $child = $allOptions[$child];
                 }
                 if (isset($options['childrenDynamicLists'][$childId])) {
                     continue;
                 }
                 $child['id'] = $childId;
                 $child['parent'] = $viewModel;
                 $queue->enqueue($child);
             }
         }
         if (isset($options['childrenDynamicLists'])) {
             foreach ($options['childrenDynamicLists'] as $childName => $listName) {
                 $list = $viewModel->getVariable($listName);
                 if ($list === null) {
                     throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . View does not contain variable '{$listName}'.");
                 }
                 if (!is_array($list) && !$list instanceof \Traversable) {
                     throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . List '{$listName}' must be array " . gettype($list) . " given.");
                 }
                 if (array_key_exists($childName, $options['children'])) {
                     $child = $options['children'][$childName];
                 } else {
                     if (in_array($childName, $options['children'])) {
                         $child = $allOptions[$childName];
                     } else {
                         throw new \UnexpectedValueException("Cannot build children list of '{$childName}' by '{$listName}' list . Child '{$childName}' not found");
                     }
                 }
                 $child['id'] = $childName;
                 $child['parent'] = $viewModel;
                 if (isset($child['data']['fromParent'])) {
                     $varFromParent = $child['data']['fromParent'];
                 }
                 foreach ($list as $entry) {
                     if (isset($varFromParent)) {
                         if (is_array($varFromParent)) {
                             foreach ($varFromParent as $varFromParentName => $viewVarName) {
                                 $dataForChild = [$viewVarName => $entry];
                             }
                         } else {
                             $dataForChild = [$varFromParent => $entry];
                         }
                         if (!isset($child['data']['static'])) {
                             $child['data']['static'] = [];
                         }
                         $child['data']['static'] = array_merge($child['data']['static'], $dataForChild);
                         unset($child['data']['fromParent']);
                     }
                     $queue->enqueue($child);
                 }
             }
         }
         $i++;
     }
     return $rootViewModel;
 }