public function BackendBase::registerFunction in SCSS Compiler 1.0.x
Register a PHP callback function bridge that can be used in SCSS code.
It is recommended to register all needed functions before the compiler is ran for the first time. If a function is registered after the compiler's first run, then the behavior is undefined.
Parameters
callable $cb: The PHP function to call when bridged from SCSS.
Optional and pass-by-reference parameters aren't supported.
string|null $name: The name of the function as it should be used in the compiled language. This value must be a valid PHP identifier.
Optional unless an anonymous function is supplied (default: NULL).
Throws
\InvalidArgumentException If a bad function name is supplied, or a callback with an optional or pass-by-reference parameter is supplied.
Overrides BackendInterface::registerFunction
File
- src/
BackendBase.php, line 183
Class
- BackendBase
- Provides a base compiler backend implementation.
Namespace
Drupal\compiler_scssCode
public function registerFunction(callable $callback, $name = NULL) {
// Use reflection to facilitate registering the supplied callback function.
$reflection = new \ReflectionFunction($callback);
// Attempt to resolve the name of the callback function.
if (!$reflection
->isClosure()) {
$name = $reflection
->getShortName();
}
elseif (preg_match(self::IDENT_EXPR, $name) !== 1) {
throw new \InvalidArgumentException('Bad function name');
}
// Fetch a list of parameter names for this function.
$params = array_map(function ($param) {
// TODO: We don't yet support optional parameters.
//
// Pass-by-reference parameters will never be supported across languages.
if ($param
->isOptional() || $param
->isPassedByReference()) {
throw new \InvalidArgumentException('Optional or pass-by-reference parameters are not supported for callback functions');
}
return $param
->getName();
}, $reflection
->getParameters());
// Construct the function's SASS signature using the parameter list.
$signature = implode(', ', array_map(function ($param) {
return '$' . $param;
}, $params));
$this->functions[$name] = (object) [
'callback' => $callback,
'name' => $name,
'params' => $params,
'signature' => $signature,
];
}