Ejemplo n.º 1
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;
 }
Ejemplo n.º 2
0
 public function testServices()
 {
     $workingDir = __DIR__ . '/02';
     $factory = new ContainerFactory();
     $factory->setWorkingDirectory($workingDir);
     $factory->addConfig(__DIR__ . '/02/config.neon');
     $factory->addConfig(__DIR__ . '/02/config2.neon');
     $cont = new Container();
     $cont->addService('myService', new \StdClass());
     $cont->setParameter('myContainerKey', 'myContainerValue');
     $factory->addContainerToMerge($cont);
     $container = $factory->create();
     $this->assertTrue($container->hasService('myservice'));
     $this->assertFalse($container->hasService('myArrayWhichDoesNotExists'));
     $this->assertInstanceOf('DateTime', $container->getService('myservice'));
     $this->assertEquals(date('Y-m-d', strtotime('+ 1 day')), $container->getService('myservice')->format('Y-m-d'));
 }