ICanBoogie/HTTP v2.4.0
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Exception
    • HTTP
      • Dispatcher
      • Headers
      • Request

Classes

  • ICanBoogie\Exception\RescueEvent
  • ICanBoogie\HTTP\CallableDispatcher
  • ICanBoogie\HTTP\Dispatcher
  • ICanBoogie\HTTP\Dispatcher\BeforeDispatchEvent
  • ICanBoogie\HTTP\Dispatcher\DispatchEvent
  • ICanBoogie\HTTP\File
  • ICanBoogie\HTTP\FileInfo
  • ICanBoogie\HTTP\FileList
  • ICanBoogie\HTTP\Headers
  • ICanBoogie\HTTP\Headers\CacheControl
  • ICanBoogie\HTTP\Headers\ContentDisposition
  • ICanBoogie\HTTP\Headers\ContentType
  • ICanBoogie\HTTP\Headers\Date
  • ICanBoogie\HTTP\Headers\Header
  • ICanBoogie\HTTP\Headers\HeaderParameter
  • ICanBoogie\HTTP\Helpers
  • ICanBoogie\HTTP\RedirectResponse
  • ICanBoogie\HTTP\Request
  • ICanBoogie\HTTP\Request\Context
  • ICanBoogie\HTTP\Response
  • ICanBoogie\HTTP\Status
  • ICanBoogie\HTTP\WeightedDispatcher

Interfaces

  • ICanBoogie\HTTP\DispatcherInterface
  • ICanBoogie\HTTP\Exception

Exceptions

  • ICanBoogie\HTTP\DispatcherNotDefined
  • ICanBoogie\HTTP\ForceRedirect
  • ICanBoogie\HTTP\MethodNotSupported
  • ICanBoogie\HTTP\NotFound
  • ICanBoogie\HTTP\ServiceUnavailable
  • ICanBoogie\HTTP\StatusCodeNotValid
 1 <?php
 2 
 3 /*
 4  * This file is part of the ICanBoogie package.
 5  *
 6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
 7  *
 8  * For the full copyright and license information, please view the LICENSE
 9  * file that was distributed with this source code.
10  */
11 
12 namespace ICanBoogie\HTTP;
13 
14 /**
15  * File information.
16  */
17 class FileInfo
18 {
19     static public $types = [
20 
21         '.doc'  => 'application/msword',
22         '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
23         '.gif'  => 'image/gif',
24         '.jpg'  => 'image/jpeg',
25         '.jpeg' => 'image/jpeg',
26         '.js'   => 'application/javascript',
27         '.json' => 'application/json',
28         '.mp3'  => 'audio/mpeg',
29         '.odt'  => 'application/vnd.oasis.opendocument.text',
30         '.pdf'  => 'application/pdf',
31         '.php'  => 'application/x-php',
32         '.png'  => 'image/png',
33         '.psd'  => 'application/psd',
34         '.rar'  => 'application/rar',
35         '.txt'  => 'text/plain',
36         '.zip'  => 'application/zip',
37         '.xls'  => 'application/vnd.ms-excel',
38         '.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
39 
40     ];
41 
42     static public $forced_types = [
43 
44         '.js',
45         '.json',
46         '.php',
47         '.txt'
48 
49     ];
50 
51     static public $types_alias = [
52 
53         'text/x-php' => 'application/x-php'
54 
55     ];
56 
57     /**
58      * Resolves the MIME type of a file.
59      *
60      * @param string $pathname Pathname to the file.
61      * @param string $extension The variable passed by reference receives the extension
62      * of the file.
63      *
64      * @return string The MIME type of the file, or `application/octet-stream` if it could not
65      * be determined.
66      */
67     static public function resolve_type($pathname, &$extension = null)
68     {
69         $extension = '.' . strtolower(pathinfo($pathname, PATHINFO_EXTENSION));
70 
71         if (in_array($extension, self::$forced_types))
72         {
73             return self::$types[$extension];
74         }
75 
76         if (file_exists($pathname) && extension_loaded('fileinfo'))
77         {
78             $fi = new \finfo(FILEINFO_MIME_TYPE);
79             $type = $fi->file($pathname);
80 
81             if ($type)
82             {
83                 return isset(self::$types_alias[$type]) ? self::$types_alias[$type] : $type;
84             }
85         } // @codeCoverageIgnore
86 
87         if (isset(self::$types[$extension]))
88         {
89             return self::$types[$extension];
90         }
91 
92         return 'application/octet-stream'; // @codeCoverageIgnore
93     }
94 }
95 
ICanBoogie/HTTP v2.4.0 API documentation generated by ApiGen