/** * Calls a method with given arguments * * @param string $methodName * @param array $arguments * @return mixed * @throws FunctionException If the method does not exist */ public function call($methodName, $arguments = []) { $methodName = strtolower($methodName); if (isset($this->aliases[$methodName])) { $methodName = $this->aliases[$methodName]; } if (isset($this->functions[$methodName])) { $arguments = $this->prepareArguments($arguments); if ($this->functions[$methodName] === true) { // built in function return call_user_func_array([$this, $methodName], $arguments); } else { // this is a external callable // provide access to function registry (pass as first parameter) array_unshift($arguments, $this); return call_user_func_array($this->functions[$methodName], $arguments); } } else { if ($this->parent) { return $this->parent->call($methodName, $arguments); } } }
/** * @covers addFunction */ public function testCustomFunction() { $registry = new FunctionRegistry(); $registry->addFunction('foobar', [$this, 'foobarCallable']); $registry->call('foobar', ['a', 'b']); }
/** * Sets the function registry, also links the registry with this environment instance. * * @param FunctionRegistry $registry * * @return Context */ public function setFunctionRegistry(FunctionRegistry $registry) { $this->functionRegistry = $registry; // provide access to The context, which is need to access generateCSS() $this->functionRegistry->setEnvironment($this); return $this; }