MediaWiki master
MediaHandlerFactory.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Media;
11
12use Psr\Log\LoggerInterface;
13
20
24 private const CORE_HANDLERS = [
25 'image/jpeg' => JpegHandler::class,
26 'image/png' => PNGHandler::class,
27 'image/gif' => GIFHandler::class,
28 'image/tiff' => TiffHandler::class,
29 'image/webp' => WebPHandler::class,
30 'image/x-ms-bmp' => BmpHandler::class,
31 'image/x-bmp' => BmpHandler::class,
32 'image/x-xcf' => XCFHandler::class,
33 'image/svg+xml' => SvgHandler::class, // official
34 'image/svg' => SvgHandler::class, // compat
35 'image/vnd.djvu' => DjVuHandler::class, // official
36 'image/x.djvu' => DjVuHandler::class, // compat
37 'image/x-djvu' => DjVuHandler::class, // compat
38 'image/jp2' => Jpeg2000Handler::class,
39 'image/jpx' => Jpeg2000Handler::class,
40 ];
41
43 private $logger;
44
46 private $registry;
47
53 private $handlers;
54
55 public function __construct(
56 LoggerInterface $logger,
57 array $registry
58 ) {
59 $this->logger = $logger;
60 $this->registry = $registry + self::CORE_HANDLERS;
61 }
62
67 protected function getHandlerClass( $type ) {
68 return $this->registry[$type] ?? false;
69 }
70
75 public function getHandler( $type ) {
76 if ( isset( $this->handlers[$type] ) ) {
77 return $this->handlers[$type];
78 }
79
80 $class = $this->getHandlerClass( $type );
81 if ( $class !== false ) {
83 $handler = new $class;
84 if ( !$handler->isEnabled() ) {
85 $this->logger->debug(
86 '{class} is not enabled.',
87 [ 'class' => $class ]
88 );
89 $handler = false;
90 }
91 } else {
92 $this->logger->debug(
93 'no handler found for {type}.',
94 [ 'type' => $type ]
95 );
96 $handler = false;
97 }
98
99 $this->handlers[$type] = $handler;
100 return $handler;
101 }
102}
103
105class_alias( MediaHandlerFactory::class, 'MediaHandlerFactory' );
Class to construct MediaHandler objects.
__construct(LoggerInterface $logger, array $registry)
Base media handler class.