protected function rename_extracted_rootdir($dirname, $rootdir, array $files) {

        if (!is_dir($dirname)) {
            $this->debug('Unable to rename rootdir of non-existing content');
            return $files;
        }

        if (file_exists($dirname.'/'.$rootdir)) {
            // This typically means the real root dir already has the $rootdir name.
            return $files;
        }

        $found = null; // The name of the first subdirectory under the $dirname.
        foreach (scandir($dirname) as $item) {
            if (substr($item, 0, 1) === '.') {
                continue;
            }
            if (is_dir($dirname.'/'.$item)) {
                if ($found !== null and $found !== $item) {
                    // Multiple directories found.
                    throw new moodle_exception('unexpected_archive_structure', 'core_plugin');
                }
                $found = $item;
            }
        }

        if (!is_null($found)) {
            if (rename($dirname.'/'.$found, $dirname.'/'.$rootdir)) {
                $newfiles = array();
                foreach ($files as $filepath => $status) {
                    $newpath = preg_replace('~^'.preg_quote($found.'/').'~', preg_quote($rootdir.'/'), $filepath);
                    $newfiles[$newpath] = $status;
                }
                return $newfiles;
            }
        }

        return $files;
    }
protected function rename_extracted_rootdir($dirname, $rootdir, array $files) {

        if (!is_dir($dirname)) {
            $this->debug('Unable to rename rootdir of non-existing content');
            return $files;
        }

        if (file_exists($dirname.'/'.$rootdir)) {
            // This typically means the real root dir already has the $rootdir name.
            return $files;
        }

        $found = null; // The name of the first subdirectory under the $dirname.
        foreach (scandir($dirname) as $item) {
            if (substr($item, 0, 1) === '.') {
                continue;
            }
            if (is_dir($dirname.'/'.$item)) {
                if ($found !== null and $found !== $item) {
                    // Multiple directories found.
                    throw new moodle_exception('unexpected_archive_structure', 'core_plugin');
                }
                $found = $item;
            }
        }

        if (!is_null($found)) {
            if (rename($dirname.'/'.$found, $dirname.'/'.$rootdir)) {
                $newfiles = array();
                foreach ($files as $filepath => $status) {
                    $newpath = preg_replace('~^'.preg_quote($found.'/').'~', preg_quote($rootdir.'/'), $filepath);
                    $newfiles[$newpath] = $status;
                }
                return $newfiles;
            }
        }

        return $files;
    }
protected function rename_extracted_rootdir ( $ dirname , $ rootdir , array $ files ) { if ( ! is_dir ( $ dirname ) ) { $ this -> debug ( 'Unable to rename rootdir of non-existing content' ) ; return $ files ; } if ( file_exists ( $ dirname . '/' . $ rootdir ) ) { // This typically means the real root dir already has the $rootdir name. return $ files ; } $ found = null ; // The name of the first subdirectory under the $dirname. foreach ( scandir ( $ dirname ) as $ item ) { if ( substr ( $ item , 0 , 1 ) === '.' ) { continue ; } if ( is_dir ( $ dirname . '/' . $ item ) ) { if ( $ found !== null and $ found !== $ item ) { // Multiple directories found. throw new moodle_exception ( 'unexpected_archive_structure' , 'core_plugin' ) ; } $ found = $ item ; } } if ( ! is_null ( $ found ) ) { if ( rename ( $ dirname . '/' . $ found , $ dirname . '/' . $ rootdir ) ) { $ newfiles = array ( ) ; foreach ( $ files as $ filepath => $ status ) { $ newpath = preg_replace ( '~^' . preg_quote ( $ found . '/' ) . '~' , preg_quote ( $ rootdir . '/' ) , $ filepath ) ; $ newfiles [ $ newpath ] = $ status ; } return $ newfiles ; } } return $ files ; }
Renames the root directory of the extracted ZIP package.

This internal helper method assumes that the plugin ZIP package has been
extracted into a temporary empty directory so the plugin folder is the
only folder there. The ZIP package is supposed to be validated so that
it contains just a single root folder.

@param string $dirname fullpath location of the extracted ZIP package
@param string $rootdir the requested name of the root directory
@param array $files list of extracted files
@return array eventually amended list of extracted files
Renames the root directory of the extracted ZIP package .