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