From 9a546c535c9f9c160dd4a40e07f07eb33c883ecb Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 26 Dec 2022 13:51:45 +0100 Subject: [PATCH] [ticket/16955] Use common code for path iterator generation PHPBB3-16955 --- phpBB/develop/export_events_for_bbcode.php | 2 +- phpBB/develop/export_events_for_rst.php | 2 +- phpBB/develop/export_events_for_wiki.php | 2 +- phpBB/includes/acp/acp_language.php | 11 +---- phpBB/includes/functions_compatibility.php | 12 ++--- phpBB/phpbb/event/md_exporter.php | 9 +--- phpBB/phpbb/extension/manager.php | 9 ++-- phpBB/phpbb/finder/finder.php | 9 +--- .../recursive_dot_prefix_filter_iterator.php | 2 +- .../iterator/recursive_path_iterator.php | 47 +++++++++++++++++++ 10 files changed, 64 insertions(+), 41 deletions(-) rename phpBB/phpbb/{ => iterator}/recursive_dot_prefix_filter_iterator.php (96%) create mode 100644 phpBB/phpbb/iterator/recursive_path_iterator.php diff --git a/phpBB/develop/export_events_for_bbcode.php b/phpBB/develop/export_events_for_bbcode.php index 56709189c8..e30027bf94 100644 --- a/phpBB/develop/export_events_for_bbcode.php +++ b/phpBB/develop/export_events_for_bbcode.php @@ -65,7 +65,7 @@ require __DIR__ . '/../phpbb/event/md_exporter.' . $phpEx; require __DIR__ . '/../phpbb/event/rst_exporter.' . $phpEx; require __DIR__ . '/../includes/functions.' . $phpEx; require __DIR__ . '/../phpbb/event/recursive_event_filter_iterator.' . $phpEx; -require __DIR__ . '/../phpbb/recursive_dot_prefix_filter_iterator.' . $phpEx; +require __DIR__ . '/../phpbb/iterator/recursive_dot_prefix_filter_iterator.' . $phpEx; switch ($action) { diff --git a/phpBB/develop/export_events_for_rst.php b/phpBB/develop/export_events_for_rst.php index d8cef7bf9f..908d4298d6 100644 --- a/phpBB/develop/export_events_for_rst.php +++ b/phpBB/develop/export_events_for_rst.php @@ -68,7 +68,7 @@ require __DIR__ . '/../phpbb/event/md_exporter.' . $phpEx; require __DIR__ . '/../phpbb/event/rst_exporter.' . $phpEx; require __DIR__ . '/../includes/functions.' . $phpEx; require __DIR__ . '/../phpbb/event/recursive_event_filter_iterator.' . $phpEx; -require __DIR__ . '/../phpbb/recursive_dot_prefix_filter_iterator.' . $phpEx; +require __DIR__ . '/../phpbb/iterator/recursive_dot_prefix_filter_iterator.' . $phpEx; switch ($action) { diff --git a/phpBB/develop/export_events_for_wiki.php b/phpBB/develop/export_events_for_wiki.php index be16e5e7cd..128d6f3c57 100644 --- a/phpBB/develop/export_events_for_wiki.php +++ b/phpBB/develop/export_events_for_wiki.php @@ -67,7 +67,7 @@ require __DIR__ . '/../phpbb/event/php_exporter.' . $phpEx; require __DIR__ . '/../phpbb/event/md_exporter.' . $phpEx; require __DIR__ . '/../includes/functions.' . $phpEx; require __DIR__ . '/../phpbb/event/recursive_event_filter_iterator.' . $phpEx; -require __DIR__ . '/../phpbb/recursive_dot_prefix_filter_iterator.' . $phpEx; +require __DIR__ . '/../phpbb/iterator/recursive_dot_prefix_filter_iterator.' . $phpEx; switch ($action) { diff --git a/phpBB/includes/acp/acp_language.php b/phpBB/includes/acp/acp_language.php index a647128624..82a3e570e5 100644 --- a/phpBB/includes/acp/acp_language.php +++ b/phpBB/includes/acp/acp_language.php @@ -220,15 +220,7 @@ class acp_language { try { - $iterator = new \RecursiveIteratorIterator( - new \phpbb\recursive_dot_prefix_filter_iterator( - new \RecursiveDirectoryIterator( - $this->phpbb_root_path . 'language/' . $this->config['default_lang'] . '/', - \FilesystemIterator::SKIP_DOTS - ) - ), - \RecursiveIteratorIterator::LEAVES_ONLY - ); + $iterator = new \phpbb\iterator\recursive_path_iterator($this->phpbb_root_path . 'language/' . $this->config['default_lang'] . '/'); } catch (\Exception $e) { @@ -237,7 +229,6 @@ class acp_language foreach ($iterator as $file_info) { - /** @var \RecursiveDirectoryIterator $file_info */ $relative_path = $iterator->getInnerIterator()->getSubPathname(); $relative_path = str_replace(DIRECTORY_SEPARATOR, '/', $relative_path); diff --git a/phpBB/includes/functions_compatibility.php b/phpBB/includes/functions_compatibility.php index 8f9bac5b82..0783035e6c 100644 --- a/phpBB/includes/functions_compatibility.php +++ b/phpBB/includes/functions_compatibility.php @@ -646,14 +646,10 @@ function phpbb_email_hash($email) */ function phpbb_load_extensions_autoloaders($phpbb_root_path) { - $iterator = new \RecursiveIteratorIterator( - new \phpbb\recursive_dot_prefix_filter_iterator( - new \RecursiveDirectoryIterator( - $phpbb_root_path . 'ext/', - \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS - ) - ), - \RecursiveIteratorIterator::SELF_FIRST + $iterator = new \phpbb\iterator\recursive_path_iterator( + $phpbb_root_path . 'ext/', + \RecursiveIteratorIterator::SELF_FIRST, + \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS ); $iterator->setMaxDepth(2); diff --git a/phpBB/phpbb/event/md_exporter.php b/phpBB/phpbb/event/md_exporter.php index af5eed7867..8ddc213020 100644 --- a/phpBB/phpbb/event/md_exporter.php +++ b/phpBB/phpbb/event/md_exporter.php @@ -658,13 +658,8 @@ class md_exporter { try { - $iterator = new \RecursiveIteratorIterator( - new \phpbb\recursive_dot_prefix_filter_iterator( - new \RecursiveDirectoryIterator( - $dir, - \FilesystemIterator::SKIP_DOTS - ) - ), + $iterator = new \phpbb\iterator\recursive_path_iterator( + $dir, \RecursiveIteratorIterator::SELF_FIRST ); } diff --git a/phpBB/phpbb/extension/manager.php b/phpBB/phpbb/extension/manager.php index c012d9a989..0590adfc98 100644 --- a/phpBB/phpbb/extension/manager.php +++ b/phpBB/phpbb/extension/manager.php @@ -380,11 +380,10 @@ class manager return $available; } - $iterator = new \RecursiveIteratorIterator( - new \phpbb\recursive_dot_prefix_filter_iterator( - new \RecursiveDirectoryIterator($this->phpbb_root_path . 'ext/', \FilesystemIterator::NEW_CURRENT_AND_KEY | \FilesystemIterator::FOLLOW_SYMLINKS) - ), - \RecursiveIteratorIterator::SELF_FIRST + $iterator = new \phpbb\iterator\recursive_path_iterator( + $this->phpbb_root_path . 'ext/', + \RecursiveIteratorIterator::SELF_FIRST, + \FilesystemIterator::NEW_CURRENT_AND_KEY | \FilesystemIterator::FOLLOW_SYMLINKS ); $iterator->setMaxDepth(2); diff --git a/phpBB/phpbb/finder/finder.php b/phpBB/phpbb/finder/finder.php index 6b0baef19f..bcd1c355c0 100644 --- a/phpBB/phpbb/finder/finder.php +++ b/phpBB/phpbb/finder/finder.php @@ -486,13 +486,8 @@ class finder if (is_dir($path)) { - $iterator = new \RecursiveIteratorIterator( - new \phpbb\recursive_dot_prefix_filter_iterator( - new \RecursiveDirectoryIterator( - $path, - \FilesystemIterator::SKIP_DOTS - ) - ), + $iterator = new \phpbb\iterator\recursive_path_iterator( + $path, \RecursiveIteratorIterator::SELF_FIRST ); diff --git a/phpBB/phpbb/recursive_dot_prefix_filter_iterator.php b/phpBB/phpbb/iterator/recursive_dot_prefix_filter_iterator.php similarity index 96% rename from phpBB/phpbb/recursive_dot_prefix_filter_iterator.php rename to phpBB/phpbb/iterator/recursive_dot_prefix_filter_iterator.php index 1446551b8b..4549e03e8e 100644 --- a/phpBB/phpbb/recursive_dot_prefix_filter_iterator.php +++ b/phpBB/phpbb/iterator/recursive_dot_prefix_filter_iterator.php @@ -11,7 +11,7 @@ * */ -namespace phpbb; +namespace phpbb\iterator; /** * Class recursive_dot_prefix_filter_iterator diff --git a/phpBB/phpbb/iterator/recursive_path_iterator.php b/phpBB/phpbb/iterator/recursive_path_iterator.php new file mode 100644 index 0000000000..d120c451fd --- /dev/null +++ b/phpBB/phpbb/iterator/recursive_path_iterator.php @@ -0,0 +1,47 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +declare(strict_types=1); + +namespace phpbb\iterator; + +class recursive_path_iterator extends \RecursiveIteratorIterator +{ + /** + * Constructor + * + * @param string $path Path to iterate over + * @param int $mode Iterator mode + * @param int $flags Flags + */ + public function __construct(string $path, int $mode = self::LEAVES_ONLY, int $flags = \FilesystemIterator::SKIP_DOTS) + { + parent::__construct( + new recursive_dot_prefix_filter_iterator(new \RecursiveDirectoryIterator($path, $flags)), + \RecursiveIteratorIterator::SELF_FIRST + ); + } + + /** + * Get inner iterator + * + * @return recursive_dot_prefix_filter_iterator + */ + public function getInnerIterator(): \RecursiveIterator + { + $inner_iterator = parent::getInnerIterator(); + + assert($inner_iterator instanceof recursive_dot_prefix_filter_iterator); + return $inner_iterator; + } +}