ExtensionLoader.php in Unified Twig Extensions 1.0.x
File
src/TwigExtension/ExtensionLoader.php
View source
<?php
namespace Drupal\unified_twig_ext\TwigExtension;
class ExtensionLoader {
protected static $objects = [];
public static function init() {
if (!self::$objects) {
static::loadAll('filters');
static::loadAll('functions');
static::loadAll('tags');
}
}
public static function get($type) {
return !empty(self::$objects[$type]) ? self::$objects[$type] : [];
}
protected static function loadAll($type) {
$theme = \Drupal::config('system.theme')
->get('default');
$themeLocation = drupal_get_path('theme', $theme);
$themePath = DRUPAL_ROOT . '/' . $themeLocation . '/';
$extensionPaths = glob($themePath . '*/_twig-components/');
foreach ($extensionPaths as $extensionPath) {
$fullPath = $extensionPath;
foreach (scandir($fullPath . $type) as $file) {
$fileInfo = pathinfo($file);
if ($fileInfo['extension'] === 'php') {
if ($file[0] != '.' && $file[0] != '_' && substr($file, 0, 3) != 'pl_') {
static::load($type, $fullPath . $type . '/' . $file);
}
}
}
}
}
protected static function load($type, $file) {
include $file;
switch ($type) {
case 'filters':
if (isset($filter)) {
self::$objects['filters'][] = $filter;
}
break;
case 'functions':
if (isset($function)) {
self::$objects['functions'][] = $function;
}
break;
case 'tags':
if (preg_match('/^([^\\.]+)\\.tag\\.php$/', basename($file), $matches)) {
$class = "Project_{$matches[1]}_TokenParser";
if (class_exists($class)) {
self::$objects['parsers'][] = new $class();
}
}
break;
}
}
}