15 private const TTMSERVER_CLASSES = [
16 ReadableTtmServer::class,
17 WritableTtmServer::class,
18 SearchableTtmServer::class
23 private readonly array $configs,
24 private readonly ?
string $default =
null,
31 foreach ( $this->configs as $serviceId => $config ) {
32 $type = $config[
'type'] ??
'';
33 if ( $type ===
'ttmserver' || $type ===
'remote-ttmserver' ) {
34 $ttmServersIds[] = $serviceId;
40 $serviceClass = $config[
'class'] ??
null;
41 if ( $serviceClass !==
null ) {
42 foreach ( self::TTMSERVER_CLASSES as $ttmClass ) {
43 if ( $serviceClass instanceof $ttmClass ) {
44 $ttmServersIds[] = $serviceId;
50 return $ttmServersIds;
53 public function has(
string $name ): bool {
54 $ttmServersIds = $this->getNames();
55 return in_array( $name, $ttmServersIds );
58 public function create(
string $name ): TtmServer {
59 if ( !$this->has( $name ) ) {
60 throw new ServiceCreationFailure(
"No configuration for name '$name'" );
63 $config = $this->configs[$name];
64 if ( !is_array( $config ) ) {
65 throw new ServiceCreationFailure(
"Invalid configuration for name '$name'" );
68 if ( isset( $config[
'class'] ) ) {
69 $class = $config[
'class'];
72 if ( in_array( $class, [ DatabaseTtmServer::class,
'DatabaseTTMServer',
'DatabaseTtmServer' ] ) ) {
73 return new DatabaseTtmServer( $config );
76 return new $class( $config );
77 } elseif ( isset( $config[
'type'] ) ) {
78 $type = $config[
'type'];
81 return new DatabaseTtmServer( $config );
82 case 'remote-ttmserver':
83 return new RemoteTTMServer( $config );
85 throw new ServiceCreationFailure(
"Unknown type for name '$name': $type" );
89 throw new ServiceCreationFailure(
"Invalid configuration for name '$name': type not specified" );
92 public function getDefaultForQuerying(): ReadableTtmServer {
93 if ( $this->default === null ) {
94 return new FakeTtmServer();
97 if ( $this->configs[ $this->
default ][
'writable' ] ??
false ) {
98 throw new InvalidArgumentException(
99 "Default TTM service {$this->default} cannot be write only"
103 $service = $this->create( $this->
default );
105 if ( $service instanceof ReadableTtmServer ) {
109 throw new InvalidArgumentException(
110 "Default TTM service {$this->default} must implement ReadableTtmServer."
119 $writableServers = $readOnlyServers = [];
120 $ttmServerIds = $this->getNames();
122 foreach ( $ttmServerIds as $serverId ) {
123 $isWritable = $this->configs[ $serverId ][
'writable' ] ??
null;
126 if ( $serverId === $this->
default ) {
127 throw new InvalidArgumentException(
128 "Default TTM server {$this->default} cannot be write only"
132 $server = $this->create( $serverId );
134 throw new InvalidArgumentException(
135 "Server '$serverId' marked writable does not implement WritableTtmServer interface"
138 $writableServers[ $serverId ] = $server;
139 } elseif ( $isWritable ===
false ) {
140 $readOnlyServers[] = $serverId;
144 if ( $writableServers ) {
145 return $writableServers;
149 if ( $this->
default ) {
150 $defaultTtmServer = $this->create( $this->
default );
152 if ( $defaultTtmServer instanceof WritableTtmServer ) {
153 if ( !in_array( $this->
default, $readOnlyServers ) ) {
154 $writableServers[ $this->default ] = $defaultTtmServer;
158 if ( $writableServers ) {
159 return $writableServers;
169 $ttmServerIds = $this->getNames();
170 $writableServers = [];
171 foreach ( $ttmServerIds as $serverId ) {
172 if ( $this->configs[ $serverId ][
'writable' ] ??
false ) {
173 $server = $this->create( $serverId );
175 throw new \InvalidArgumentException(
176 "Server '$serverId' marked writable does not implement WritableTtmServer interface"
179 $writableServers[ $serverId ] = $server;
183 return $writableServers;