MediaWiki 1.43.1
MediaHandlerFactory.php
Go to the documentation of this file.
1<?php
24use Psr\Log\LoggerInterface;
25
32
36 private const CORE_HANDLERS = [
37 'image/jpeg' => JpegHandler::class,
38 'image/png' => PNGHandler::class,
39 'image/gif' => GIFHandler::class,
40 'image/tiff' => TiffHandler::class,
41 'image/webp' => WebPHandler::class,
42 'image/x-ms-bmp' => BmpHandler::class,
43 'image/x-bmp' => BmpHandler::class,
44 'image/x-xcf' => XCFHandler::class,
45 'image/svg+xml' => SvgHandler::class, // official
46 'image/svg' => SvgHandler::class, // compat
47 'image/vnd.djvu' => DjVuHandler::class, // official
48 'image/x.djvu' => DjVuHandler::class, // compat
49 'image/x-djvu' => DjVuHandler::class, // compat
50 'image/jp2' => Jpeg2000Handler::class,
51 'image/jpx' => Jpeg2000Handler::class,
52 ];
53
55 private $logger;
56
58 private $registry;
59
65 private $handlers;
66
71 public function __construct(
72 LoggerInterface $logger,
73 array $registry
74 ) {
75 $this->logger = $logger;
76 $this->registry = $registry + self::CORE_HANDLERS;
77 }
78
79 protected function getHandlerClass( $type ) {
80 return $this->registry[$type] ?? false;
81 }
82
87 public function getHandler( $type ) {
88 if ( isset( $this->handlers[$type] ) ) {
89 return $this->handlers[$type];
90 }
91
92 $class = $this->getHandlerClass( $type );
93 if ( $class !== false ) {
95 $handler = new $class;
96 if ( !$handler->isEnabled() ) {
97 $this->logger->debug(
98 '{class} is not enabled.',
99 [ 'class' => $class ]
100 );
101 $handler = false;
102 }
103 } else {
104 $this->logger->debug(
105 'no handler found for {type}.',
106 [ 'type' => $type ]
107 );
108 $handler = false;
109 }
110
111 $this->handlers[$type] = $handler;
112 return $handler;
113 }
114}
Class to construct MediaHandler objects.
__construct(LoggerInterface $logger, array $registry)
Base media handler class.