Skip to content

Commit

Permalink
[ExpressionLanguage] renamed addFunction() to register()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 21, 2013
1 parent 5076ec7 commit e869136
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
Expand Up @@ -27,13 +27,13 @@ protected function registerFunctions()
{
parent::registerFunctions();

$this->addFunction('service', function ($arg) {
$this->register('service', function ($arg) {
return sprintf('$this->get(%s)', $arg);
}, function (array $variables, $value) {
return $variables['container']->get($value);
});

$this->addFunction('parameter', function ($arg) {
$this->register('parameter', function ($arg) {
return sprintf('$this->getParameter(%s)', $arg);
}, function (array $variables, $value) {
return $variables['container']->getParameter($value);
Expand Down
11 changes: 9 additions & 2 deletions src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php
Expand Up @@ -82,14 +82,21 @@ public function parse($expression, $names)
return $this->cache[$key];
}

public function addFunction($name, $compiler, $evaluator)
/**
* Registers a function.
*
* @param string $name The function name
* @param callable $compiler A callable able to compile the function
* @param callable $evaluator A callable able to evaluate the function
*/
public function register($name, $compiler, $evaluator)
{
$this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
}

protected function registerFunctions()
{
$this->addFunction('constant', function ($constant) {
$this->register('constant', function ($constant) {
return sprintf('constant(%s)', $constant);
}, function (array $values, $constant) {
return constant($constant);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/ExpressionLanguage/README.md
Expand Up @@ -25,7 +25,7 @@ You can extend your DSL with functions:
$evaluator = function (array $variables, $value) {
return strtoupper($value);
};
$language->addFunction('upper', $compiler, $evaluator);
$language->register('upper', $compiler, $evaluator);

echo $language->evaluate('"foo" ~ upper(foo)', array('foo' => 'bar'));
// would output fooBAR
Expand Down
Expand Up @@ -24,31 +24,31 @@ protected function registerFunctions()
{
parent::registerFunctions();

$this->addFunction('is_anonymous', function () {
$this->register('is_anonymous', function () {
return '$trust_resolver->isAnonymous($token)';
}, function (array $variables) {
return $variables['trust_resolver']->isAnonymous($variables['token']);
});

$this->addFunction('is_authenticated', function () {
$this->register('is_authenticated', function () {
return '!$trust_resolver->isAnonymous($token)';
}, function (array $variables) {
return !$variables['trust_resolver']->isAnonymous($variables['token']);
});

$this->addFunction('is_fully_authenticated', function () {
$this->register('is_fully_authenticated', function () {
return '!$trust_resolver->isFullFledge($token)';
}, function (array $variables) {
return !$variables['trust_resolver']->isFullFledge($variables['token']);
});

$this->addFunction('is_remember_me', function () {
$this->register('is_remember_me', function () {
return '!$trust_resolver->isRememberMe($token)';
}, function (array $variables) {
return !$variables['trust_resolver']->isRememberMe($variables['token']);
});

$this->addFunction('has_role', function ($role) {
$this->register('has_role', function ($role) {
return sprintf('in_array(%s, $roles)', $role);
}, function (array $variables, $role) {
return in_array($role, $variables['roles']);
Expand Down

0 comments on commit e869136

Please sign in to comment.