Skip to content

Commit 808088a

Browse files
committed
added the ability to use dot and single quotes in the keys and values
1 parent cbb4bba commit 808088a

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ private function dumpValue($value, $interpolate = true)
10221022
$code = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', $replaceParameters, var_export($value, true)));
10231023

10241024
// optimize string
1025-
$code = preg_replace(array("/^''\./", "/\.''$/", "/'\.'/", "/\.''\./"), array('', '', '', '.'), $code);
1025+
$code = preg_replace(array("/^''\./", "/\.''$/", "/(\w+)(?:'\.')/", "/(.+)(?:\.''\.)/"), array('', '', '$1', '$1.'), $code);
10261026

10271027
return $code;
10281028
}

tests/Symfony/Tests/Component/DependencyInjection/Dumper/PhpDumperTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
1616
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
1717
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\Definition;
1819

1920
class PhpDumperTest extends \PHPUnit_Framework_TestCase
2021
{
@@ -33,15 +34,42 @@ public function testDump()
3334
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services1-1.php', $dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), '->dump() takes a class and a base_class options');
3435

3536
$container = new ContainerBuilder();
37+
new PhpDumper($container);
38+
}
39+
40+
public function testDumpOptimizationString()
41+
{
42+
$definition = new Definition();
43+
$definition->setClass('stdClass');
44+
$definition->addArgument(array(
45+
'only dot' => '.',
46+
'concatenation as value' => '.\'\'.',
47+
'concatenation from the start value' => '\'\'.',
48+
'.' => 'dot as a key',
49+
'.\'\'.' => 'concatenation as a key',
50+
'\'\'.' =>'concatenation from the start key',
51+
'optimize concatenation' => "string1%some_string%string2",
52+
'optimize concatenation with empty string' => "string1%empty_value%string2",
53+
'optimize concatenation from the start' => '%empty_value%start',
54+
'optimize concatenation at the end' => 'end%empty_value%',
55+
));
56+
57+
$container = new ContainerBuilder();
58+
$container->setDefinition('test', $definition);
59+
$container->setParameter('empty_value', '');
60+
$container->setParameter('some_string', '-');
61+
$container->compile();
62+
3663
$dumper = new PhpDumper($container);
64+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
3765
}
3866

3967
/**
4068
* @expectedException InvalidArgumentException
4169
*/
4270
public function testExportParameters()
4371
{
44-
$dumper = new PhpDumper($container = new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
72+
$dumper = new PhpDumper(new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
4573
$dumper->dump();
4674
}
4775

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerInterface;
4+
use Symfony\Component\DependencyInjection\Container;
5+
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
6+
use Symfony\Component\DependencyInjection\Reference;
7+
use Symfony\Component\DependencyInjection\Parameter;
8+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
9+
10+
/**
11+
* ProjectServiceContainer
12+
*
13+
* This class has been auto-generated
14+
* by the Symfony Dependency Injection Component.
15+
*/
16+
class ProjectServiceContainer extends Container
17+
{
18+
/**
19+
* Constructor.
20+
*/
21+
public function __construct()
22+
{
23+
$this->parameters = $this->getDefaultParameters();
24+
25+
$this->services =
26+
$this->scopedServices =
27+
$this->scopeStacks = array();
28+
29+
$this->set('service_container', $this);
30+
31+
$this->scopes = array();
32+
$this->scopeChildren = array();
33+
}
34+
35+
/**
36+
* Gets the 'test' service.
37+
*
38+
* This service is shared.
39+
* This method always returns the same instance of the service.
40+
*
41+
* @return stdClass A stdClass instance.
42+
*/
43+
protected function getTestService()
44+
{
45+
return $this->services['test'] = new \stdClass(array('only dot' => '.', 'concatenation as value' => '.\'\'.', 'concatenation from the start value' => '\'\'.', '.' => 'dot as a key', '.\'\'.' => 'concatenation as a key', '\'\'.' => 'concatenation from the start key', 'optimize concatenation' => 'string1-string2', 'optimize concatenation with empty string' => 'string1string2', 'optimize concatenation from the start' => 'start', 'optimize concatenation at the end' => 'end'));
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function getParameter($name)
52+
{
53+
$name = strtolower($name);
54+
55+
if (!array_key_exists($name, $this->parameters)) {
56+
throw new \InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
57+
}
58+
59+
return $this->parameters[$name];
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function hasParameter($name)
66+
{
67+
return array_key_exists(strtolower($name), $this->parameters);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function setParameter($name, $value)
74+
{
75+
throw new \LogicException('Impossible to call set() on a frozen ParameterBag.');
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*/
81+
public function getParameterBag()
82+
{
83+
if (null === $this->parameterBag) {
84+
$this->parameterBag = new FrozenParameterBag($this->parameters);
85+
}
86+
87+
return $this->parameterBag;
88+
}
89+
/**
90+
* Gets the default parameters.
91+
*
92+
* @return array An array of the default parameters
93+
*/
94+
protected function getDefaultParameters()
95+
{
96+
return array(
97+
'empty_value' => '',
98+
'some_string' => '-',
99+
);
100+
}
101+
}

0 commit comments

Comments
 (0)