Esempio n. 1
0
 protected function buildContainer($class, $file)
 {
     $parameterBag = new ParameterBag($this->getKernelParameters());
     $container = new ContainerBuilder($parameterBag);
     foreach ($this->bundles as $bundle) {
         if (null !== ($c = $bundle->buildContainer($parameterBag))) {
             $container->merge($c);
         }
         if ($this->debug) {
             $container->addObjectResource($bundle);
         }
     }
     if (null !== ($cont = $this->registerContainerConfiguration($this->getContainerLoader($container)))) {
         $container->merge($cont);
     }
     $container->freeze();
     foreach (array('cache', 'logs') as $name) {
         $dir = $container->getParameter(sprintf('kernel.%s_dir', $name));
         if (!is_dir($dir)) {
             if (false === @mkdir($dir, 0777, true)) {
                 die(sprintf('Unable to create the %s directory (%s)', $name, dirname($dir)));
             }
         } elseif (!is_writable($dir)) {
             die(sprintf('Unable to write in the %s directory (%s)', $name, $dir));
         }
     }
     // cache the container
     $dumper = new PhpDumper($container);
     $content = $dumper->dump(array('class' => $class));
     if (!$this->debug) {
         $content = self::stripComments($content);
     }
     $this->writeCacheFile($file, $content);
     if ($this->debug) {
         $container->addObjectResource($this);
         // save the resources
         $this->writeCacheFile($this->getCacheDir() . '/' . $class . '.meta', serialize($container->getResources()));
     }
 }
Esempio n. 2
0
 /**
  * @covers Symfony\Components\DependencyInjection\ContainerBuilder::merge
  */
 public function testMerge()
 {
     $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
     $config = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
     $container->merge($config);
     $this->assertEquals(array('bar' => 'foo', 'foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');
     $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
     $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%')));
     $container->merge($config);
     ////// FIXME
     $container->freeze();
     $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
     $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
     $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%', 'baz' => '%foo%')));
     $container->merge($config);
     ////// FIXME
     $container->freeze();
     $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
     $container = new ContainerBuilder();
     $container->register('foo', 'FooClass');
     $container->register('bar', 'BarClass');
     $config = new ContainerBuilder();
     $config->setDefinition('baz', new Definition('BazClass'));
     $config->setAlias('alias_for_foo', 'foo');
     $container->merge($config);
     $this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
     $this->assertEquals(array('alias_for_foo' => 'foo'), $container->getAliases(), '->merge() registers defined aliases');
     $container = new ContainerBuilder();
     $container->register('foo', 'FooClass');
     $config->setDefinition('foo', new Definition('BazClass'));
     $container->merge($config);
     $this->assertEquals('BazClass', $container->getDefinition('foo')->getClass(), '->merge() overrides already defined services');
 }