-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8399988
commit f8e8a8d
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Everlution\AjaxcomBundle\DataSource; | ||
|
||
/** | ||
* Class BaseDataSource. | ||
* | ||
* @author Ivan Barlog <[email protected]> | ||
*/ | ||
class BaseDataSource extends \Twig_Extension | ||
{ | ||
protected const TWIG_FUNCTION_SUFFIX = 'Provider'; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getFunctions() | ||
{ | ||
$functions = []; | ||
$class = new \ReflectionClass(get_class($this)); | ||
|
||
if (self::class === $class->name) { | ||
return $functions; | ||
} | ||
|
||
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { | ||
if (false === ($offset = strpos($method->getName(), self::TWIG_FUNCTION_SUFFIX))) { | ||
continue; | ||
} | ||
|
||
$functions[] = $this->registerAsTwigFunction(substr($method->getName(), 0, $offset)); | ||
} | ||
|
||
return $functions; | ||
} | ||
|
||
private function registerAsTwigFunction(string $name): ?\Twig_SimpleFunction | ||
{ | ||
return new \Twig_SimpleFunction($name, [$this, $name.self::TWIG_FUNCTION_SUFFIX]); | ||
} | ||
} |