Exemplo n.º 1
0
 protected function autowire(IBuild $build, Container $container)
 {
     foreach ($this->getAutowiredProperties($build) as $property => $service) {
         if (!$container->hasService($service)) {
             throw new Exception("Cannot found service '{$service}' to inject into " . get_class($build) . "::{$property}.");
         }
         $build->{$property} = $container->getService($service);
     }
 }
Exemplo n.º 2
0
 public function create()
 {
     if (!class_exists('Nette\\Neon\\Decoder', TRUE)) {
         throw new Exception("Neon is not loaded.");
     }
     if (empty($this->configs)) {
         throw new ContainerFactoryException("No config added.");
     }
     $config = ['parameters' => ['workingDirectory' => $this->workingDirectory]];
     if ($this->containersToMerge !== NULL) {
         foreach ($this->containersToMerge as $containerToMerge) {
             foreach ($containerToMerge->getParameters() as $k => $v) {
                 $config['parameters'][$k] = $v;
             }
             foreach ($containerToMerge->getServices() as $k => $v) {
                 $config['services'][$k] = $v;
             }
         }
     }
     $configs = $this->resolveFiles($this->configs);
     $config = $this->readConfigs($configs, $config);
     $config = $this->parseValues($config);
     // BC break check
     $mainSections = ['includes', 'class', 'parameters', 'services'];
     foreach ($config as $key => $val) {
         if (!in_array($key, $mainSections)) {
             throw new NotSupportedException("Since version 2.0 are supported main only these sections: " . implode(", ", $mainSections) . ". Section '{$key}' found. Move your variables into parameters section.");
         }
     }
     $container = new Container();
     $container->setClass($config['class']);
     if (isset($config['parameters'])) {
         $container->setParameters($config['parameters']);
     }
     if (isset($config['services'])) {
         foreach ($config['services'] as $name => $config) {
             if (!is_array($config)) {
                 $container->addService($name, $config);
                 // is directly service object from merged container
                 continue;
             }
             if (!isset($config['class'])) {
                 throw new ContainerFactoryException("Service '{$name}' does not have defined class.");
             }
             $class = $config['class'];
             $arguments = [];
             if ($config['class'] instanceof \Nette\Neon\Entity) {
                 $class = $config['class']->value;
                 $arguments = $config['class']->attributes;
             }
             $reflectionClass = new \ReflectionClass($class);
             $service = $reflectionClass->newInstanceArgs($arguments);
             if (isset($config['setup'])) {
                 foreach ($config['setup'] as $neonEntity) {
                     if (!method_exists($service, $neonEntity->value)) {
                         throw new ContainerFactoryException("Class {$class} does not have method {$neonEntity->value}().");
                     }
                     call_user_func_array(array($service, $neonEntity->value), $neonEntity->attributes);
                 }
             }
             $container->addService($name, $service);
         }
     }
     return $container;
 }
Exemplo n.º 3
0
 /**
  * @expectedException \Genesis\MemberAccessException
  */
 public function testGetServiceFail()
 {
     $container = new Container();
     $container->getService('unknown');
 }