ContainerBuilder PHP Class Code Examples

This page contains top rated real world PHP examples of class ContainerBuilder extracted from open source projects. You can rate examples to help us improve the quality of examples

Example #1
Show file File: MimeTypeGuesserTest.php Project: aWEBoLabs/taxi
0
 /**
  * @covers ::registerWithSymfonyGuesser
  *
  * @see Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser
  */
 public function testSymfonyGuesserRegistration()
 {
     // Make the guessers property accessible on Symfony's MimeTypeGuesser.
     $symfony_guesser = SymfonyMimeTypeGuesser::getInstance();
     // Test that the Drupal mime type guess is not being used before the
     // override method is called. It is possible that the test environment does
     // not support the default guessers.
     $guessers = $this->readAttribute($symfony_guesser, 'guessers');
     if (count($guessers)) {
         $this->assertNotInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]);
     }
     $container = new ContainerBuilder();
     $container->set('file.mime_type.guesser', new MimeTypeGuesser(new StreamWrapperManager()));
     MimeTypeGuesser::registerWithSymfonyGuesser($container);
     $symfony_guesser = SymfonyMimeTypeGuesser::getInstance();
     $guessers = $this->readAttribute($symfony_guesser, 'guessers');
     $this->assertSame($container->get('file.mime_type.guesser'), $guessers[0]);
     $this->assertInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]);
     $count = count($guessers);
     $container = new ContainerBuilder();
     $container->set('file.mime_type.guesser', new MimeTypeGuesser(new StreamWrapperManager()));
     MimeTypeGuesser::registerWithSymfonyGuesser($container);
     $symfony_guesser = SymfonyMimeTypeGuesser::getInstance();
     $guessers = $this->readAttribute($symfony_guesser, 'guessers');
     $this->assertSame($container->get('file.mime_type.guesser'), $guessers[0]);
     $this->assertInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]);
     $new_count = count($guessers);
     $this->assertEquals($count, $new_count, 'The count of mime type guessers remains the same after container re-init.');
 }
Example #2
Show file File: RepositoryCompilerPassTest.php Project: larriereguichet/DoctrineRepositoryBundle
0
 public function testProcess()
 {
     // TEST 1
     // must work with no tagged services
     $containerBuilder = new ContainerBuilder();
     $compilerPass = new RepositoryCompilerPass();
     $compilerPass->process($containerBuilder);
     // TEST 2
     // must add repository tagged service into the repository pool
     $containerBuilder = new ContainerBuilder();
     $containerBuilder->setDefinition('lag.repository.repository_pool', new Definition());
     $repository = new Definition();
     $repository->addTag('doctrine.repository');
     $containerBuilder->setDefinition('repository1', $repository);
     $repository = new Definition();
     $repository->addTag('doctrine.repository');
     $containerBuilder->setDefinition('repository2', $repository);
     $compilerPass = new RepositoryCompilerPass();
     $compilerPass->process($containerBuilder);
     $methodCalls = $containerBuilder->getDefinition('lag.repository.repository_pool')->getMethodCalls();
     $this->assertCount(2, $methodCalls);
     foreach ($methodCalls as $call) {
         /** @var Reference $reference */
         $reference = $call[1][0];
         // test reference assignment
         $this->assertEquals('add', $call[0]);
         $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $reference);
         $this->assertTrue(in_array($reference->__toString(), ['repository1', 'repository2']));
     }
 }
Example #3
Show file File: MergeExtensionConfigurationPass.php Project: Ener-Getick/symfony
0
 /**
  * {@inheritdoc}
  */
 public function process(ContainerBuilder $container)
 {
     $parameters = $container->getParameterBag()->all();
     $definitions = $container->getDefinitions();
     $aliases = $container->getAliases();
     $exprLangProviders = $container->getExpressionLanguageProviders();
     foreach ($container->getExtensions() as $extension) {
         if ($extension instanceof PrependExtensionInterface) {
             $extension->prepend($container);
         }
     }
     foreach ($container->getExtensions() as $name => $extension) {
         if (!($config = $container->getExtensionConfig($name))) {
             // this extension was not called
             continue;
         }
         $config = $container->getParameterBag()->resolveValue($config);
         $tmpContainer = new ContainerBuilder($container->getParameterBag());
         $tmpContainer->setResourceTracking($container->isTrackingResources());
         $tmpContainer->addObjectResource($extension);
         foreach ($exprLangProviders as $provider) {
             $tmpContainer->addExpressionLanguageProvider($provider);
         }
         $extension->load($config, $tmpContainer);
         $container->merge($tmpContainer);
         $container->getParameterBag()->add($parameters);
     }
     $container->addDefinitions($definitions);
     $container->addAliases($aliases);
 }
Example #4
Show file File: CrossCheckTest.php Project: xingshanghe/symfony
0
 /**
  * @dataProvider crossCheckLoadersDumpers
  */
 public function testCrossCheck($fixture, $type)
 {
     $loaderClass = 'Symfony\\Component\\DependencyInjection\\Loader\\' . ucfirst($type) . 'FileLoader';
     $dumperClass = 'Symfony\\Component\\DependencyInjection\\Dumper\\' . ucfirst($type) . 'Dumper';
     $tmp = tempnam('sf_service_container', 'sf');
     file_put_contents($tmp, file_get_contents(self::$fixturesPath . '/' . $type . '/' . $fixture));
     $container1 = new ContainerBuilder();
     $loader1 = new $loaderClass($container1, new FileLocator());
     $loader1->load($tmp);
     $dumper = new $dumperClass($container1);
     file_put_contents($tmp, $dumper->dump());
     $container2 = new ContainerBuilder();
     $loader2 = new $loaderClass($container2, new FileLocator());
     $loader2->load($tmp);
     unlink($tmp);
     $this->assertEquals($container2->getAliases(), $container1->getAliases(), 'loading a dump from a previously loaded container returns the same container');
     $this->assertEquals($container2->getDefinitions(), $container1->getDefinitions(), 'loading a dump from a previously loaded container returns the same container');
     $this->assertEquals($container2->getParameterBag()->all(), $container1->getParameterBag()->all(), '->getParameterBag() returns the same value for both containers');
     $this->assertEquals(serialize($container2), serialize($container1), 'loading a dump from a previously loaded container returns the same container');
     $services1 = array();
     foreach ($container1 as $id => $service) {
         $services1[$id] = serialize($service);
     }
     $services2 = array();
     foreach ($container2 as $id => $service) {
         $services2[$id] = serialize($service);
     }
     unset($services1['service_container'], $services2['service_container']);
     $this->assertEquals($services2, $services1, 'Iterator on the containers returns the same services');
 }
Example #5
Show file File: ContainerLoader.php Project: OverByThere/Behat
0
 /**
  * Loads extension configuration.
  *
  * @param ContainerBuilder $container
  * @param Extension        $extension
  * @param array            $config
  */
 private function loadExtension(ContainerBuilder $container, Extension $extension, array $config)
 {
     $tempContainer = new ContainerBuilder(new ParameterBag(array('paths.base' => $container->getParameter('paths.base'), 'extensions' => $container->getParameter('extensions'))));
     $tempContainer->addObjectResource($extension);
     $extension->load($container, $config);
     $container->merge($tempContainer);
     $container->addCompilerPass($extension);
 }
Example #6
Show file File: TwigFormPassTest.php Project: jstnthms/FMElfinderBundle
0
 public function testProcess()
 {
     $container = new ContainerBuilder();
     $pass = new TwigFormPass();
     $pass->process($container);
     $this->assertFalse($container->hasParameter('twig.form.resources'));
     $container = new ContainerBuilder();
     $container->setParameter('twig.form.resources', array());
     $pass->process($container);
     $this->assertEquals(array('FMElfinderBundle:Form:elfinder_widget.html.twig'), $container->getParameter('twig.form.resources'));
 }
Example #7
Show file File: XmlFileLoaderTest.php Project: ABaldwinHunter/symfony-clone
0
 public function testExtensions()
 {
     $container = new ContainerBuilder();
     $container->registerExtension(new \ProjectExtension());
     $container->registerExtension(new \ProjectWithXsdExtension());
     $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));
     // extension without an XSD
     $loader->load('extensions/services1.xml');
     $container->compile();
     $services = $container->getDefinitions();
     $parameters = $container->getParameterBag()->all();
     $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
     $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
     $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
     $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
     // extension with an XSD
     $container = new ContainerBuilder();
     $container->registerExtension(new \ProjectExtension());
     $container->registerExtension(new \ProjectWithXsdExtension());
     $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));
     $loader->load('extensions/services2.xml');
     $container->compile();
     $services = $container->getDefinitions();
     $parameters = $container->getParameterBag()->all();
     $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
     $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
     $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
     $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
     $container = new ContainerBuilder();
     $container->registerExtension(new \ProjectExtension());
     $container->registerExtension(new \ProjectWithXsdExtension());
     $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));
     // extension with an XSD (does not validate)
     try {
         $loader->load('extensions/services3.xml');
         $this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
     } catch (\Exception $e) {
         $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
         $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'services3.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
         $e = $e->getPrevious();
         $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
         $this->assertContains('The attribute \'bar\' is not allowed', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
     }
     // non-registered extension
     try {
         $loader->load('extensions/services4.xml');
         $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
         $this->assertStringStartsWith('There is no extension able to load the configuration for "project:bar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
     }
 }