| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 | <?phpnamespace League\Flysystem;use League\Flysystem\Util\MimeType;use LogicException;use function strcmp;class Util{    /**     * Get normalized pathinfo.     *     * @param string $path     *     * @return array pathinfo     */    public static function pathinfo($path)    {        $pathinfo = compact('path');        if ('' !== $dirname = dirname($path)) {            $pathinfo['dirname'] = static::normalizeDirname($dirname);        }        $pathinfo['basename'] = static::basename($path);        $pathinfo += pathinfo($pathinfo['basename']);        return $pathinfo + ['dirname' => ''];    }    /**     * Normalize a dirname return value.     *     * @param string $dirname     *     * @return string normalized dirname     */    public static function normalizeDirname($dirname)    {        return $dirname === '.' ? '' : $dirname;    }    /**     * Get a normalized dirname from a path.     *     * @param string $path     *     * @return string dirname     */    public static function dirname($path)    {        return static::normalizeDirname(dirname($path));    }    /**     * Map result arrays.     *     * @param array $object     * @param array $map     *     * @return array mapped result     */    public static function map(array $object, array $map)    {        $result = [];        foreach ($map as $from => $to) {            if ( ! isset($object[$from])) {                continue;            }            $result[$to] = $object[$from];        }        return $result;    }    /**     * Normalize path.     *     * @param string $path     *     * @throws LogicException     *     * @return string     */    public static function normalizePath($path)    {        return static::normalizeRelativePath($path);    }    /**     * Normalize relative directories in a path.     *     * @param string $path     *     * @throws LogicException     *     * @return string     */    public static function normalizeRelativePath($path)    {        $path = str_replace('\\', '/', $path);        $path =  static::removeFunkyWhiteSpace($path);        $parts = [];        foreach (explode('/', $path) as $part) {            switch ($part) {                case '':                case '.':                break;            case '..':                if (empty($parts)) {                    throw new LogicException(                        'Path is outside of the defined root, path: [' . $path . ']'                    );                }                array_pop($parts);                break;            default:                $parts[] = $part;                break;            }        }        $path = implode('/', $parts);        return $path;    }    /**     * Rejects unprintable characters and invalid unicode characters.     *     * @param string $path     *     * @return string $path     */    protected static function removeFunkyWhiteSpace($path)    {        if (preg_match('#\p{C}+#u', $path)) {            throw CorruptedPathDetected::forPath($path);        }        return $path;    }    /**     * Normalize prefix.     *     * @param string $prefix     * @param string $separator     *     * @return string normalized path     */    public static function normalizePrefix($prefix, $separator)    {        return rtrim($prefix, $separator) . $separator;    }    /**     * Get content size.     *     * @param string $contents     *     * @return int content size     */    public static function contentSize($contents)    {        return defined('MB_OVERLOAD_STRING') ? mb_strlen($contents, '8bit') : strlen($contents);    }    /**     * Guess MIME Type based on the path of the file and it's content.     *     * @param string          $path     * @param string|resource $content     *     * @return string|null MIME Type or NULL if no extension detected     */    public static function guessMimeType($path, $content)    {        $mimeType = MimeType::detectByContent($content);        if ( ! (empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm']))) {            return $mimeType;        }        return MimeType::detectByFilename($path);    }    /**     * Emulate directories.     *     * @param array $listing     *     * @return array listing with emulated directories     */    public static function emulateDirectories(array $listing)    {        $directories = [];        $listedDirectories = [];        foreach ($listing as $object) {            [$directories, $listedDirectories] = static::emulateObjectDirectories($object, $directories, $listedDirectories);        }        $directories = array_diff(array_unique($directories), array_unique($listedDirectories));        foreach ($directories as $directory) {            $listing[] = static::pathinfo($directory) + ['type' => 'dir'];        }        return $listing;    }    /**     * Ensure a Config instance.     *     * @param null|array|Config $config     *     * @return Config config instance     *     * @throw  LogicException     */    public static function ensureConfig($config)    {        if ($config === null) {            return new Config();        }        if ($config instanceof Config) {            return $config;        }        if (is_array($config)) {            return new Config($config);        }        throw new LogicException('A config should either be an array or a Flysystem\Config object.');    }    /**     * Rewind a stream.     *     * @param resource $resource     */    public static function rewindStream($resource)    {        if (ftell($resource) !== 0 && static::isSeekableStream($resource)) {            rewind($resource);        }    }    public static function isSeekableStream($resource)    {        $metadata = stream_get_meta_data($resource);        return $metadata['seekable'];    }    /**     * Get the size of a stream.     *     * @param resource $resource     *     * @return int|null stream size     */    public static function getStreamSize($resource)    {        $stat = fstat($resource);        if ( ! is_array($stat) || ! isset($stat['size'])) {            return null;        }        return $stat['size'];    }    /**     * Emulate the directories of a single object.     *     * @param array $object     * @param array $directories     * @param array $listedDirectories     *     * @return array     */    protected static function emulateObjectDirectories(array $object, array $directories, array $listedDirectories)    {        if ($object['type'] === 'dir') {            $listedDirectories[] = $object['path'];        }        if ( ! isset($object['dirname']) || trim($object['dirname']) === '') {            return [$directories, $listedDirectories];        }        $parent = $object['dirname'];        while (isset($parent) && trim($parent) !== '' && ! in_array($parent, $directories)) {            $directories[] = $parent;            $parent = static::dirname($parent);        }        if (isset($object['type']) && $object['type'] === 'dir') {            $listedDirectories[] = $object['path'];            return [$directories, $listedDirectories];        }        return [$directories, $listedDirectories];    }    /**     * Returns the trailing name component of the path.     *     * @param string $path     *     * @return string     */    private static function basename($path)    {        $separators = DIRECTORY_SEPARATOR === '/' ? '/' : '\/';        $path = rtrim($path, $separators);        $basename = preg_replace('#.*?([^' . preg_quote($separators, '#') . ']+$)#', '$1', $path);        if (DIRECTORY_SEPARATOR === '/') {            return $basename;        }        // @codeCoverageIgnoreStart        // Extra Windows path munging. This is tested via AppVeyor, but code        // coverage is not reported.        // Handle relative paths with drive letters. c:file.txt.        while (preg_match('#^[a-zA-Z]{1}:[^\\\/]#', $basename)) {            $basename = substr($basename, 2);        }        // Remove colon for standalone drive letter names.        if (preg_match('#^[a-zA-Z]{1}:$#', $basename)) {            $basename = rtrim($basename, ':');        }        return $basename;        // @codeCoverageIgnoreEnd    }}
 |