15 private array $configs;
16 private ?
string $default;
17 private const TTMSERVER_CLASSES = [
18 ReadableTtmServer::class,
19 WritableTtmServer::class,
20 SearchableTtmServer::class
24 public function __construct( array $configs, ?
string $default =
null ) {
25 $this->configs = $configs;
26 $this->
default = $default;
32 foreach ( $this->configs as $serviceId => $config ) {
33 $type = $config[
'type'] ??
'';
34 if ( $type ===
'ttmserver' || $type ===
'remote-ttmserver' ) {
35 $ttmServersIds[] = $serviceId;
41 $serviceClass = $config[
'class'] ??
null;
42 if ( $serviceClass !==
null ) {
43 foreach ( self::TTMSERVER_CLASSES as $ttmClass ) {
44 if ( $serviceClass instanceof $ttmClass ) {
45 $ttmServersIds[] = $serviceId;
51 return $ttmServersIds;
54 public function has(
string $name ): bool {
55 $ttmServersIds = $this->getNames();
56 return in_array( $name, $ttmServersIds );
59 public function create(
string $name ):
TTMServer {
60 if ( !$this->has( $name ) ) {
61 throw new ServiceCreationFailure(
"No configuration for name '$name'" );
64 $config = $this->configs[$name];
65 if ( !is_array( $config ) ) {
66 throw new ServiceCreationFailure(
"Invalid configuration for name '$name'" );
69 if ( isset( $config[
'class'] ) ) {
70 $class = $config[
'class'];
73 if ( in_array( $class, [ DatabaseTtmServer::class,
'DatabaseTTMServer',
'DatabaseTtmServer' ] ) ) {
74 return new DatabaseTtmServer( $config );
77 return new $class( $config );
78 } elseif ( isset( $config[
'type'] ) ) {
79 $type = $config[
'type'];
82 return new DatabaseTtmServer( $config );
83 case 'remote-ttmserver':
84 return new RemoteTTMServer( $config );
86 throw new ServiceCreationFailure(
"Unknown type for name '$name': $type" );
90 throw new ServiceCreationFailure(
"Invalid configuration for name '$name': type not specified" );
93 public function getDefaultForQuerying(): ReadableTtmServer {
94 if ( $this->default === null ) {
95 return new FakeTtmServer();
98 if ( $this->configs[ $this->
default ][
'writable' ] ??
false ) {
99 throw new InvalidArgumentException(
100 "Default TTM service {$this->default} cannot be write only"
104 $service = $this->create( $this->
default );
106 if ( $service instanceof ReadableTtmServer ) {
110 throw new InvalidArgumentException(
111 "Default TTM service {$this->default} must implement ReadableTtmServer."
120 $writableServers = $readOnlyServers = [];
121 $ttmServerIds = $this->getNames();
123 foreach ( $ttmServerIds as $serverId ) {
124 $isWritable = $this->configs[ $serverId ][
'writable' ] ??
null;
127 if ( $serverId === $this->
default ) {
128 throw new InvalidArgumentException(
129 "Default TTM server {$this->default} cannot be write only"
133 $server = $this->create( $serverId );
135 throw new InvalidArgumentException(
136 "Server '$serverId' marked writable does not implement WritableTtmServer interface"
139 $writableServers[ $serverId ] = $server;
140 } elseif ( $isWritable ===
false ) {
141 $readOnlyServers[] = $serverId;
145 if ( $writableServers ) {
146 return $writableServers;
150 if ( $this->
default ) {
151 $defaultTtmServer = $this->create( $this->
default );
153 if ( $defaultTtmServer instanceof WritableTtmServer ) {
154 if ( !in_array( $this->
default, $readOnlyServers ) ) {
155 $writableServers[ $this->default ] = $defaultTtmServer;
159 if ( $writableServers ) {
160 return $writableServers;
170 $ttmServerIds = $this->getNames();
171 $writableServers = [];
172 foreach ( $ttmServerIds as $serverId ) {
173 if ( $this->configs[ $serverId ][
'writable' ] ??
false ) {
174 $server = $this->create( $serverId );
176 throw new \InvalidArgumentException(
177 "Server '$serverId' marked writable does not implement WritableTtmServer interface"
180 $writableServers[ $serverId ] = $server;
184 return $writableServers;