From 6cee91b678006e538b79f58f433a74189533e4cf Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Tue, 11 May 2021 14:40:19 +0300 Subject: [PATCH] introduce JEDCheckerHelper::findManifests to get "real" manifest files, sorted by depth --- .../com_jedchecker/libraries/helper.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/administrator/components/com_jedchecker/libraries/helper.php b/administrator/components/com_jedchecker/libraries/helper.php index 6251e04..26d71c3 100644 --- a/administrator/components/com_jedchecker/libraries/helper.php +++ b/administrator/components/com_jedchecker/libraries/helper.php @@ -23,6 +23,71 @@ abstract class JEDCheckerHelper const CLEAN_COMMENTS = 2; const CLEAN_STRINGS = 4; + /** + * Returns XML manifest files in the package (sorted by depth) + * + * @param string $basedir Extension's directory + * + * @return string[] + * @since 3.0 + */ + public static function findManifests($basedir) + { + // Find all XML files of the extension + $files = JFolder::files($basedir, '\.xml$', true, true); + + $excludeList = array(); + + foreach ($files as $file) + { + $xml = simplexml_load_file($file); + + if (!$xml || ($xml->getName() !== 'extension' && $xml->getName() !== 'install')) + { + // Exclude non-install-manifest XML files + $excludeList[] = $file; + } + elseif ((string) $xml['type'] === 'component' && isset($xml->administration->files['folder'])) + { + // Exclude possible duplicates of manifest in components + $excludeList[] = dirname($file) . '/' . trim($xml->administration->files['folder'], ' /') . '/' . basename($file); + } + elseif ((string) $xml['type'] === 'file' && isset($xml->fileset->files)) + { + // Exclude possible duplicates of file-type extension manifest + foreach ($xml->fileset->files as $child) + { + if (isset($child['folder'])) + { + $excludeList[] = dirname($file) . '/' . trim($child['folder'], ' /') . '/' . basename($file); + } + } + } + } + + $files = array_diff($files, $excludeList); + usort($files, array(__CLASS__, 'sortPathsCmp')); + + return $files; + } + + /** + * Sort directories by depth + * + * @param string $path1 1st path to compare + * @param string $path2 2nd path to compare + * + * @return integer + * @since 3.0 + */ + public static function sortPathsCmp($path1, $path2) + { + $depth1 = substr_count($path1, '/'); + $depth2 = substr_count($path2, '/'); + + return ($depth1 === $depth2) ? strcmp($path1, $path2) : ($depth1 - $depth2); + } + /** * Split text into lines *