| 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 |
|
| 25 |
namespace MediaWiki\Content; |
| 26 |
|
| 27 |
use InvalidArgumentException; |
| 28 |
use MediaWiki\HookContainer\HookContainer; |
| 29 |
use MediaWiki\HookContainer\HookRunner; |
| 30 |
use MWUnknownContentModelException; |
| 31 |
use Psr\Log\LoggerInterface; |
| 32 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
final class ContentHandlerFactory implements IContentHandlerFactory { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private $handlerSpecs; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private $handlersByModel = []; |
| 51 |
|
| 52 |
|
| 53 |
private $objectFactory; |
| 54 |
|
| 55 |
|
| 56 |
private $hookRunner; |
| 57 |
|
| 58 |
|
| 59 |
private $logger; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function __construct( |
| 73 |
array $handlerSpecs, |
| 74 |
ObjectFactory $objectFactory, |
| 75 |
HookContainer $hookContainer, |
| 76 |
LoggerInterface $logger |
| 77 |
) { |
| 78 |
$this->handlerSpecs = $handlerSpecs; |
| 79 |
$this->objectFactory = $objectFactory; |
| 80 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 81 |
$this->logger = $logger; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function getContentHandler( string $modelID ): ContentHandler { |
| 91 |
if ( empty( $this->handlersByModel[$modelID] ) ) { |
| 92 |
$contentHandler = $this->createForModelID( $modelID ); |
| 93 |
|
| 94 |
$this->logger->info( |
| 95 |
"Registered handler for {$modelID}: " . get_class( $contentHandler ) |
| 96 |
); |
| 97 |
$this->handlersByModel[$modelID] = $contentHandler; |
| 98 |
} |
| 99 |
|
| 100 |
return $this->handlersByModel[$modelID]; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function defineContentHandler( string $modelID, $handlerSpec ): void { |
| 112 |
if ( !is_callable( $handlerSpec ) && !is_string( $handlerSpec ) ) { |
| 113 |
throw new InvalidArgumentException( |
| 114 |
"ContentHandler Spec for modelID '{$modelID}' must be callable or class name" |
| 115 |
); |
| 116 |
} |
| 117 |
unset( $this->handlersByModel[$modelID] ); |
| 118 |
$this->handlerSpecs[$modelID] = $handlerSpec; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function getContentModels(): array { |
| 127 |
$modelsFromHook = []; |
| 128 |
$this->hookRunner->onGetContentModels( $modelsFromHook ); |
| 129 |
$models = array_merge( |
| 130 |
array_keys( $this->handlerSpecs ), |
| 131 |
|
| 132 |
|
| 133 |
array_keys( $this->handlersByModel ), |
| 134 |
|
| 135 |
|
| 136 |
$modelsFromHook ); |
| 137 |
|
| 138 |
return array_unique( $models ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function getAllContentFormats(): array { |
| 145 |
$formats = []; |
| 146 |
foreach ( $this->handlerSpecs as $model => $class ) { |
| 147 |
$formats += array_fill_keys( |
| 148 |
$this->getContentHandler( $model )->getSupportedFormats(), |
| 149 |
true ); |
| 150 |
} |
| 151 |
|
| 152 |
return array_keys( $formats ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function isDefinedModel( string $modelID ): bool { |
| 161 |
return in_array( $modelID, $this->getContentModels(), true ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
private function createForModelID( string $modelID ): ContentHandler { |
| 175 |
$handlerSpec = $this->handlerSpecs[$modelID] ?? null; |
| 176 |
if ( $handlerSpec !== null ) { |
| 177 |
return $this->createContentHandlerFromHandlerSpec( $modelID, $handlerSpec ); |
| 178 |
} |
| 179 |
|
| 180 |
return $this->createContentHandlerFromHook( $modelID ); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private function validateContentHandler( string $modelID, $contentHandler ): void { |
| 190 |
if ( $contentHandler === null ) { |
| 191 |
throw new MWUnknownContentModelException( $modelID ); |
| 192 |
} |
| 193 |
|
| 194 |
if ( !is_object( $contentHandler ) ) { |
| 195 |
throw new InvalidArgumentException( |
| 196 |
"ContentHandler for model {$modelID} wrong: non-object given." |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
if ( !$contentHandler instanceof ContentHandler ) { |
| 201 |
throw new InvalidArgumentException( |
| 202 |
"ContentHandler for model {$modelID} must supply a ContentHandler instance, " |
| 203 |
. get_class( $contentHandler ) . ' given.' |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private function createContentHandlerFromHandlerSpec( |
| 216 |
string $modelID, $handlerSpec |
| 217 |
): ContentHandler { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
$contentHandler = $this->objectFactory->createObject( |
| 222 |
$handlerSpec, |
| 223 |
[ |
| 224 |
'assertClass' => ContentHandler::class, |
| 225 |
'allowCallable' => true, |
| 226 |
'allowClassName' => true, |
| 227 |
'extraArgs' => [ $modelID ], |
| 228 |
] |
| 229 |
); |
| 230 |
|
| 231 |
$this->validateContentHandler( $modelID, $contentHandler ); |
| 232 |
|
| 233 |
return $contentHandler; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private function createContentHandlerFromHook( string $modelID ): ContentHandler { |
| 243 |
$contentHandler = null; |
| 244 |
|
| 245 |
$this->hookRunner->onContentHandlerForModelID( $modelID, $contentHandler ); |
| 246 |
$this->validateContentHandler( $modelID, $contentHandler ); |
| 247 |
|
| 248 |
'@phan-var ContentHandler $contentHandler'; |
| 249 |
|
| 250 |
return $contentHandler; |
| 251 |
} |
| 252 |
} |