[ExpressionLanguage] renamed addFunction() to register()
This commit is contained in:
parent
5076ec7e90
commit
e8691366ce
|
@ -27,13 +27,13 @@ class ExpressionLanguage extends BaseExpressionLanguage
|
|||
{
|
||||
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);
|
||||
|
|
|
@ -82,14 +82,21 @@ class ExpressionLanguage
|
|||
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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -24,31 +24,31 @@ class ExpressionLanguage extends BaseExpressionLanguage
|
|||
{
|
||||
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']);
|
||||
|
|
Reference in New Issue