diff --git a/src/TwigExtension/ExtensionAdapter.php b/src/TwigExtension/ExtensionAdapter.php index cd669c0..ae05fbc 100644 --- a/src/TwigExtension/ExtensionAdapter.php +++ b/src/TwigExtension/ExtensionAdapter.php @@ -1,9 +1,9 @@ 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); - } - } - } + // Iterates recursively through theme to find Twig extensions. + $dir = new RecursiveDirectoryIterator($themePath); + $ite = new RecursiveIteratorIterator($dir); + // Searches for Twig extensions of $type. + // Excludes plugin names starting with: ".", "_", and "pl_". + $pattern = '/.*\/_twig-components\/' . $type . '\/(?!(?:\.|_|pl_)).*\.php/'; + $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH); + $files = array_keys(iterator_to_array($files)); + // Loads each matching Twig extension that was found. + foreach ($files as $file) { + static::load($type, $file); } } @@ -71,15 +77,17 @@ static protected function loadAll($type) { * @param string $file * The fully qualified path of the plugin to be loaded. */ - static protected function load($type, $file) { + protected static function load($type, $file) { include $file; switch ($type) { case 'filters': - self::$objects['filters'][] = $filter; + self::$objects['filters'][] = $file; break; + case 'functions': - self::$objects['functions'][] = $function; + self::$objects['functions'][] = $file; break; + case 'tags': if (preg_match('/^([^\.]+)\.tag\.php$/', basename($file), $matches)) { $class = "Project_{$matches[1]}_TokenParser"; @@ -90,4 +98,5 @@ static protected function load($type, $file) { break; } } + } diff --git a/unified_twig_ext.services.yml b/unified_twig_ext.services.yml index 4574878..9bb7029 100755 --- a/unified_twig_ext.services.yml +++ b/unified_twig_ext.services.yml @@ -1,5 +1,5 @@ services: - newcity_twig.twig_extension: + unified_twig_ext.twig_extension: arguments: ['@renderer'] class: Drupal\unified_twig_ext\TwigExtension\ExtensionAdapter tags: