MediaWiki master
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
67 public function __construct(
68 LoggerInterface $logger,
69 array $registry
70 ) {
71 $this->logger = $logger;
72 $this->registry = $registry + self::CORE_HANDLERS;
73 }
74
75 protected function getHandlerClass( $type ) {
76 return $this->registry[$type] ?? false;
77 }
78
83 public function getHandler( $type ) {
84 if ( isset( $this->handlers[$type] ) ) {
85 return $this->handlers[$type];
86 }
87
88 $class = $this->getHandlerClass( $type );
89 if ( $class !== false ) {
91 $handler = new $class;
92 if ( !$handler->isEnabled() ) {
93 $this->logger->debug(
94 '{class} is not enabled.',
95 [ 'class' => $class ]
96 );
97 $handler = false;
98 }
99 } else {
100 $this->logger->debug(
101 'no handler found for {type}.',
102 [ 'type' => $type ]
103 );
104 $handler = false;
105 }
106
107 $this->handlers[$type] = $handler;
108 return $handler;
109 }
110}
Class to construct MediaHandler objects.
__construct(LoggerInterface $logger, array $registry)
Base media handler class.