protected function getValue($value, $default = '') { return Builder::resolveValue($value, $this->container->getParameters()); }
$t->diag('::resolveValue()'); $t->is(Builder::resolveValue('foo', array()), 'foo', '->resolveValue() returns its argument unmodified if no placeholders are found'); $t->is(Builder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), 'I\'m a bar', '->resolveValue() replaces placeholders by their values'); $t->ok(Builder::resolveValue('%foo%', array('foo' => true)) === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings'); $t->is(Builder::resolveValue(array('%foo%' => '%foo%'), array('foo' => 'bar')), array('bar' => 'bar'), '->resolveValue() replaces placeholders in keys and values of arrays'); $t->is(Builder::resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%'))), array('foo' => 'bar')), array('bar' => array('bar' => array('bar' => 'bar'))), '->resolveValue() replaces placeholders in nested arrays'); $t->is(Builder::resolveValue('I\'m a %%foo%%', array('foo' => 'bar')), 'I\'m a %foo%', '->resolveValue() supports % escaping by doubling it'); $t->is(Builder::resolveValue('I\'m a %foo% %%foo %foo%', array('foo' => 'bar')), 'I\'m a bar %foo bar', '->resolveValue() supports % escaping by doubling it'); try { Builder::resolveValue('%foobar%', array()); $t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } catch (RuntimeException $e) { $t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } try { Builder::resolveValue('foo %foobar% bar', array()); $t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } catch (RuntimeException $e) { $t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } // ->resolveServices() $t->diag('->resolveServices()'); $builder = new Builder(); $builder->register('foo', 'FooClass'); $t->is($builder->resolveServices(new Reference('foo')), $builder->getService('foo'), '->resolveServices() resolves service references to service instances'); $t->is($builder->resolveServices(array('foo' => array('foo', new Reference('foo')))), array('foo' => array('foo', $builder->getService('foo'))), '->resolveServices() resolves service references to service instances in nested arrays'); // ->merge() $t->diag('->merge()'); $container = new Builder(); $container->merge(null); $t->is($container->getParameters(), array(), '->merge() accepts null as an argument');
public function optimizeContainer(Builder $container) { // replace all classes with the real value foreach ($container->getDefinitions() as $definition) { if (false !== strpos($class = $definition->getClass(), '%')) { $definition->setClass(Builder::resolveValue($class, $container->getParameters())); unset($container[substr($class, 1, -1)]); } } }
/** * @covers Symfony\Components\DependencyInjection\Builder::resolveValue */ public function testResolveValue() { $this->assertEquals('foo', Builder::resolveValue('foo', array()), '->resolveValue() returns its argument unmodified if no placeholders are found'); $this->assertEquals('I\'m a bar', Builder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), '->resolveValue() replaces placeholders by their values'); $this->assertTrue(Builder::resolveValue('%foo%', array('foo' => true)) === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings'); $this->assertEquals(array('bar' => 'bar'), Builder::resolveValue(array('%foo%' => '%foo%'), array('foo' => 'bar')), '->resolveValue() replaces placeholders in keys and values of arrays'); $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), Builder::resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%'))), array('foo' => 'bar')), '->resolveValue() replaces placeholders in nested arrays'); $this->assertEquals('I\'m a %foo%', Builder::resolveValue('I\'m a %%foo%%', array('foo' => 'bar')), '->resolveValue() supports % escaping by doubling it'); $this->assertEquals('I\'m a bar %foo bar', Builder::resolveValue('I\'m a %foo% %%foo %foo%', array('foo' => 'bar')), '->resolveValue() supports % escaping by doubling it'); try { Builder::resolveValue('%foobar%', array()); $this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } catch (\Exception $e) { $this->assertInstanceOf('\\RuntimeException', $e, '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); $this->assertEquals('The parameter "foobar" must be defined.', $e->getMessage(), '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } try { Builder::resolveValue('foo %foobar% bar', array()); $this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } catch (\Exception $e) { $this->assertInstanceOf('\\RuntimeException', $e, '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); $this->assertEquals('The parameter "foobar" must be defined (used in the following expression: "foo %foobar% bar").', $e->getMessage(), '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } }
public function testResolveValue() { $this->assertEquals(Builder::resolveValue('foo', array()), 'foo', '->resolveValue() returns its argument unmodified if no placeholders are found'); $this->assertEquals(Builder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), 'I\'m a bar', '->resolveValue() replaces placeholders by their values'); $this->assertTrue(Builder::resolveValue('%foo%', array('foo' => true)) === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings'); $this->assertEquals(Builder::resolveValue(array('%foo%' => '%foo%'), array('foo' => 'bar')), array('bar' => 'bar'), '->resolveValue() replaces placeholders in keys and values of arrays'); $this->assertEquals(Builder::resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%'))), array('foo' => 'bar')), array('bar' => array('bar' => array('bar' => 'bar'))), '->resolveValue() replaces placeholders in nested arrays'); $this->assertEquals(Builder::resolveValue('I\'m a %%foo%%', array('foo' => 'bar')), 'I\'m a %foo%', '->resolveValue() supports % escaping by doubling it'); $this->assertEquals(Builder::resolveValue('I\'m a %foo% %%foo %foo%', array('foo' => 'bar')), 'I\'m a bar %foo bar', '->resolveValue() supports % escaping by doubling it'); try { Builder::resolveValue('%foobar%', array()); $this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } catch (\RuntimeException $e) { } try { Builder::resolveValue('foo %foobar% bar', array()); $this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); } catch (\RuntimeException $e) { } }