Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaHandlerFactory
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getHandlerClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHandler
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Media-handling base classes and generic functionality.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24use Psr\Log\LoggerInterface;
25
26/**
27 * Class to construct MediaHandler objects
28 *
29 * @since 1.28
30 */
31class MediaHandlerFactory {
32
33    /**
34     * Default, MediaWiki core media handlers
35     *
36     * @var array
37     */
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
56    /** @var LoggerInterface */
57    private $logger;
58
59    /** @var array */
60    private $registry;
61
62    /**
63     * Instance cache of MediaHandler objects by mimetype
64     *
65     * @var MediaHandler[]
66     */
67    private $handlers;
68
69    /**
70     * @param LoggerInterface $logger
71     * @param array $registry
72     */
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
85    /**
86     * @param string $type mimetype
87     * @return MediaHandler|false
88     */
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 ) {
96            /** @var MediaHandler $handler */
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}