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