Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Zip.php

function Zip::locateFile

Find a file by name, returning the one that has the shortest path.

Throws

\RuntimeException

1 call to Zip::locateFile()
Zip::getComposerJson in vendor/composer/composer/src/Composer/Util/Zip.php
Gets content of the root composer.json inside a ZIP archive.

File

vendor/composer/composer/src/Composer/Util/Zip.php, line 60

Class

Zip
@author Andreas Schempp <andreas.schempp@terminal42.ch>

Namespace

Composer\Util

Code

private static function locateFile(\ZipArchive $zip, string $filename) : int {
    // return root composer.json if it is there and is a file
    if (false !== ($index = $zip->locateName($filename)) && $zip->getFromIndex($index) !== false) {
        return $index;
    }
    $topLevelPaths = [];
    for ($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);
        $dirname = dirname($name);
        // ignore OSX specific resource fork folder
        if (strpos($name, '__MACOSX') !== false) {
            continue;
        }
        // handle archives with proper TOC
        if ($dirname === '.') {
            $topLevelPaths[$name] = true;
            if (\count($topLevelPaths) > 1) {
                throw new \RuntimeException('Archive has more than one top level directories, and no composer.json was found on the top level, so it\'s an invalid archive. Top level paths found were: ' . implode(',', array_keys($topLevelPaths)));
            }
            continue;
        }
        // handle archives which do not have a TOC record for the directory itself
        if (false === strpos($dirname, '\\') && false === strpos($dirname, '/')) {
            $topLevelPaths[$dirname . '/'] = true;
            if (\count($topLevelPaths) > 1) {
                throw new \RuntimeException('Archive has more than one top level directories, and no composer.json was found on the top level, so it\'s an invalid archive. Top level paths found were: ' . implode(',', array_keys($topLevelPaths)));
            }
        }
    }
    if ($topLevelPaths && false !== ($index = $zip->locateName(key($topLevelPaths) . $filename)) && $zip->getFromIndex($index) !== false) {
        return $index;
    }
    throw new \RuntimeException('No composer.json found either at the top level or within the topmost directory');
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal