public static function sortByDependencies($extensions) {
        $sorted = array();
        $unsorted = array();
        foreach ($extensions as $ext) {
            $unsorted[$ext->getId()] = array_keys($ext->getDependencies());
        }
        while (!empty($unsorted)) {
            $before = count($unsorted);
            foreach (array_keys($unsorted) as $id) {
                $missing = array_diff($unsorted[$id], $sorted);
                if (empty($missing)) {
                    $sorted[] = $id;
                    unset($unsorted[$id]);
                }
            }
            if (count($unsorted) == $before) {
                $notfound = array_diff($missing, array_keys($unsorted));
                if (!empty($notfound)) {
                    throw new common_exception_Error('Missing extensions '.implode(',', $notfound).' for: '.implode(',', array_keys($unsorted)));
                } else {
                    throw new common_exception_Error('Cyclic extension dependencies for: '.implode(',', array_keys($unsorted)));
                }
            }
        }
        
        $returnValue = array();
        foreach ($sorted as $id) {
            foreach ($extensions as $ext) {
                if ($ext->getId() == $id) {
                    $returnValue[$id] = $ext;
                }
            }
        }
        return $returnValue;
    }
public static function sortByDependencies($extensions) {
        $sorted = array();
        $unsorted = array();
        foreach ($extensions as $ext) {
            $unsorted[$ext->getId()] = array_keys($ext->getDependencies());
        }
        while (!empty($unsorted)) {
            $before = count($unsorted);
            foreach (array_keys($unsorted) as $id) {
                $missing = array_diff($unsorted[$id], $sorted);
                if (empty($missing)) {
                    $sorted[] = $id;
                    unset($unsorted[$id]);
                }
            }
            if (count($unsorted) == $before) {
                $notfound = array_diff($missing, array_keys($unsorted));
                if (!empty($notfound)) {
                    throw new common_exception_Error('Missing extensions '.implode(',', $notfound).' for: '.implode(',', array_keys($unsorted)));
                } else {
                    throw new common_exception_Error('Cyclic extension dependencies for: '.implode(',', array_keys($unsorted)));
                }
            }
        }
        
        $returnValue = array();
        foreach ($sorted as $id) {
            foreach ($extensions as $ext) {
                if ($ext->getId() == $id) {
                    $returnValue[$id] = $ext;
                }
            }
        }
        return $returnValue;
    }
public static function sortByDependencies ( $ extensions ) { $ sorted = array ( ) ; $ unsorted = array ( ) ; foreach ( $ extensions as $ ext ) { $ unsorted [ $ ext -> getId ( ) ] = array_keys ( $ ext -> getDependencies ( ) ) ; } while ( ! empty ( $ unsorted ) ) { $ before = count ( $ unsorted ) ; foreach ( array_keys ( $ unsorted ) as $ id ) { $ missing = array_diff ( $ unsorted [ $ id ] , $ sorted ) ; if ( empty ( $ missing ) ) { $ sorted [ ] = $ id ; unset ( $ unsorted [ $ id ] ) ; } } if ( count ( $ unsorted ) == $ before ) { $ notfound = array_diff ( $ missing , array_keys ( $ unsorted ) ) ; if ( ! empty ( $ notfound ) ) { throw new common_exception_Error ( 'Missing extensions ' . implode ( ',' , $ notfound ) . ' for: ' . implode ( ',' , array_keys ( $ unsorted ) ) ) ; } else { throw new common_exception_Error ( 'Cyclic extension dependencies for: ' . implode ( ',' , array_keys ( $ unsorted ) ) ) ; } } } $ returnValue = array ( ) ; foreach ( $ sorted as $ id ) { foreach ( $ extensions as $ ext ) { if ( $ ext -> getId ( ) == $ id ) { $ returnValue [ $ id ] = $ ext ; } } } return $ returnValue ; }
Sorts a list of extensions by dependencies,
starting with independent extensions

@param array $extensions
@throws common_exception_Error
@return array
Sorts a list of extensions by dependencies starting with independent extensions