Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| MediaHandlerFactory | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getHandlerClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHandler | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Media-handling base classes and generic functionality. |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | * @ingroup Media |
| 8 | */ |
| 9 | |
| 10 | namespace MediaWiki\Media; |
| 11 | |
| 12 | use Psr\Log\LoggerInterface; |
| 13 | |
| 14 | /** |
| 15 | * Class to construct MediaHandler objects |
| 16 | * |
| 17 | * @since 1.28 |
| 18 | */ |
| 19 | class MediaHandlerFactory { |
| 20 | |
| 21 | /** |
| 22 | * Default, MediaWiki core media handlers |
| 23 | */ |
| 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 | |
| 42 | /** @var LoggerInterface */ |
| 43 | private $logger; |
| 44 | |
| 45 | /** @var array */ |
| 46 | private $registry; |
| 47 | |
| 48 | /** |
| 49 | * Instance cache of MediaHandler objects by mimetype |
| 50 | * |
| 51 | * @var MediaHandler[] |
| 52 | */ |
| 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 | |
| 63 | /** |
| 64 | * @param string $type |
| 65 | * @return class-string<MediaHandler>|false |
| 66 | */ |
| 67 | protected function getHandlerClass( $type ) { |
| 68 | return $this->registry[$type] ?? false; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param string $type mimetype |
| 73 | * @return MediaHandler|false |
| 74 | */ |
| 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 ) { |
| 82 | /** @var MediaHandler $handler */ |
| 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 | |
| 104 | /** @deprecated class alias since 1.46 */ |
| 105 | class_alias( MediaHandlerFactory::class, 'MediaHandlerFactory' ); |