Skip to content

Commit

Permalink
allow closures as argument in TalesRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Schwarz authored and usox committed Jun 27, 2018
1 parent dd19ca7 commit 4af56cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/TalesRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public static function unregisterPrefix(string $prefix): void

/**
*
* Expects an either a function name or an array of class and method as
* callback.
* Expects either a function name or an array of class and method or a closure as callback.
* A closure *must* return a string enclosed in double quotes.
*
* @param string $prefix
* @param mixed $callback
Expand Down Expand Up @@ -95,6 +95,8 @@ public static function registerPrefix(string $prefix, $callback, ?bool $is_fallb
if (!$method->isStatic()) {
throw new Exception\ConfigurationException('The method you want to register is not static.');
}
} elseif (is_callable($callback)) {
// do nothing
} elseif (!function_exists($callback)) {
throw new Exception\ConfigurationException('The function you are trying to register does not exist.');
}
Expand Down
11 changes: 11 additions & 0 deletions tests/TalesRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public function testRegisterFunction()
);
}

public function testRegisterFunctionAcceptsClosure()
{
\PhpTal\TalesRegistry::registerPrefix('foobar', function ($arg) {
return '"ok' . $arg . '"';
});
static::assertSame(
'<p>ok1</p>',
$this->newPHPTAL()->setSource('<p tal:content="foobar:1"/>')->execute()
);
}

/**
* @runInSeparateProcess
* @expectedException \PhpTal\Exception\UnknownModifierException
Expand Down

0 comments on commit 4af56cb

Please sign in to comment.