Skip to content

Commit

Permalink
[DependencyInjection] added a __call() method to Container to allow u…
Browse files Browse the repository at this point in the history
…sage of getXXXService() methods even whithout a PHP dump (allows for faster production environments)
  • Loading branch information
fabpot committed Jan 27, 2010
1 parent 7b2a968 commit f8e3b4b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Symfony/Components/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,24 @@ public function valid()
return $this->count > 0;
}

/**
* Catches unknown methods.
*
* @param string $method The called method name
* @param array $arguments The method arguments
*
* @return mixed
*/
public function __call($method, $arguments)
{
if (!preg_match('/^get(.+)Service$/', $method, $match))
{
throw new \RuntimeException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
}

return $this->getService(self::underscore($match[1]));
}

static public function camelize($id)
{
return preg_replace(array('/(^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/';

$t = new LimeTest(41);
$t = new LimeTest(43);

// __construct()
$t->diag('__construct()');
Expand Down Expand Up @@ -206,3 +206,19 @@ protected function getFoo_BazService()
'foo.baz' => spl_object_hash($sc->__foo_baz),
'foo' => spl_object_hash($foo)),
'Container implements the Iterator interface');

// __call()
$t->diag('__call()');
$sc = new Container();
$sc->setService('foo_bar.foo', $foo = new stdClass());
$t->is($sc->getFooBar_FooService(), $foo, '__call() finds services is the method is getXXXService()');

try
{
$sc->getFooBar_Foo();
$t->pass('__call() throws a \RuntimeException exception if the method is not a service method');
}
catch (\RuntimeException $e)
{
$t->pass('__call() throws a \RuntimeException exception if the method is not a service method');
}

1 comment on commit f8e3b4b

@fabeat
Copy link

@fabeat fabeat commented on f8e3b4b Jan 27, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the test fail if the Exception is not thrown?

Please sign in to comment.