Skip to content

Commit d8c6e00

Browse files
authored
Remove static MimetypeHelper. (#5)
Remove static MimetypeHelper.
1 parent 070299d commit d8c6e00

File tree

3 files changed

+175
-123
lines changed

3 files changed

+175
-123
lines changed

src/ApacheMimetypeHelper.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace Http\Message\MultipartStream;
4+
5+
/**
6+
* This class helps to find the proper mime types. The source of this file is taken
7+
* from Guzzle.
8+
*
9+
* @author Michael Dowling and contributors to guzzlehttp/psr7
10+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
11+
*/
12+
class ApacheMimetypeHelper implements MimetypeHelper
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*
17+
* @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
18+
*/
19+
public function getMimetypeFromFilename($filename)
20+
{
21+
return $this->getMimetypeFromExtension(pathinfo($filename, PATHINFO_EXTENSION));
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*
27+
* @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
28+
*/
29+
public function getMimetypeFromExtension($extension)
30+
{
31+
static $mimetypes = [
32+
'7z' => 'application/x-7z-compressed',
33+
'aac' => 'audio/x-aac',
34+
'ai' => 'application/postscript',
35+
'aif' => 'audio/x-aiff',
36+
'asc' => 'text/plain',
37+
'asf' => 'video/x-ms-asf',
38+
'atom' => 'application/atom+xml',
39+
'avi' => 'video/x-msvideo',
40+
'bmp' => 'image/bmp',
41+
'bz2' => 'application/x-bzip2',
42+
'cer' => 'application/pkix-cert',
43+
'crl' => 'application/pkix-crl',
44+
'crt' => 'application/x-x509-ca-cert',
45+
'css' => 'text/css',
46+
'csv' => 'text/csv',
47+
'cu' => 'application/cu-seeme',
48+
'deb' => 'application/x-debian-package',
49+
'doc' => 'application/msword',
50+
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
51+
'dvi' => 'application/x-dvi',
52+
'eot' => 'application/vnd.ms-fontobject',
53+
'eps' => 'application/postscript',
54+
'epub' => 'application/epub+zip',
55+
'etx' => 'text/x-setext',
56+
'flac' => 'audio/flac',
57+
'flv' => 'video/x-flv',
58+
'gif' => 'image/gif',
59+
'gz' => 'application/gzip',
60+
'htm' => 'text/html',
61+
'html' => 'text/html',
62+
'ico' => 'image/x-icon',
63+
'ics' => 'text/calendar',
64+
'ini' => 'text/plain',
65+
'iso' => 'application/x-iso9660-image',
66+
'jar' => 'application/java-archive',
67+
'jpe' => 'image/jpeg',
68+
'jpeg' => 'image/jpeg',
69+
'jpg' => 'image/jpeg',
70+
'js' => 'text/javascript',
71+
'json' => 'application/json',
72+
'latex' => 'application/x-latex',
73+
'log' => 'text/plain',
74+
'm4a' => 'audio/mp4',
75+
'm4v' => 'video/mp4',
76+
'mid' => 'audio/midi',
77+
'midi' => 'audio/midi',
78+
'mov' => 'video/quicktime',
79+
'mp3' => 'audio/mpeg',
80+
'mp4' => 'video/mp4',
81+
'mp4a' => 'audio/mp4',
82+
'mp4v' => 'video/mp4',
83+
'mpe' => 'video/mpeg',
84+
'mpeg' => 'video/mpeg',
85+
'mpg' => 'video/mpeg',
86+
'mpg4' => 'video/mp4',
87+
'oga' => 'audio/ogg',
88+
'ogg' => 'audio/ogg',
89+
'ogv' => 'video/ogg',
90+
'ogx' => 'application/ogg',
91+
'pbm' => 'image/x-portable-bitmap',
92+
'pdf' => 'application/pdf',
93+
'pgm' => 'image/x-portable-graymap',
94+
'png' => 'image/png',
95+
'pnm' => 'image/x-portable-anymap',
96+
'ppm' => 'image/x-portable-pixmap',
97+
'ppt' => 'application/vnd.ms-powerpoint',
98+
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
99+
'ps' => 'application/postscript',
100+
'qt' => 'video/quicktime',
101+
'rar' => 'application/x-rar-compressed',
102+
'ras' => 'image/x-cmu-raster',
103+
'rss' => 'application/rss+xml',
104+
'rtf' => 'application/rtf',
105+
'sgm' => 'text/sgml',
106+
'sgml' => 'text/sgml',
107+
'svg' => 'image/svg+xml',
108+
'swf' => 'application/x-shockwave-flash',
109+
'tar' => 'application/x-tar',
110+
'tif' => 'image/tiff',
111+
'tiff' => 'image/tiff',
112+
'torrent' => 'application/x-bittorrent',
113+
'ttf' => 'application/x-font-ttf',
114+
'txt' => 'text/plain',
115+
'wav' => 'audio/x-wav',
116+
'webm' => 'video/webm',
117+
'wma' => 'audio/x-ms-wma',
118+
'wmv' => 'video/x-ms-wmv',
119+
'woff' => 'application/x-font-woff',
120+
'wsdl' => 'application/wsdl+xml',
121+
'xbm' => 'image/x-xbitmap',
122+
'xls' => 'application/vnd.ms-excel',
123+
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
124+
'xml' => 'application/xml',
125+
'xpm' => 'image/x-xpixmap',
126+
'xwd' => 'image/x-xwindowdump',
127+
'yaml' => 'text/yaml',
128+
'yml' => 'text/yaml',
129+
'zip' => 'application/zip',
130+
];
131+
132+
$extension = strtolower($extension);
133+
134+
return isset($mimetypes[$extension])
135+
? $mimetypes[$extension]
136+
: null;
137+
}
138+
}

src/MimetypeHelper.php

Lines changed: 4 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
namespace Http\Message\MultipartStream;
44

55
/**
6-
* This class helps to find the proper mime types. The source of this file is taken
7-
* from Guzzle.
8-
*
9-
* @author Michael Dowling and contributors to guzzlehttp/psr7
106
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
117
*/
12-
class MimetypeHelper
8+
interface MimetypeHelper
139
{
1410
/**
1511
* Determines the mimetype of a file by looking at its extension.
@@ -18,127 +14,14 @@ class MimetypeHelper
1814
*
1915
* @return null|string
2016
*/
21-
public static function getMimetypeFromFilename($filename)
22-
{
23-
return static::getMimetypeFromExtension(pathinfo($filename, PATHINFO_EXTENSION));
24-
}
17+
public function getMimetypeFromFilename($filename);
2518

2619
/**
2720
* Maps a file extensions to a mimetype.
2821
*
29-
* @param string $extension The file extension.
22+
* @param string $extension The file extension
3023
*
3124
* @return string|null
32-
*
33-
* @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
3425
*/
35-
public static function getMimetypeFromExtension($extension)
36-
{
37-
static $mimetypes = [
38-
'7z' => 'application/x-7z-compressed',
39-
'aac' => 'audio/x-aac',
40-
'ai' => 'application/postscript',
41-
'aif' => 'audio/x-aiff',
42-
'asc' => 'text/plain',
43-
'asf' => 'video/x-ms-asf',
44-
'atom' => 'application/atom+xml',
45-
'avi' => 'video/x-msvideo',
46-
'bmp' => 'image/bmp',
47-
'bz2' => 'application/x-bzip2',
48-
'cer' => 'application/pkix-cert',
49-
'crl' => 'application/pkix-crl',
50-
'crt' => 'application/x-x509-ca-cert',
51-
'css' => 'text/css',
52-
'csv' => 'text/csv',
53-
'cu' => 'application/cu-seeme',
54-
'deb' => 'application/x-debian-package',
55-
'doc' => 'application/msword',
56-
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
57-
'dvi' => 'application/x-dvi',
58-
'eot' => 'application/vnd.ms-fontobject',
59-
'eps' => 'application/postscript',
60-
'epub' => 'application/epub+zip',
61-
'etx' => 'text/x-setext',
62-
'flac' => 'audio/flac',
63-
'flv' => 'video/x-flv',
64-
'gif' => 'image/gif',
65-
'gz' => 'application/gzip',
66-
'htm' => 'text/html',
67-
'html' => 'text/html',
68-
'ico' => 'image/x-icon',
69-
'ics' => 'text/calendar',
70-
'ini' => 'text/plain',
71-
'iso' => 'application/x-iso9660-image',
72-
'jar' => 'application/java-archive',
73-
'jpe' => 'image/jpeg',
74-
'jpeg' => 'image/jpeg',
75-
'jpg' => 'image/jpeg',
76-
'js' => 'text/javascript',
77-
'json' => 'application/json',
78-
'latex' => 'application/x-latex',
79-
'log' => 'text/plain',
80-
'm4a' => 'audio/mp4',
81-
'm4v' => 'video/mp4',
82-
'mid' => 'audio/midi',
83-
'midi' => 'audio/midi',
84-
'mov' => 'video/quicktime',
85-
'mp3' => 'audio/mpeg',
86-
'mp4' => 'video/mp4',
87-
'mp4a' => 'audio/mp4',
88-
'mp4v' => 'video/mp4',
89-
'mpe' => 'video/mpeg',
90-
'mpeg' => 'video/mpeg',
91-
'mpg' => 'video/mpeg',
92-
'mpg4' => 'video/mp4',
93-
'oga' => 'audio/ogg',
94-
'ogg' => 'audio/ogg',
95-
'ogv' => 'video/ogg',
96-
'ogx' => 'application/ogg',
97-
'pbm' => 'image/x-portable-bitmap',
98-
'pdf' => 'application/pdf',
99-
'pgm' => 'image/x-portable-graymap',
100-
'png' => 'image/png',
101-
'pnm' => 'image/x-portable-anymap',
102-
'ppm' => 'image/x-portable-pixmap',
103-
'ppt' => 'application/vnd.ms-powerpoint',
104-
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
105-
'ps' => 'application/postscript',
106-
'qt' => 'video/quicktime',
107-
'rar' => 'application/x-rar-compressed',
108-
'ras' => 'image/x-cmu-raster',
109-
'rss' => 'application/rss+xml',
110-
'rtf' => 'application/rtf',
111-
'sgm' => 'text/sgml',
112-
'sgml' => 'text/sgml',
113-
'svg' => 'image/svg+xml',
114-
'swf' => 'application/x-shockwave-flash',
115-
'tar' => 'application/x-tar',
116-
'tif' => 'image/tiff',
117-
'tiff' => 'image/tiff',
118-
'torrent' => 'application/x-bittorrent',
119-
'ttf' => 'application/x-font-ttf',
120-
'txt' => 'text/plain',
121-
'wav' => 'audio/x-wav',
122-
'webm' => 'video/webm',
123-
'wma' => 'audio/x-ms-wma',
124-
'wmv' => 'video/x-ms-wmv',
125-
'woff' => 'application/x-font-woff',
126-
'wsdl' => 'application/wsdl+xml',
127-
'xbm' => 'image/x-xbitmap',
128-
'xls' => 'application/vnd.ms-excel',
129-
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
130-
'xml' => 'application/xml',
131-
'xpm' => 'image/x-xpixmap',
132-
'xwd' => 'image/x-xwindowdump',
133-
'yaml' => 'text/yaml',
134-
'yml' => 'text/yaml',
135-
'zip' => 'application/zip',
136-
];
137-
138-
$extension = strtolower($extension);
139-
140-
return isset($mimetypes[$extension])
141-
? $mimetypes[$extension]
142-
: null;
143-
}
26+
public function getMimetypeFromExtension($extension);
14427
}

src/MultipartStreamBuilder.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class MultipartStreamBuilder
2020
*/
2121
private $streamFactory;
2222

23+
/**
24+
* @var MimetypeHelper
25+
*/
26+
private $mimetypeHelper;
27+
2328
/**
2429
* @var string
2530
*/
@@ -49,7 +54,7 @@ public function __construct(StreamFactory $streamFactory = null)
4954
* @var array $headers additional headers ['header-name' => 'header-value']
5055
* @var string $filename
5156
* }
52-
*
57+
*
5358
* @return MultipartStreamBuilder
5459
*/
5560
public function addResource($name, $resource, array $options = [])
@@ -130,7 +135,7 @@ private function prepareHeaders($name, StreamInterface $stream, $filename, array
130135

131136
// Set a default Content-Type if one was not provided
132137
if (!$this->hasHeader($headers, 'content-type') && $hasFilename) {
133-
if ($type = MimetypeHelper::getMimetypeFromFilename($filename)) {
138+
if ($type = $this->getMimetypeHelper()->getMimetypeFromFilename($filename)) {
134139
$headers['Content-Type'] = $type;
135140
}
136141
}
@@ -198,4 +203,30 @@ public function setBoundary($boundary)
198203

199204
return $this;
200205
}
206+
207+
/**
208+
* @return MimetypeHelper
209+
*/
210+
private function getMimetypeHelper()
211+
{
212+
if ($this->mimetypeHelper === null) {
213+
$this->mimetypeHelper = new ApacheMimetypeHelper();
214+
}
215+
216+
return $this->mimetypeHelper;
217+
}
218+
219+
/**
220+
* If you have custom file extension you may overwrite the default MimetypeHelper with your own.
221+
*
222+
* @param MimetypeHelper $mimetypeHelper
223+
*
224+
* @return MultipartStreamBuilder
225+
*/
226+
public function setMimetypeHelper(MimetypeHelper $mimetypeHelper)
227+
{
228+
$this->mimetypeHelper = $mimetypeHelper;
229+
230+
return $this;
231+
}
201232
}

0 commit comments

Comments
 (0)