Puli\Repository\JsonRepository::flattenWithFilter PHP Method

flattenWithFilter() private method

This method takes a two-level reference array as returned by {@link searchReferences()}. The references are scanned for Puli paths matching the given regular expression. Those matches are returned. If a matching path refers to more than one reference, the first reference is returned in the resulting array. All references that contain directory paths may be traversed recursively and scanned for more paths matching the regular expression. This recursive traversal can be limited by passing a $maxDepth (see {@link getPathDepth()}). By default, this $maxDepth is equal to zero (no recursive scan). The flag STOP_ON_FIRST may be used to stop the search at the first result. The flag NO_SEARCH_FILESYSTEM may be used to check for whether the found paths actually exist on the filesystem. Each reference returned by this method can be: * null * a link starting with @ * an absolute filesystem path The keys of the returned array are Puli paths. Their order is undefined.
private flattenWithFilter ( array $references, string $regex, integer $flags, integer $maxDepth ) : string[] | null[]
$references array A two-level reference array as returned by {@link searchReferences()}.
$regex string A regular expression used to filter Puli paths.
$flags integer A bitwise combination of the flag constants in this class.
$maxDepth integer The maximum path depth when searching the contents of directory references. If 0, the depth is unlimited.
return string[] | null[] A one-level array of references with Puli paths as keys.
    private function flattenWithFilter(array $references, $regex, $flags = 0, $maxDepth = 0)
    {
        $result = array();
        foreach ($references as $currentPath => $currentReferences) {
            // Check whether the current entry matches the pattern
            if (!isset($result[$currentPath]) && preg_match($regex, $currentPath)) {
                // If yes, the first stored reference is returned
                $result[$currentPath] = reset($currentReferences);
                if ($flags & self::STOP_ON_FIRST) {
                    return $result;
                }
            }
            if ($flags & self::NO_SEARCH_FILESYSTEM) {
                continue;
            }
            // First follow any links before we check which of them is a directory
            $currentReferences = $this->followLinks($currentReferences);
            $currentPath = rtrim($currentPath, '/');
            // Search the nested entries if desired
            foreach ($currentReferences as $baseFilesystemPath) {
                // Ignore null values and file paths
                if (!is_dir($baseFilesystemPath)) {
                    continue;
                }
                $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseFilesystemPath, RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
                if (0 !== $maxDepth) {
                    $currentDepth = $this->getPathDepth($currentPath);
                    $maxIteratorDepth = $maxDepth - $currentDepth;
                    if ($maxIteratorDepth < 1) {
                        continue;
                    }
                    $iterator->setMaxDepth($maxIteratorDepth);
                }
                $basePathLength = strlen($baseFilesystemPath);
                foreach ($iterator as $nestedFilesystemPath) {
                    $nestedPath = substr_replace($nestedFilesystemPath, $currentPath, 0, $basePathLength);
                    if (!isset($result[$nestedPath]) && preg_match($regex, $nestedPath)) {
                        $result[$nestedPath] = $nestedFilesystemPath;
                        if ($flags & self::STOP_ON_FIRST) {
                            return $result;
                        }
                    }
                }
            }
        }
        return $result;
    }