Skip to content

[DependencyInjection] Fix Container::camelize to convert beginning and ending . and _ #8536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public function isScopeActive($name)
*/
public static function camelize($id)
{
return preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); }, $id);
return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ '))), array(' ' => ''));
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ public function testConstructor()
$this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
}

/**
* @dataProvider dataForTestCamelize
*/
public function testCamelize($id, $expected)
{
$this->assertEquals($expected, Container::camelize($id), sprintf('Container::camelize("%s")', $id));
}

public function dataForTestCamelize()
{
return array(
array('foo_bar', 'FooBar'),
array('foo.bar', 'Foo_Bar'),
array('foo.bar_baz', 'Foo_BarBaz'),
array('foo._bar', 'Foo_Bar'),
array('foo_.bar', 'Foo_Bar'),
array('_foo', 'Foo'),
array('.foo', '_Foo'),
array('foo_', 'Foo'),
array('foo.', 'Foo_'),
);
}

/**
* @covers Symfony\Component\DependencyInjection\Container::compile
*/
Expand Down