读取序列化文件

@param string $file
@param string $format 文件格式，如果未指定，使用文件后缀判断格式
@return array
332233
public static function loadFile($file, $format = null)
    {
        if (!isset($format)) {
            $ext = pathinfo($file, PATHINFO_EXTENSION);
            $format = ArrayHelper::fetch(self::$FORMATS, $ext);
            if (!isset($format)) {
                throw new \InvalidArgumentException("Cannot infer format from file '{$file}'");
            }
        }
        if ($format == 'json') {
            return json_decode(file_get_contents($file), true);
        } elseif ($format == 'yaml') {
            return Yaml::parse(file_get_contents($file));
        } elseif ($format == 'php') {
            return require($file);
        } else {
            throw new \InvalidArgumentException("Invalid format '{$format}'");
        }
    }
674980
private static function getLinks(string $file): array
    {
        $links    = [];
        $contents = (string)file_get_contents($file);
        if (false !== preg_match_all(
            '/\[(.+?)\].*?\((.+?)\)/',
            $contents,
            $matches,
            PREG_SET_ORDER
        )) {
            $links = array_merge($links, $matches);
        }

        return $links;
    }
112429
public function getHeaders($filename, $format = self::FORMAT_GUZZLE) {

        $rawHeaders = $this->getFile($filename);

        preg_match_all('/(.+):\ (.+)/i', $rawHeaders, $matches);

        // Curl format
        if($format == self::FORMAT_CURL) return $matches[0];

        // Guzzle format
        $headers = array();

        foreach($matches[0] as $i => $matche) {
            $headers[$matches[1][$i]] = $matches[2][$i];
        }

        return $headers;
    }
951390
private function parseXml($file)
    {
        $formats = [];

        // load xml file
        $xmlDoc = $this->tryLoad($file);

        $this->xpath = new \DOMXPath($xmlDoc);
        $this->xpath->registerNamespace('x', static::XML_NAMESPACE_URI);

        foreach ($this->xpath->query('/x:formats/x:format') as $formatNode) {
            $this->addFormatFromFormatNode($formatNode, $formats);
        }

        return $formats;
    }
138278
protected static function convertFile($file)
    {
        $error = $file->getError();
        $tmpName = '';
        if ($error === UPLOAD_ERR_OK) {
            $tmpName = $file->getStream()->getMetadata('uri');
        }

        return [
            'name' => $file->getClientFilename(),
            'type' => $file->getClientMediaType(),
            'tmp_name' => $tmpName,
            'error' => $error,
            'size' => $file->getSize(),
        ];
    }
800209
public function getFiles($file = null)
    {
        if ($file !== null) {
            $return = array();
            foreach ($this->_files as $name => $content) {
                if ($name === $file) {
                    $return[$file] = $this->_files[$name];
                }

                if ($content['name'] === $file) {
                    $return[$name] = $this->_files[$name];
                }
            }

            if (count($return) === 0) {
                require_once 'Zend/Validate/Exception.php';
                throw new Zend_Validate_Exception("The file '$file' was not found");
            }

            return $return;
        }

        return $this->_files;
    }
947387
public function parseCsv($file)
    {
        $handle = fopen($file, 'r');

        if (!is_resource($handle)) {
            return array();
        }

        $content = array();

        while (($data = fgetcsv($handle)) !== false) {
            $content[] = $data;
        }

        fclose($handle);
        return $content;
    }
951510
public function load($file, $range = '') {
        //$range = $range ?: $this->range;
        //if (!isset($this->lang[$range])) {
        //    $this->lang[$range] = [];
        //}

        // 批量定义
        if (is_string($file)) {
            $file = [$file];
        }

        $lang = [];

        foreach ($file as $_file) {
            if (is_file($_file)) {
                // 记录加载信息
                $_lang = include $_file;
                if (is_array($_lang)) {
                    $lang = $_lang+ $lang;
                }
            }
        }

        if(!$lang) {
            return false;
        }

        if($range) {
            if (!isset($this->range[$range])) {
                $this->range[$range] = [];
            }
            $this->range[$range] = $lang + $this->range[$range];
        }
        else {
            $this->lang = $lang + $this->lang;
        }
    }
638532
public static function get_data_from_file( $file, $single = false ) {

		$path = ABSPATH;

		// Detect path if it is set in the file param.
		if ( false !== strpos( $file, '/' ) ) {
			$path = dirname( $file );
			$file = basename( $file );
		}

		$format = 'json';

		// Detect the export format.
		if ( false !== strpos( $file, '.' ) ) {
			$format = explode( '.', $file );
			$format = end( $format );
		}

		$migrate_data = array(
			'single' => $single,
		);

		$migrate = new self( $format, null, $migrate_data );

		$raw_data = file_get_contents( $file );

		// Handle processing the raw data from the format needed.
		$data = $migrate->parse( $raw_data );

		return $data;

	}
1138718
public static function extractFilesDataFromFilesArray(array $file)
    {
        if (!isset($file['tmp_name'])) {
            return [];
        }
        
        $files = [];
        if (is_array($file['tmp_name'])) {
            foreach ($file['tmp_name'] as $index => $value) {
                // skip empty or none exsting tmp file names
                if (!isset($file['tmp_name'][$index]) || empty($file['tmp_name'][$index])) {
                    continue;
                }
                // create files structure array
                $files[] = [
                    'name' => $file['name'][$index],
                    'type' => $file['type'][$index],
                    'tmp_name' => $file['tmp_name'][$index],
                    'error' => $file['error'][$index],
                    'size' => $file['size'][$index],
                ];
            }
        } elseif (isset($file['tmp_name']) && !empty($file['tmp_name'])) {
            $files[] = [
                'name' => $file['name'],
                'type' => $file['type'],
                'tmp_name' => $file['tmp_name'],
                'error' => $file['error'],
                'size' => $file['size'],
            ];
        }
        
        return $files;
    }
430347
public function getCode($format)
    {
        foreach ($this->codes as $code) {
            if ($code->getFormat() == $format) {
                return $code;
            }
        }
    }
1223888
protected function getExportOption($key, $format)
    {
        if ('1.2.xliff' !== $format) {
            return;
        }

        $translate = true;

        if (in_array(
            $key,
            $this->seoAttributes
        )) {
            $translate = false;
        }

        return [
            'translate' => $translate,
        ];
    }
787694
public static function getFormattedOrphans($format = 'ini')
    {
        $formatter = StructureHelper::getFormatClass($format);

        $returns = [];
        $options = [];

        switch (strtolower($format)) {
            case 'ini':
                $orphans = static::getAutoHandledOrphans();

                foreach ($orphans as $key => $value) {
                    $key2 = explode('.', $key);

                    if (isset($key2[1])) {
                        $returns[$key2[1]][$key] = $value;

                        continue;
                    }

                    $returns[$key] = $value;
                }

                break;

            case 'yaml':
            case 'yml':
                $options['inline'] = 99;
            // No break
            default:
                $orphans = static::getAutoHandledOrphans(false);
                $returns = $orphans;
        }

        return $formatter::structToString($returns, $options);
    }
368707
protected function Read($file)
    {
        # Open File
        $fh = fopen($file, 'r');
        $fileSize = filesize($file);
        
        if(!empty($fileSize)) {
            # Read Data
            $data = fread($fh, $fileSize);
            
            # Fix Data Stream to be the exact same format as PHP's strings
            $data = str_replace(array("\r\n", "\r", "\n"), "\n", $data);
        } else {
            $data = '';
        }
        
        # Close File
        fclose($fh);
        
        # Return Data
        return $data;
    }
1209394
public function read($file)
    {
        $file = '/settings/' . $file;

        /**
         * @todo validate that $file is a string
         */

        $result = [];

        if ($this->source_filesystem->has($file . '.yml')) {

            $result = $this->yaml->parse($this->source_filesystem->read($file . '.yml'));
        }

        return $result;
    }
218036
public function fileinfo($file, $language = 'de')
    {
        if (!$file) {
            return [];
        }

        $parts = explode(';', $file);
        $fileId = $parts[0];

        $volume = $this->volumeManager->getByFileId($fileId);
        $file = $volume->findFile($fileId);

        if (!$file) {
            return [];
        }

        $attributes = $file->getAttributes();

        $info = [
            'name' => $file->getName(),
            'mimetype' => $file->getMimeType(),
            'mediaCategory' => $file->getMediaCategory(),
            'mediaType' => $file->getMediaType(),
            'size' => $file->getSize(),
            'attributes' => !empty($attributes['fileattributes']) ? $attributes['fileattributes'] : array(),
            'createdAt' => $file->getCreatedAt(),
            'modifiedAt' => $file->getModifiedAt(),
            'meta' => [],
        ];

        $metasets = $this->metaSetResolver->resolve($file);
        $meta = array();
        foreach ($metasets as $metaset) {
            $metadata = $this->metaDataManager->findByMetaSetAndFile($metaset, $file);
            $data = [];
            foreach ($metaset->getFields() as $field) {
                $value = '';
                if ($metadata) {
                    $value = $metadata->get($field->getName(), $language);
                }
                $data[$field->getName()] = $value;
            }
            $meta[$metaset->getName()] = $data;
        }

        $info['meta'] = new MetaSetAccessor($meta);

        return $info;
    }
963786
public function loadFile($file, $format = 'ini', $package = null)
    {
        $config = $this->config;

        $format = $format ?: $config->get('language.format', 'ini');

        $default = $config['language.default'] ?: 'en-GB';
        $locale  = $config['language.locale'] ?: 'en-GB';

        $ext = $format == 'yaml' ? 'yml' : $format;

        $default = LanguageNormalize::toLanguageTag($default);
        $locale  = LanguageNormalize::toLanguageTag($locale);

        // If package name exists, we load package language first, that global can override it.
        if (is_string($package)) {
            $package = $this->packageResolver->getPackage($package);
        }

        if ($package instanceof AbstractPackage) {
            $path = $package->getDir() . '/Resources/language/%s/%s.%s';

            // Get Package language
            if (!$config->get('language.debug') || $locale == $default) {
                $this->loadLanguageFile(sprintf($path, $default, $file, $ext), $format);
            }

            // If locale not equals default locale, load it to override default
            if ($locale != $default) {
                $this->loadLanguageFile(sprintf($path, $locale, $file, $format), $format);
            }
        }

        // Get Global language
        $path = $config->get('path.languages') . '/%s/%s.%s';

        if (!$config->get('language.debug') || $locale == $default) {
            $this->loadLanguageFile(sprintf($path, $default, $file, $format), $format);
        }

        // If locale not equals default locale, load it to override default
        if ($locale != $default) {
            $this->loadLanguageFile(sprintf($path, $locale, $file, $format), $format);
        }

        return $this;
    }