Get the format of a file from its extension

@param SplFileInfo $file The file

@return string The format
540001
public static function getFormat(SplFileInfo $file)
    {
        $name = $file->getRelativePathname();
        $ext  = substr($name, strrpos($name, '.') + 1);

        switch ($ext) {
            case 'md':
                return 'markdown';

            case 'yml':
                return 'yaml';

            default:
                return $ext;
        }
    }
408751
private function getClassFullNameFromFile(SplFileInfo $file)
    {
        $targets = [
            realpath($this->namespaceDirectory).'/',
            '.php',
        ];

        $replaceValues = ['', ''];

        $className = str_replace($targets, $replaceValues, realpath($file->getPathname()));
        $className = str_replace('/', "\\", $className);

        return $this->namespace."\\".$className;
    }
669665
public function transform(SplFileInfo $file)
    {
        try {
            $contentFile = $this->fileToContentFile->transform($file);
            $contentItem = $this->buildContentItem($contentFile);

            return $contentItem;
        } catch (\Exception $e) {
            throw new TransformationFailure($e->getMessage(), 0, $e, $file->getRealPath());
        }
    }
1348252
protected function match(SplFileInfo $file): bool
    {
        return $file->isFile() &&
                $this->hasExtension($file) &&
                $this->instanceOf($file)
        ;
    }
1038361
public function current($file = null)
    {
        $splFileInfo = $file ?: parent::current();

        return $this->transformer->transform($splFileInfo);
    }
492624
protected function normalizeFileInfo(SplFileInfo $file)
    {
        if (! $file->isLink()) {
            $normalized = [
                'type'      => $file->getType(),
                'path'      => $this->getFilePath($file),
                'timestamp' => $file->getMTime()
            ];

            if ($normalized['type'] === 'file') {
                $normalized['size'] = $file->getSize();
            }

            return $normalized;
        }
    }
1174627
public function filter(\SplFileInfo $file)
    {
        try {
            $class = $this->getClassName($file->getPathname());
        } catch (\Exception $e){
            return false;
        }

        if (! @include_once($file->getPathname())) {
            return false;
        }

        $ReflectionClass = new \ReflectionClass($class);

        foreach ($this->instances as $instance) {
            if ($ReflectionClass->isSubclassOf($instance)) {
                return true;
            }

            if ($class === $instance) {
                return true;
            }
        }

        return false;
    }
791686
protected static function _getExtension($file) {
		$pieces = explode('.', $file);
		$ext = strtolower(array_pop($pieces));
		return $ext;
	}
1114309
public static function getFileMimeType($file)
    {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        if ($finfo) {
            $mime_type = finfo_file($finfo, $file);
            finfo_close($finfo);
            return $mime_type;
        }
        return false;
    }
616419
public static function get(
        $file
    ) {
        if (is_string($file)) {
            $file = new \SplFileInfo($file);
        }

        $extension = mb_strtolower($file->getExtension());
        if ('' === $extension) {
            return static::MIME_TYPE_IF_UNKNOWN;
        }

        if (array_key_exists($extension, static::$mimeTypes)) {
            return static::$mimeTypes[$extension];
        }

        if (function_exists('finfo_open') && $file->isFile()) {
            $path = $file->getPath();

            $fileInfo = finfo_open(FILEINFO_MIME);
            $mimeType = finfo_file($fileInfo, $path);
            finfo_close($fileInfo);

            return $mimeType;
        }

        return static::MIME_TYPE_IF_UNKNOWN;
    }
889504
public function getFormatForFileExtension($extension)
    {
        $format = $this->getConfiguredFormat();
        if ($format === static::FORMAT_AUTO) {
            $format = $this->getAutomaticFormatForFileExtension((string) $extension);
        }

        return $format;
    }
1037554
protected function load(SplFileInfo $file)
    {
        // File info
        $this->filename = $file->getBasename();
        $this->sourcePath = $file->getRelativePath();

        // Load the file
        $data = $file->getContents();

        list($content, $meta) = $this->splitContentMeta($data);

        // Parse meta
        $meta = Yaml::parse($meta) ?: [];

        // Parse content
        switch ($file->getExtension()) {
            case 'md':
            case 'markdown':
                $content = Markdown::parse($content);
                $this->type = self::TYPE_MARKDOWN;
                break;
            case 'tx':
            case 'textile':
                $content = Textile::parse($content);
                $this->type = self::TYPE_TEXTILE;
                break;
        }

        // Set content
        $this->content = $content;
        $this->meta = $meta;

        // Ensure local URLs are absolute
        foreach($this->meta as $key=>$value) {
            if (preg_match('/\burl\b|.*_url\b/', $key)) {
                $this->meta[$key] = $this->builder->getUrl($value);
            }
        }

        // Set target
        $this->setTarget($file);

        // Pagination enabled
        $this->paginate = isset($this->meta['paginate']);

        // Get parent page
        if ($root = dirname(dirname($this->target))) {
            if ($root !== DIRECTORY_SEPARATOR) {
                $this->parentId = ltrim($root, '/');
            }
        }

        // Set URL
        $this->url = '/' . trim(str_replace([DIRECTORY_SEPARATOR, '//'], ['/', '/'], $this->target), '/');

        // Remove "index.html" from the end, this provides a cleaner URL
        if (substr($this->url, -10) === 'index.html') {
            $this->url = substr($this->url, 0, -10);
        }

        // Set basic values
        $this->id = trim($this->url, '/') ?: 'root';
        $this->title = $this->get('title');
        $this->url = $this->builder->getUrl($this->url);

        // Set Description
        if ($this->has('description')) {
            $this->description = $this->get('description');
        }
        else {
            $this->description = $this->getDescription();
        }
    }
615444
protected function parseArchive(\SplFileInfo $file): \SplFileInfo
    {
        $archive = (new ZipFile())->openFile(
            $file instanceof \SplTempFileObject ? $this->generateTempFile($file) : $file->getRealPath()
        );
        $this->correctArchiveFilenamesEncoding($archive);
        $this->flattenArchive($archive);
        $this->filenames = $this->correctArchiveFilenames($archive);
        
        $tempDirectoryPath = (new GenericDictionaryParser())->generateTempDirectory();
        $archive->extractTo($tempDirectoryPath);
        $archive->close();

        $validator = new \esperecyan\dictionary_php\Validator();
        $validator->setLogger($this->logger);
        foreach (new \FilesystemIterator(
            $tempDirectoryPath,
            \FilesystemIterator::KEY_AS_FILENAME
        ) as $filename => $file) {
            if ($file->getExtension() === 'txt') {
                if (isset($quizFile)) {
                    throw new SyntaxException(_('拡張子が「.txt」のファイルが2つ以上含まれています。'));
                }
                if (!in_array(
                    (new Finfo(FILEINFO_MIME_TYPE))->file($file),
                    ['text/plain', 'text/csv']
                )) {
                    throw new SyntaxException(
                        sprintf(_('「%s」は通常のテキストファイルとして認識できません。'), $filename)
                    );
                }
                $quizFile = $file;
            }
        }

        if (empty($quizFile)) {
            throw new SyntaxException(_('拡張子が「.txt」のファイルが見つかりません。'));
        }
        
        return $quizFile;
    }
470931
protected function load(SplFileInfo $file)
    {
        $path = $file->getPathName();

        if (!isset($this->cache['contents'][$path])) {
            $data    = $this->decoder->decode($file->getContents(), static::getFormat($file));
            $context = ['file' => $file, 'data' => $data];

            if (is_array($data)) {
                foreach ($this->handlers as $property => $handler) {
                    if ($handler->isSupported($data)) {
                        $value = isset($data[$property]) ? $data[$property] : null;
                        $data[$property] = $handler->handle($value, $context);
                    }
                }
            }

            $this->cache['contents'][$path] = $data;
        }

        return $this->cache['contents'][$path];
    }
836432
protected function getTypeByFile($file)
  {
    switch (strtolower(pathinfo($file, PATHINFO_EXTENSION))) {
      case 'jpg':
      case 'jpeg':
        $type = IMG_JPG;
        break;
      case 'png':
        $type = IMG_PNG;
        break;
      case 'gif':
        $type = IMG_GIF;
        break;
      default:
        $type = false;
    }

    return $type;
  }
860550
public function getMimeType(\SplFileInfo $file)
    {
        $fileInfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimeType = finfo_file($fileInfo, $file->getPathname());
        finfo_close($fileInfo);

        return $mimeType;
    }
594470
public function filter(\SplFileInfo $file)
    {
        $content = file_get_contents($file);
        $result = preg_match_all('/class\s+([^\s|\{]+)[\s\{]/im', $content, $results, PREG_PATTERN_ORDER);
        unset($content);

        if (!$result) {
            return false;
        }

        if (count($results[0]) > 1) {
            return false;
        }

        return true;
    }