Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Attributes | List of all members
Hoa\Mime\Mime Class Reference
Inheritance diagram for Hoa\Mime\Mime:

Public Member Functions

 __construct (Stream $stream, Array $parameters=[])
 
 getParameters ()
 
 getExtension ()
 
 getOtherExtensions ()
 
 getMime ()
 
 getMedia ()
 
 getType ()
 
 isExperimental ()
 
 isVendor ()
 

Static Public Member Functions

static compute ($magic=null)
 
static extensionExists ($extension)
 
static getExtensionsFromMime ($mime)
 
static getMimeFromExtension ($extension)
 
static parseMime ($mime)
 

Public Attributes

const STRUCTURE_MEDIA_TYPE = 0
 
const STRUCTURE_EXTENSION = 1
 
const MIME_MEDIA = 0
 
const MIME_TYPE = 1
 

Protected Member Functions

 _find (Stream $stream)
 

Protected Attributes

 $_extension = null
 
 $_mime = null
 
 $_media = null
 
 $_type = null
 

Static Protected Attributes

static $_computed = null
 

Private Attributes

 $_parameters = null
 

Detailed Description

Class .

Get informations relative to MIME (media, type, extension, other extensions) about a stream.

Definition at line 51 of file Mime.php.

Constructor & Destructor Documentation

Hoa\Mime\Mime::__construct ( Stream  $stream,
Array  $parameters = [] 
)

Get informations relative to MIME about a stream.

Parameters
\Hoa\Stream$streamStream to study.
array$parametersParameters.
Returns
void
Exceptions

Definition at line 144 of file Mime.php.

145  {
146  $this->_parameters = new Core\Parameter(
147  $this,
148  [],
149  [
150  'magic' => null
151  ]
152  );
153  $this->_parameters->setParameters($parameters);
154  $magic = $this->_parameters->getParameter('magic');
155 
156  if (null === $magic) {
157  $this->_parameters->setParameter(
158  'magic',
159  'hoa://Library/Mime/Mime.types'
160  );
161  $magic = $this->_parameters->getParameter('magic');
162  }
163 
164  if (null === static::$_computed) {
165  static::compute($magic);
166  }
167 
168  $this->_find($stream);
169 
170  return;
171  }
_find(Stream $stream)
Definition: Mime.php:367

Here is the call graph for this function:

Member Function Documentation

Hoa\Mime\Mime::_find ( Stream  $stream)
protected

Find informations about stream.

Parameters
\Hoa\Stream$streamStream to study.
Returns
void
Exceptions

Definition at line 367 of file Mime.php.

368  {
369  $name = $stream->getStreamName();
370 
371  if ($stream instanceof Stream\IStream\Pathable) {
372  $based = $stream->getBasename();
373  } else {
374  $based = basename($name);
375  }
376 
377  if (false === $poos = strrpos($based, '.')) {
378  throw new Exception\MimeIsNotFound(
379  'Cannot find MIME type of %s, because extension is not found.',
380  4,
381  $name
382  );
383  }
384 
385  $this->_extension = substr($based, $poos + 1);
386  $this->_mime = static::getMimeFromExtension($this->_extension);
387 
388  if (null === $this->_mime) {
389  throw new Exception\MimeIsNotFound(
390  'No MIME type associated to the %s extension.',
391  5,
392  $this->_extension
393  );
394  }
395 
396  list($this->_media, $this->_type) = static::parseMime($this->_mime);
397 
398  return;
399  }

Here is the call graph for this function:

Here is the caller graph for this function:

static Hoa\Mime\Mime::compute (   $magic = null)
static

Compute a magic file.

Parameters
string$magicMagic file to compute.
Returns
void
Exceptions

Definition at line 190 of file Mime.php.

191  {
192  if (null === $magic) {
193  $magic = 'hoa://Library/Mime/Mime.types';
194  }
195 
196  if (!file_exists($magic)) {
197  throw new Exception(
198  'Magic file %s does not exist.',
199  0,
200  $magic
201  );
202  }
203 
204  static::$_computed = [
205  self::STRUCTURE_MEDIA_TYPE => [],
206  self::STRUCTURE_EXTENSION => []
207  ];
208  $s_media_type = &static::$_computed[self::STRUCTURE_MEDIA_TYPE];
209  $s_extension = &static::$_computed[self::STRUCTURE_EXTENSION];
210  $splashed = file($magic);
211  $max = count($splashed) - 1;
212 
213  foreach ($splashed as $i => $line) {
214  $line = trim($line);
215 
216  if (empty($line) || '#' == $line[0]) {
217  continue;
218  }
219 
220  if (false === strpos($line, "\t")) {
221  $mime = $line;
222  $extensions = null;
223  } else {
224  list($mime, $extensions) = preg_split('#' . "\t" . '+#', $line);
225  }
226 
227  try {
228  list($media, $type) = static::parseMime($mime);
229  } catch (Exception $exception) {
230  $message = null;
231  $m = min($max, $i + 3);
232  $foo = strlen((string) $m);
233 
234  for (
235  $e = max(0, $i - 3);
236  $e <= $m;
237  ++$e
238  ) {
239  $message .= "\n" . sprintf('%' . $foo . 'd', $e) . '. ';
240 
241  if ($i == $e) {
242  $message .= '➜ ';
243  } else {
244  $message .= ' ';
245  }
246 
247  $message .= trim($splashed[$e]);
248  }
249 
250  throw new Exception(
251  'Magic file %s seems to be corrupted (at line %d). ' .
252  'You should take a look at this piece of code:' .
253  $message,
254  1,
255  [$magic, $i],
256  $exception
257  );
258  }
259 
260  if (!isset($s_media_type[$media])) {
261  $s_media_type[$media] = [];
262  }
263 
264  if (null == $extensions) {
265  $s_media_type[$media][$type] = null;
266 
267  continue;
268  }
269 
270  $extensions = explode(' ', $extensions);
271  $s_media_type[$media][$type] = $extensions;
272 
273  foreach ($extensions as $extension) {
274  $s_extension[$extension] = $mime;
275  }
276  }
277 
278  return;
279  }
static Hoa\Mime\Mime::extensionExists (   $extension)
static

Check if extension exists in the magic file.

Parameters
string$extensionExtension to check.
Returns
bool

Definition at line 287 of file Mime.php.

288  {
289  if (null === static::$_computed) {
290  static::compute();
291  }
292 
293  return isset(static::$_computed[self::STRUCTURE_EXTENSION][$extension]);
294  }
Hoa\Mime\Mime::getExtension ( )

Get extension.

Returns
string

Definition at line 406 of file Mime.php.

407  {
408  return $this->_extension;
409  }

Here is the caller graph for this function:

static Hoa\Mime\Mime::getExtensionsFromMime (   $mime)
static

Get extensions from a MIME (media/type).

Parameters
string$mimeMIME.
Returns
array
Exceptions

Definition at line 303 of file Mime.php.

304  {
305  if (null === static::$_computed) {
306  static::compute();
307  }
308 
309  list($media, $type) = static::parseMime($mime);
310 
311  if (!isset(static::$_computed[self::STRUCTURE_MEDIA_TYPE][$media][$type])) {
312  throw new Exception\MimeIsNotFound(
313  'MIME type %s does not exist.',
314  2,
315  $mime
316  );
317  }
318 
319  return static::$_computed[self::STRUCTURE_MEDIA_TYPE][$media][$type];
320  }
Hoa\Mime\Mime::getMedia ( )

Get media.

Returns
string

Definition at line 450 of file Mime.php.

451  {
452  return $this->_media;
453  }

Here is the caller graph for this function:

Hoa\Mime\Mime::getMime ( )

Get MIME (media/type).

Returns
string

Definition at line 440 of file Mime.php.

441  {
442  return $this->_mime;
443  }
static Hoa\Mime\Mime::getMimeFromExtension (   $extension)
static

Get MIME (media/type) from extension.

Parameters
string$extensionExtension to considere.
Returns
string

Definition at line 328 of file Mime.php.

329  {
330  $extension = strtolower($extension);
331 
332  if (false === static::extensionExists($extension)) {
333  return null;
334  }
335 
336  return static::$_computed[self::STRUCTURE_EXTENSION][$extension];
337  }
Hoa\Mime\Mime::getOtherExtensions ( )

Get other extensions (associated to the same MIME).

Returns
array

Definition at line 416 of file Mime.php.

417  {
418  $out = [];
419  $current = $this->getExtension();
420  $others =
421  self::$_computed
422  [self::STRUCTURE_MEDIA_TYPE]
423  [$this->getMedia()]
424  [$this->getType()];
425 
426  foreach ($others as $other) {
427  if ($other !== $current) {
428  $out[] = $other;
429  }
430  }
431 
432  return $out;
433  }
getExtension()
Definition: Mime.php:406

Here is the call graph for this function:

Hoa\Mime\Mime::getParameters ( )

Get parameters.

Returns

Implements Hoa\Core\Parameter\Parameterizable.

Definition at line 178 of file Mime.php.

179  {
180  return $this->_parameters;
181  }
Hoa\Mime\Mime::getType ( )

Get type.

Returns
string

Definition at line 460 of file Mime.php.

461  {
462  return $this->_type;
463  }

Here is the caller graph for this function:

Hoa\Mime\Mime::isExperimental ( )

Check if the MIME is experimental or not.

Returns
bool

Definition at line 470 of file Mime.php.

471  {
472  return 'x-' === substr($this->getType(), 0, 2);
473  }

Here is the call graph for this function:

Hoa\Mime\Mime::isVendor ( )

Check if the MIME is a vendor's one or not.

Returns
bool

Definition at line 480 of file Mime.php.

481  {
482  return 'vnd.' === substr($this->getType(), 0, 4);
483  }

Here is the call graph for this function:

static Hoa\Mime\Mime::parseMime (   $mime)
static

Parse MIME, i.e. extract media and type. Please, see self::MIME_* constants as result array index.

Parameters
string$mimeMIME to parse.
Returns
array
Exceptions

Definition at line 347 of file Mime.php.

348  {
349  if (false === strpos($mime, '/')) {
350  throw new Exception(
351  'MIME %s is not well-formed (media/type).',
352  3,
353  $mime
354  );
355  }
356 
357  return explode('/', $mime);
358  }

Member Data Documentation

Hoa\Mime\Mime::$_computed = null
staticprotected

Definition at line 104 of file Mime.php.

Hoa\Mime\Mime::$_extension = null
protected

Definition at line 111 of file Mime.php.

Hoa\Mime\Mime::$_media = null
protected

Definition at line 125 of file Mime.php.

Hoa\Mime\Mime::$_mime = null
protected

Definition at line 118 of file Mime.php.

Hoa\Mime\Mime::$_parameters = null
private

Definition at line 86 of file Mime.php.

Hoa\Mime\Mime::$_type = null
protected

Definition at line 132 of file Mime.php.

const Hoa\Mime\Mime::MIME_MEDIA = 0

Parse: media index.

int

Definition at line 72 of file Mime.php.

const Hoa\Mime\Mime::MIME_TYPE = 1

Parse: type index.

int

Definition at line 79 of file Mime.php.

const Hoa\Mime\Mime::STRUCTURE_EXTENSION = 1

Structure: extension part.

int

Definition at line 65 of file Mime.php.

const Hoa\Mime\Mime::STRUCTURE_MEDIA_TYPE = 0

Structure: media/type part.

int

Definition at line 58 of file Mime.php.


The documentation for this class was generated from the following file: