Merge "Override MediaHandlers in tests using MediaWikiServices"
[lhc/web/wiklou.git] / includes / media / MediaHandlerFactory.php
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
24 /**
25 * Class to construct MediaHandler objects
26 *
27 * @since 1.28
28 */
29 class MediaHandlerFactory {
30
31 /**
32 * @var MediaHandler[]
33 */
34 private $handlers;
35
36 protected function getHandlerClass( $type ) {
37 global $wgMediaHandlers;
38 if ( isset( $wgMediaHandlers[$type] ) ) {
39 return $wgMediaHandlers[$type];
40 } else {
41 return false;
42 }
43 }
44
45 /**
46 * @param string $type mimetype
47 * @return bool|MediaHandler
48 */
49 public function getHandler( $type ) {
50 if ( isset( $this->handlers[$type] ) ) {
51 return $this->handlers[$type];
52 }
53
54 $class = $this->getHandlerClass( $type );
55 if ( $class !== false ) {
56 /** @var MediaHandler $handler */
57 $handler = new $class;
58 if ( !$handler->isEnabled() ) {
59 wfDebug( __METHOD__ . ": $class is not enabled\n" );
60 $handler = false;
61 }
62 } else {
63 wfDebug( __METHOD__ . ": no handler found for $type.\n" );
64 $handler = false;
65 }
66
67 $this->handlers[$type] = $handler;
68 return $handler;
69 }
70 }