| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use Psr\Log\LoggerInterface; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class MediaHandlerFactory { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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, |
| 46 |
'image/svg' => SvgHandler::class, |
| 47 |
'image/vnd.djvu' => DjVuHandler::class, |
| 48 |
'image/x.djvu' => DjVuHandler::class, |
| 49 |
'image/x-djvu' => DjVuHandler::class, |
| 50 |
'image/jp2' => Jpeg2000Handler::class, |
| 51 |
'image/jpx' => Jpeg2000Handler::class, |
| 52 |
]; |
| 53 |
|
| 54 |
|
| 55 |
private $logger; |
| 56 |
|
| 57 |
|
| 58 |
private $registry; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private $handlers; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function __construct( |
| 72 |
LoggerInterface $logger, |
| 73 |
array $registry |
| 74 |
) { |
| 75 |
$this->logger = $logger; |
| 76 |
$this->registry = $registry + self::CORE_HANDLERS; |
| 77 |
} |
| 78 |
|
| 79 |
protected function getHandlerClass( $type ) { |
| 80 |
return $this->registry[$type] ?? false; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function getHandler( $type ) { |
| 88 |
if ( isset( $this->handlers[$type] ) ) { |
| 89 |
return $this->handlers[$type]; |
| 90 |
} |
| 91 |
|
| 92 |
$class = $this->getHandlerClass( $type ); |
| 93 |
if ( $class !== false ) { |
| 94 |
|
| 95 |
$handler = new $class; |
| 96 |
if ( !$handler->isEnabled() ) { |
| 97 |
$this->logger->debug( |
| 98 |
'{class} is not enabled.', |
| 99 |
[ 'class' => $class ] |
| 100 |
); |
| 101 |
$handler = false; |
| 102 |
} |
| 103 |
} else { |
| 104 |
$this->logger->debug( |
| 105 |
'no handler found for {type}.', |
| 106 |
[ 'type' => $type ] |
| 107 |
); |
| 108 |
$handler = false; |
| 109 |
} |
| 110 |
|
| 111 |
$this->handlers[$type] = $handler; |
| 112 |
return $handler; |
| 113 |
} |
| 114 |
} |