Skip to content

Commit bdb8fd5

Browse files
committed
Remove static MimetypeHelper.
Fix issue #4
1 parent 070299d commit bdb8fd5

File tree

3 files changed

+164
-119
lines changed

3 files changed

+164
-119
lines changed

src/ApacheMimetypeHelper.php

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

src/MimetypeHelper.php

Lines changed: 5 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
2-
32
namespace Http\Message\MultipartStream;
43

4+
55
/**
66
* This class helps to find the proper mime types. The source of this file is taken
77
* from Guzzle.
88
*
99
* @author Michael Dowling and contributors to guzzlehttp/psr7
1010
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1111
*/
12-
class MimetypeHelper
12+
interface MimetypeHelper
1313
{
1414
/**
1515
* Determines the mimetype of a file by looking at its extension.
@@ -18,127 +18,14 @@ class MimetypeHelper
1818
*
1919
* @return null|string
2020
*/
21-
public static function getMimetypeFromFilename($filename)
22-
{
23-
return static::getMimetypeFromExtension(pathinfo($filename, PATHINFO_EXTENSION));
24-
}
21+
public function getMimetypeFromFilename($filename);
2522

2623
/**
2724
* Maps a file extensions to a mimetype.
2825
*
2926
* @param string $extension The file extension.
3027
*
3128
* @return string|null
32-
*
33-
* @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
3429
*/
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-
}
144-
}
30+
public function getMimetypeFromExtension($extension);
31+
}

src/MultipartStreamBuilder.php

Lines changed: 30 additions & 1 deletion
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
*/
@@ -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,28 @@ 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+
* @param MimetypeHelper $mimetypeHelper
221+
*
222+
* @return MultipartStreamBuilder
223+
*/
224+
public function setMimetypeHelper($mimetypeHelper)
225+
{
226+
$this->mimetypeHelper = $mimetypeHelper;
227+
228+
return $this;
229+
}
201230
}

0 commit comments

Comments
 (0)