From eb413cdf92ed04e005a38c97e516f6f172ed5816 Mon Sep 17 00:00:00 2001 From: Michael Wojcik Date: Tue, 6 Nov 2018 17:42:16 -0500 Subject: [PATCH 1/2] Fixes ExtensionLoader::loadAll method to find all Twig extensions as expected --- src/TwigExtension/ExtensionAdapter.php | 4 +-- src/TwigExtension/ExtensionLoader.php | 47 +++++++++++++++----------- unified_twig_ext.services.yml | 2 +- 3 files changed, 31 insertions(+), 22 deletions(-) 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; break; + case 'functions': self::$objects['functions'][] = $function; 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: From a3ccfef40be6c5b9743fcda14b1d6292858d16d5 Mon Sep 17 00:00:00 2001 From: Michael Wojcik Date: Mon, 12 Nov 2018 18:50:02 -0500 Subject: [PATCH 2/2] Minor fix to ExtensionLoader::load method --- src/TwigExtension/ExtensionLoader.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TwigExtension/ExtensionLoader.php b/src/TwigExtension/ExtensionLoader.php index e566adf..05a8887 100644 --- a/src/TwigExtension/ExtensionLoader.php +++ b/src/TwigExtension/ExtensionLoader.php @@ -81,11 +81,11 @@ 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':