MediaWiki master
LeximorphFactory.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Language;
8
11use Psr\Log\LoggerInterface;
12use Wikimedia\Bcp47Code\Bcp47Code;
16
46 public const CONSTRUCTOR_OPTIONS = [
48 ];
49
51 private const CACHE_SIZE = 100;
52
54 private MapCacheLRU $managerCache;
55
57 private MapCacheLRU $providerCache;
58
60 private bool $useLeximorph;
61
67 public function __construct(
68 ServiceOptions $options,
69 private readonly LoggerInterface $logger,
70 ) {
71 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
72 $this->useLeximorph = (bool)$options->get( MainConfigNames::UseLeximorph );
73
74 $this->managerCache = new MapCacheLRU( self::CACHE_SIZE );
75 $this->providerCache = new MapCacheLRU( self::CACHE_SIZE );
76 }
77
84 public function getManager( Bcp47Code $code ): ?Manager {
85 if ( !$this->useLeximorph ) {
86 return null;
87 }
88 $langCode = LanguageCode::bcp47ToInternal( $code );
89
90 return $this->managerCache->getWithSetCallback(
91 $langCode,
92 fn () => new Manager( $langCode, $this->logger )
93 );
94 }
95
102 public function getProvider( Bcp47Code $code ): ?Provider {
103 if ( !$this->useLeximorph ) {
104 return null;
105 }
106 $langCode = LanguageCode::bcp47ToInternal( $code );
107
108 return $this->providerCache->getWithSetCallback(
109 $langCode,
110 fn () => new Provider( $langCode, $this->logger )
111 );
112 }
113}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
static bcp47ToInternal( $code)
Convert standardized BCP 47 codes to the internal names used by MediaWiki and returned by Language::g...
Create and cache Manager and Provider instances.
getManager(Bcp47Code $code)
Get a Manager for the given language code.
__construct(ServiceOptions $options, private readonly LoggerInterface $logger,)
getProvider(Bcp47Code $code)
Get a Provider for the given language code.
A class containing constants representing the names of configuration variables.
const UseLeximorph
Name constant for the UseLeximorph setting, for use with Config::get()
Store key-value entries in a size-limited in-memory LRU cache.