* @copyright 2023 Siddharth VP */ class MultiGadgetRepo extends GadgetRepo { /** * @var GadgetRepo[] */ private array $repos; /** * @param GadgetRepo[] $repos */ public function __construct( array $repos ) { $this->repos = $repos; } /** * @inheritDoc */ public function getGadget( string $id ): Gadget { foreach ( $this->repos as $repo ) { try { return $repo->getGadget( $id ); } catch ( InvalidArgumentException $e ) { // Try next repo } } throw new InvalidArgumentException( "No gadget registered for '$id'" ); } /** * @inheritDoc */ public function getGadgetIds(): array { $ids = []; foreach ( $this->repos as $repo ) { $ids = array_merge( $ids, $repo->getGadgetIds() ); } return array_values( array_unique( $ids ) ); } /** * @inheritDoc */ public function handlePageUpdate( LinkTarget $target ): void { foreach ( $this->repos as $repo ) { $repo->handlePageUpdate( $target ); } } private function getRepoForGadget( string $id ): GadgetRepo { foreach ( $this->repos as $repo ) { try { $repo->getGadget( $id ); // return repo if it didn't throw return $repo; } catch ( InvalidArgumentException $e ) { } } throw new InvalidArgumentException( "No repo found for gadget $id" ); } public function getGadgetDefinitionTitle( string $id ): ?Title { return $this->getRepoForGadget( $id )->getGadgetDefinitionTitle( $id ); } public function titleWithoutPrefix( string $titleText, string $gadgetId ): string { return $this->getRepoForGadget( $gadgetId )->titleWithoutPrefix( $titleText, $gadgetId ); } public function validationWarnings( Gadget $gadget ): array { $duplicateWarnings = $this->isDefinedTwice( $gadget->getName() ) ? [ wfMessage( "gadgets-validate-duplicate", $gadget->getName() ) ] : []; return array_merge( $duplicateWarnings, parent::validationWarnings( $gadget ) ); } /** * Checks if a gadget is defined with the same name in two different repos. * @param string $id Gadget name * @return bool */ private function isDefinedTwice( string $id ) { $found = false; foreach ( $this->repos as $repo ) { try { $repo->getGadget( $id ); if ( $found ) { // found it a second time return true; } else { $found = true; } } catch ( InvalidArgumentException $e ) { } } return false; } }