/** * @dataProvider crossCheckLoadersDumpers */ public function testCrossCheck($fixture, $type) { $loaderClass = 'Symfony\\Components\\DependencyInjection\\Loader\\' . ucfirst($type) . 'FileLoader'; $dumperClass = 'Symfony\\Components\\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); $loader1->load($tmp); $dumper = new $dumperClass($container1); file_put_contents($tmp, $dumper->dump()); $container2 = new ContainerBuilder(); $loader2 = new $loaderClass($container2); $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'); }
/** * @covers Symfony\Components\DependencyInjection\Loader\ClosureLoader::load */ public function testLoad() { $loader = new ClosureLoader($container = new ContainerBuilder()); $loader->load(function ($container) { $container->setParameter('foo', 'foo'); }); $this->assertEquals('foo', $container->getParameter('foo'), '->load() loads a \\Closure resource'); }
/** * Loads the Twig configuration. * * @param array $config An array of configuration settings * @param \Symfony\Components\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance */ public function configLoad($config, ContainerBuilder $container) { if (!$container->hasDefinition('twig')) { $loader = new XmlFileLoader($container, __DIR__ . '/../Resources/config'); $loader->load('twig.xml'); } $container->setParameter('twig.options', array_replace($container->getParameter('twig.options'), $config)); }
/** * Customizes the Container instance. * * @param \Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface $parameterBag A ParameterBagInterface instance * * @return \Symfony\Components\DependencyInjection\ContainerBuilder A ContainerBuilder instance */ public function buildContainer(ParameterBagInterface $parameterBag) { ContainerBuilder::registerExtension(new KernelExtension()); $container = new ContainerBuilder(); $loader = new XmlFileLoader($container, array(__DIR__ . '/../Resources/config', __DIR__ . '/Resources/config')); $loader->load('services.xml'); if ($parameterBag->get('kernel.debug')) { $loader->load('debug.xml'); $container->setDefinition('event_dispatcher', $container->findDefinition('debug.event_dispatcher')); } return $container; }
/** * @covers Symfony\Components\DependencyInjection\Extension\Extension::load */ public function testLoad() { $extension = new \ProjectExtension(); try { $extension->load('foo', array(), new ContainerBuilder()); $this->fail('->load() throws an InvalidArgumentException if the tag does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag does not exist'); $this->assertEquals('The tag "project:foo" is not defined in the "project" extension.', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag does not exist'); } $extension->load('bar', array('foo' => 'bar'), $config = new ContainerBuilder()); $this->assertEquals(array('project.parameter.bar' => 'bar', 'project.parameter.foo' => 'bar'), $config->getParameterBag()->all(), '->load() calls the method tied to the given tag'); }
public function testAddService() { $container = (include self::$fixturesPath . '/containers/container9.php'); $dumper = new YamlDumper($container); $this->assertEquals(str_replace('%path%', self::$fixturesPath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath . '/yaml/services9.yml')), $dumper->dump(), '->dump() dumps services'); $dumper = new YamlDumper($container = new ContainerBuilder()); $container->register('foo', 'FooClass')->addArgument(new \stdClass()); try { $dumper->dump(); $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); } catch (\Exception $e) { $this->assertInstanceOf('\\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); } }
/** * @covers Symfony\Components\DependencyInjection\Loader\DelegatingLoader::load */ public function testLoad() { $container = new ContainerBuilder(); $resolver = new LoaderResolver(array(new ClosureLoader($container))); $loader = new DelegatingLoader($resolver); $loader->load(function ($container) { $container->setParameter('foo', 'foo'); }); $this->assertEquals('foo', $container->getParameter('foo'), '->load() loads a resource using the loaders from the resolver'); try { $loader->load('foo.foo'); $this->fail('->load() throws an \\InvalidArgumentException if the resource cannot be loaded'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an \\InvalidArgumentException if the resource cannot be loaded'); } }
/** * @covers Symfony\Components\DependencyInjection\Loader\IniFileLoader::__construct * @covers Symfony\Components\DependencyInjection\Loader\IniFileLoader::load */ public function testLoader() { $container = new ContainerBuilder(); $loader = new IniFileLoader($container, self::$fixturesPath . '/ini'); $loader->load('parameters.ini'); $this->assertEquals(array('foo' => 'bar', 'bar' => '%foo%'), $container->getParameterBag()->all(), '->load() takes a single file name as its first argument'); try { $loader->load('foo.ini'); $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist'); $this->assertStringStartsWith('The file "foo.ini" does not exist (in: ', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist'); } try { @$loader->load('nonvalid.ini'); $this->fail('->load() throws an InvalidArgumentException if the loaded file is not parseable'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not parseable'); $this->assertEquals('The nonvalid.ini file is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file is not parseable'); } }
public function configLoad($config, ContainerBuilder $container) { if (isset($config['charset'])) { $container->setParameter('kernel.charset', $config['charset']); } if (!array_key_exists('compilation', $config)) { $classes = array('Symfony\\Components\\Routing\\Router', 'Symfony\\Components\\Routing\\RouterInterface', 'Symfony\\Components\\EventDispatcher\\Event', 'Symfony\\Components\\Routing\\Matcher\\UrlMatcherInterface', 'Symfony\\Components\\Routing\\Matcher\\UrlMatcher', 'Symfony\\Components\\HttpKernel\\HttpKernel', 'Symfony\\Components\\HttpFoundation\\Request', 'Symfony\\Components\\HttpFoundation\\Response', 'Symfony\\Components\\HttpKernel\\ResponseListener', 'Symfony\\Components\\HttpKernel\\Controller\\ControllerLoaderListener', 'Symfony\\Components\\Templating\\Loader\\LoaderInterface', 'Symfony\\Components\\Templating\\Loader\\Loader', 'Symfony\\Components\\Templating\\Loader\\FilesystemLoader', 'Symfony\\Components\\Templating\\Engine', 'Symfony\\Components\\Templating\\Renderer\\RendererInterface', 'Symfony\\Components\\Templating\\Renderer\\Renderer', 'Symfony\\Components\\Templating\\Renderer\\PhpRenderer', 'Symfony\\Components\\Templating\\Storage\\Storage', 'Symfony\\Components\\Templating\\Storage\\FileStorage', 'Symfony\\Bundle\\FrameworkBundle\\RequestListener', 'Symfony\\Bundle\\FrameworkBundle\\Controller', 'Symfony\\Bundle\\FrameworkBundle\\Templating\\Engine'); } else { $classes = array(); foreach (explode("\n", $config['compilation']) as $class) { if ($class) { $classes[] = trim($class); } } } $container->setParameter('kernel.compiled_classes', $classes); if (array_key_exists('error_handler_level', $config)) { $container->setParameter('error_handler.level', $config['error_handler_level']); } return $container; }
public function barLoad(array $config, ContainerBuilder $configuration) { $configuration->setDefinition('project.service.bar', new Definition('FooClass')); $configuration->setParameter('project.parameter.bar', isset($config['foo']) ? $config['foo'] : 'foobar'); $configuration->setDefinition('project.service.foo', new Definition('FooClass')); $configuration->setParameter('project.parameter.foo', isset($config['foo']) ? $config['foo'] : 'foobar'); return $configuration; }
/** * Customizes the Container instance. * * @param \Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface $parameterBag A ParameterBagInterface instance * * @return \Symfony\Components\DependencyInjection\ContainerBuilder A ContainerBuilder instance */ public function buildContainer(ParameterBagInterface $parameterBag) { ContainerBuilder::registerExtension(new WebExtension($parameterBag->get('kernel.bundle_dirs'), $parameterBag->get('kernel.bundles'))); $dirs = array('%kernel.root_dir%/views/%%bundle%%/%%controller%%/%%name%%%%format%%.%%renderer%%'); foreach ($parameterBag->get('kernel.bundle_dirs') as $dir) { $dirs[] = $dir . '/%%bundle%%/Resources/views/%%controller%%/%%name%%%%format%%.%%renderer%%'; } $parameterBag->set('templating.loader.filesystem.path', $dirs); $container = new ContainerBuilder(); if ($parameterBag->get('kernel.debug')) { $loader = new XmlFileLoader($container, __DIR__ . '/Resources/config'); $loader->load('debug.xml'); } return $container; }
/** * Loads the i18n configuration. * * Usage example: * * <zend:i18n locale="en" adapter="xliff" data="/path/to/messages.xml" /> * * @param array $config An array of configuration settings * @param \Symfony\Components\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance */ public function i18nLoad($config, ContainerBuilder $container) { if (!$container->hasDefinition('zend.i18n')) { $loader = new XmlFileLoader($container, __DIR__ . '/../Resources/config'); $loader->load($this->resources['i18n']); $container->setAlias('i18n', 'zend.i18n'); } if (isset($config['locale'])) { $container->setParameter('zend.translator.locale', $config['locale']); } if (isset($config['adapter'])) { $container->setParameter('zend.translator.adapter', constant($config['adapter'])); } if (isset($config['translations']) && is_array($config['translations'])) { foreach ($config['translations'] as $locale => $catalogue) { if ($locale == $container->getParameter('zend.translator.locale')) { $container->setParameter('zend.translator.catalogue', $catalogue); } $container->findDefinition('zend.translator')->addMethodCall('addTranslation', array($catalogue, $locale)); } } }
public function slideshareLoad($config, ContainerBuilder $container) { if (!$container->hasDefinition('slideshare')) { $loader = new XmlFileLoader($container, __DIR__ . '/../Resources/config'); $loader->load($this->resources['slideshare']); } if (isset($config['url'])) { $container->setParameter('news.slideshare.url', $config['url']); } if (isset($config['api_key'])) { $container->setParameter('news.slideshare.api_key', $config['api_key']); } if (isset($config['api_secret'])) { $container->setParameter('news.slideshare.api_secret', $config['api_secret']); } if (isset($config['search_query'])) { $container->setParameter('news.slideshare.search_query', $config['search_query']); } }
/** * Loads the DBAL configuration. * * @param array $config An array of configuration settings * @param \Symfony\Components\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance */ public function dbalLoad($config, ContainerBuilder $container) { if (!$container->hasDefinition('propel')) { $loader = new XmlFileLoader($container, __DIR__ . '/../Resources/config'); $loader->load($this->resources['propel']); } $defaultConnection = array('driver' => 'mysql', 'user' => 'root', 'password' => null, 'dsn' => null, 'classname' => 'DebugPDO', 'options' => array(), 'attributes' => array(), 'settings' => array('charset' => array('value' => 'UTF8'))); $defaultConnectionName = isset($config['default_connection']) ? $config['default_connection'] : $container->getParameter('propel.dbal.default_connection'); $container->setParameter('propel.dbal.default_connection', $defaultConnectionName); $connections = array(); if (isset($config['connections'])) { foreach ($config['connections'] as $name => $connection) { $connections[isset($connection['id']) ? $connection['id'] : $name] = $connection; } } else { $connections = array($defaultConnectionName => $config); } $arguments = $container->getDefinition('propel.configuration')->getArguments(); if (count($arguments)) { $c = $arguments[0]; } else { $c = array('log' => array('level' => 7), 'datasources' => array()); } foreach ($connections as $name => $connection) { if (isset($c['datasources'][$name])) { } else { $connection = array_replace($defaultConnection, $connection); $c['datasources'][$name] = array('connection' => array()); } if (isset($connection['driver'])) { $c['datasources'][$name]['adapter'] = $connection['driver']; } foreach (array('dsn', 'user', 'password', 'classname', 'options', 'attributes', 'settings') as $att) { if (isset($connection[$att])) { $c['datasources'][$name]['connection'][$att] = $connection[$att]; } } } $container->getDefinition('propel.configuration')->setArguments(array($c)); }
public function testExtensions() { $container = new ContainerBuilder(); $container->registerExtension(new \ProjectExtension()); $loader = new ProjectLoader3($container, self::$fixturesPath . '/yaml'); $loader->load('services10.yml'); $container->freeze(); $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'); try { $loader->load('services11.yml'); $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 "foobar.foobar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid'); } try { $loader->load('services12.yml'); $this->fail('->load() throws an InvalidArgumentException if an extension is not loaded'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if an extension is not loaded'); $this->assertStringStartsWith('The "foobar" tag is not valid (in', $e->getMessage(), '->load() throws an InvalidArgumentException if an extension is not loaded'); } }
public function testExtensions() { $container = new ContainerBuilder(); $container->registerExtension(new \ProjectExtension()); $container->registerExtension(new \ProjectWithXsdExtension()); $loader = new ProjectLoader2($container, self::$fixturesPath . '/xml'); // extension without an XSD $loader->load('extensions/services1.xml'); $container->freeze(); $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 ProjectLoader2($container, self::$fixturesPath . '/xml'); $loader->load('extensions/services2.xml'); $container->freeze(); $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'); $loader = new ProjectLoader2(new ContainerBuilder(), 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('\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD'); $this->assertRegexp('/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'); } // non-existent tag for a known extension try { $loader->load('extensions/services5.xml'); $this->fail('->load() throws an InvalidArgumentException if a tag is not valid for a given extension'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is not valid for a given extension'); $this->assertStringStartsWith('The tag "projectwithxsd:foobar" is not defined in the "projectwithxsd" extension.', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag is not valid for a given extension'); } }
/** * Customizes the Container instance. * * @param \Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface $parameterBag A ParameterBagInterface instance * * @return \Symfony\Components\DependencyInjection\ContainerBuilder A ContainerBuilder instance */ public function buildContainer(ParameterBagInterface $parameterBag) { ContainerBuilder::registerExtension(new DoctrineExtension($parameterBag->get('kernel.bundle_dirs'), $parameterBag->get('kernel.bundles'), $parameterBag->get('kernel.cache_dir'))); }
<?php require_once __DIR__ . '/../includes/classes.php'; use Symfony\Components\DependencyInjection\ContainerBuilder; use Symfony\Components\DependencyInjection\Reference; $container = new ContainerBuilder(); $container->register('foo', 'FooClass')->addArgument(new Reference('bar')); return $container;
public function testEntityManagerMemcacheMetadataCacheDriverConfiguration() { $container = new ContainerBuilder(); $loader = $this->getDoctrineExtensionLoader(); $container->registerExtension($loader); $this->loadFromFile($container, 'orm_service_simple_single_entity_manager'); $container->freeze(); $definition = $container->getDefinition('doctrine.orm.default_metadata_cache'); $this->assertEquals('Doctrine\\Common\\Cache\\MemcacheCache', $definition->getClass()); $calls = $definition->getMethodCalls(); $this->assertEquals('setMemcache', $calls[0][0]); $this->assertEquals('doctrine.orm.default_memcache_instance', (string) $calls[0][1][0]); $definition = $container->getDefinition('doctrine.orm.default_memcache_instance'); $this->assertEquals('Memcache', $definition->getClass()); $calls = $definition->getMethodCalls(); $this->assertEquals('connect', $calls[0][0]); $this->assertEquals('localhost', $calls[0][1][0]); $this->assertEquals(11211, $calls[0][1][1]); }
<?php require_once __DIR__ . '/../includes/classes.php'; use Symfony\Components\DependencyInjection\ContainerInterface; use Symfony\Components\DependencyInjection\ContainerBuilder; use Symfony\Components\DependencyInjection\Reference; use Symfony\Components\DependencyInjection\Parameter; $container = new ContainerBuilder(); $container->register('foo', 'FooClass')->addAnnotation('foo', array('foo' => 'foo'))->addAnnotation('foo', array('bar' => 'bar'))->setFactoryMethod('getInstance')->setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->setFile(realpath(__DIR__ . '/../includes/foo.php'))->setShared(false)->addMethodCall('setBar', array('bar'))->addMethodCall('initialize')->setConfigurator('sc_configure'); $container->register('bar', 'FooClass')->setArguments(array('foo', new Reference('foo.baz'), new Parameter('foo_bar')))->setShared(true)->setConfigurator(array(new Reference('foo.baz'), 'configure')); $container->register('foo.baz', '%baz_class%')->setFactoryMethod('getInstance')->setConfigurator(array('%baz_class%', 'configureStatic1')); $container->register('foo_bar', '%foo_class%'); $container->getParameterBag()->clear(); $container->getParameterBag()->add(array('baz_class' => 'BazClass', 'foo_class' => 'FooClass', 'foo' => 'bar')); $container->setAlias('alias_for_foo', 'foo'); $container->register('method_call1', 'FooClass')->addMethodCall('setBar', array(new Reference('foo')))->addMethodCall('setBar', array(new Reference('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE)))->addMethodCall('setBar', array(new Reference('foo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->addMethodCall('setBar', array(new Reference('foobaz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); $container->register('factory_service')->setFactoryService('foo.baz')->setFactoryMethod('getInstance'); return $container;
protected function wrapServiceConditionals($value, $code) { if (!($services = ContainerBuilder::getServiceConditionals($value))) { return $code; } $conditions = array(); foreach ($services as $service) { $conditions[] = sprintf("\$this->has('%s')", $service); } // re-indent the wrapped code $code = implode("\n", array_map(function ($line) { return $line ? ' ' . $line : $line; }, explode("\n", $code))); return sprintf(" if (%s) {\n%s }\n", implode(' && ', $conditions), $code); }
/** * @covers Symfony\Components\DependencyInjection\Loader\PhpFileLoader::load */ public function testLoad() { $loader = new PhpFileLoader($container = new ContainerBuilder()); $loader->load(__DIR__ . '/../Fixtures/php/simple.php'); $this->assertEquals('foo', $container->getParameter('foo'), '->load() loads a PHP file resource'); }
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())); } }
/** * Loads the templating configuration. * * @param array $config An array of configuration settings * @param \Symfony\Components\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance */ public function templatingLoad($config, ContainerBuilder $container) { if (!$container->hasDefinition('templating')) { $loader = new XmlFileLoader($container, __DIR__ . '/../Resources/config'); $loader->load($this->resources['templating']); } if (array_key_exists('escaping', $config)) { $container->setParameter('templating.output_escaper', $config['escaping']); } if (array_key_exists('assets_version', $config)) { $container->setParameter('templating.assets.version', $config['assets_version']); } // path for the filesystem loader if (isset($config['path'])) { $container->setParameter('templating.loader.filesystem.path', $config['path']); } // loaders if (isset($config['loader'])) { $loaders = array(); $ids = is_array($config['loader']) ? $config['loader'] : array($config['loader']); foreach ($ids as $id) { $loaders[] = new Reference($id); } if (1 === count($loaders)) { $container->setAlias('templating.loader', (string) $loaders[0]); } else { $container->getDefinition('templating.loader.chain')->addArgument($loaders); $container->setAlias('templating.loader', 'templating.loader.chain'); } } // cache? $container->setParameter('templating.loader.cache.path', null); if (isset($config['cache'])) { // wrap the loader with some cache $container->setDefinition('templating.loader.wrapped', $container->findDefinition('templating.loader')); $container->setDefinition('templating.loader', $container->getDefinition('templating.loader.cache')); $container->setParameter('templating.loader.cache.path', $config['cache']); } }
/** * Merges a ContainerBuilder with the current ContainerBuilder configuration. * * Service definitions overrides the current defined ones. * * But for parameters, they are overridden by the current ones. It allows * the parameters passed to the container constructor to have precedence * over the loaded ones. * * $container = new ContainerBuilder(array('foo' => 'bar')); * $loader = new LoaderXXX($container); * $loader->load('resource_name'); * $container->register('foo', new stdClass()); * * In the above example, even if the loaded resource defines a foo * parameter, the value will still be 'bar' as defined in the ContainerBuilder * constructor. */ public function merge(ContainerBuilder $container) { if (true === $this->isFrozen()) { throw new \LogicException('Cannot merge on a frozen container.'); } $this->addDefinitions($container->getDefinitions()); $this->addAliases($container->getAliases()); $this->parameterBag->add($container->getParameterBag()->all()); foreach ($container->getResources() as $resource) { $this->addResource($resource); } foreach ($container->getExtensionContainers() as $name => $container) { if (isset($this->extensionContainers[$name])) { $this->extensionContainers[$name]->merge($container); } else { $this->extensionContainers[$name] = $container; } } }
/** * Loads the Swift Mailer configuration. * * Usage example: * * <swift:mailer transport="gmail" delivery_strategy="spool"> * <swift:username>fabien</swift:username> * <swift:password>xxxxx</swift:password> * <swift:spool path="/path/to/spool/" /> * </swift:mailer> * * @param array $config An array of configuration settings * @param \Symfony\Components\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance */ public function mailerLoad($config, ContainerBuilder $container) { if (!$container->hasDefinition('swiftmailer.mailer')) { $loader = new XmlFileLoader($container, __DIR__ . '/../Resources/config'); $loader->load($this->resources['mailer']); $container->setAlias('mailer', 'swiftmailer.mailer'); } $r = new \ReflectionClass('Swift_Message'); $container->setParameter('swiftmailer.base_dir', dirname(dirname(dirname($r->getFilename())))); $transport = $container->getParameter('swiftmailer.transport.name'); if (array_key_exists('transport', $config)) { if (null === $config['transport']) { $transport = 'null'; } elseif ('gmail' === $config['transport']) { $config['encryption'] = 'ssl'; $config['auth_mode'] = 'login'; $config['host'] = 'smtp.gmail.com'; $transport = 'smtp'; } else { $transport = $config['transport']; } $container->setParameter('swiftmailer.transport.name', $transport); } $container->setAlias('swiftmailer.transport', 'swiftmailer.transport.' . $transport); if (isset($config['encryption']) && 'ssl' === $config['encryption'] && !isset($config['port'])) { $config['port'] = 465; } foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode') as $key) { if (isset($config[$key])) { $container->setParameter('swiftmailer.transport.' . $transport . '.' . $key, $config[$key]); } } // spool? if (isset($config['spool'])) { $type = isset($config['type']) ? $config['type'] : 'file'; $container->setAlias('swiftmailer.transport.real', 'swiftmailer.transport.' . $transport); $container->setAlias('swiftmailer.transport', 'swiftmailer.transport.spool'); $container->setAlias('swiftmailer.spool', 'swiftmailer.spool.' . $type); foreach (array('path') as $key) { if (isset($config['spool'][$key])) { $container->setParameter('swiftmailer.spool.' . $type . '.' . $key, $config['spool'][$key]); } } } if (isset($config['delivery_address'])) { $container->setParameter('swiftmailer.single_address', $config['delivery_address']); $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.redirecting'))); } if (isset($config['disable_delivery']) && $config['disable_delivery']) { $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.blackhole'))); } }
/** * Customizes the Container instance. * * @param \Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface $parameterBag A ParameterBagInterface instance * * @return \Symfony\Components\DependencyInjection\ContainerBuilder A ContainerBuilder instance */ public function buildContainer(ParameterBagInterface $parameterBag) { ContainerBuilder::registerExtension(new ZendExtension()); }
/** * Detects what metadata driver to use for the supplied directory. * * @param string $dir A directory path * @param ContainerBuilder $container A ContainerBuilder configuration * * @return string|null A metadata driver short name, if one can be detected */ protected static function detectMetadataDriver($dir, ContainerBuilder $container) { // add the closest existing directory as a resource $resource = $dir . '/Resources/config/doctrine/metadata'; while (!is_dir($resource)) { $resource = dirname($resource); } $container->addResource(new FileResource($resource)); if (count(glob($dir . '/Resources/config/doctrine/metadata/*.xml'))) { return 'xml'; } elseif (count(glob($dir . '/Resources/config/doctrine/metadata/*.yml'))) { return 'yml'; } // add the directory itself as a resource $container->addResource(new FileResource($dir)); if (is_dir($dir . '/Entity')) { return 'annotation'; } }
/** * @covers Symfony\Components\DependencyInjection\ContainerBuilder::registerExtension * @covers Symfony\Components\DependencyInjection\ContainerBuilder::getExtension */ public function testExtension() { $container = new ContainerBuilder(); $container->registerExtension($extension = new \ProjectExtension()); $this->assertTrue($container->getExtension('project') === $extension, '->registerExtension() registers an extension'); }