Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TtmServerFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TtmServer;
5
6use InvalidArgumentException;
7
14
15 private const TTMSERVER_CLASSES = [
16 ReadableTtmServer::class,
17 WritableTtmServer::class,
18 SearchableTtmServer::class
19 ];
20
22 public function __construct(
23 private readonly array $configs,
24 private readonly ?string $default = null,
25 ) {
26 }
27
29 public function getNames(): array {
30 $ttmServersIds = [];
31 foreach ( $this->configs as $serviceId => $config ) {
32 $type = $config['type'] ?? '';
33 if ( $type === 'ttmserver' || $type === 'remote-ttmserver' ) {
34 $ttmServersIds[] = $serviceId;
35 }
36
37 // Translation memory configuration may not define a type, in such
38 // cases we determine whether the service is a TTM server using the
39 // interfaces it implements.
40 $serviceClass = $config['class'] ?? null;
41 if ( $serviceClass !== null ) {
42 foreach ( self::TTMSERVER_CLASSES as $ttmClass ) {
43 if ( $serviceClass instanceof $ttmClass ) {
44 $ttmServersIds[] = $serviceId;
45 break;
46 }
47 }
48 }
49 }
50 return $ttmServersIds;
51 }
52
53 public function has( string $name ): bool {
54 $ttmServersIds = $this->getNames();
55 return in_array( $name, $ttmServersIds );
56 }
57
58 public function create( string $name ): TtmServer {
59 if ( !$this->has( $name ) ) {
60 throw new ServiceCreationFailure( "No configuration for name '$name'" );
61 }
62
63 $config = $this->configs[$name];
64 if ( !is_array( $config ) ) {
65 throw new ServiceCreationFailure( "Invalid configuration for name '$name'" );
66 }
67
68 if ( isset( $config['class'] ) ) {
69 $class = $config['class'];
70
71 // TODO: Add a factory to create TTM server instances
72 if ( in_array( $class, [ DatabaseTtmServer::class, 'DatabaseTTMServer', 'DatabaseTtmServer' ] ) ) {
73 return new DatabaseTtmServer( $config );
74 }
75
76 return new $class( $config );
77 } elseif ( isset( $config['type'] ) ) {
78 $type = $config['type'];
79 switch ( $type ) {
80 case 'ttmserver':
81 return new DatabaseTtmServer( $config );
82 case 'remote-ttmserver':
83 return new RemoteTTMServer( $config );
84 default:
85 throw new ServiceCreationFailure( "Unknown type for name '$name': $type" );
86 }
87 }
88
89 throw new ServiceCreationFailure( "Invalid configuration for name '$name': type not specified" );
90 }
91
92 public function getDefaultForQuerying(): ReadableTtmServer {
93 if ( $this->default === null ) {
94 return new FakeTtmServer();
95 }
96
97 if ( $this->configs[ $this->default ][ 'writable' ] ?? false ) {
98 throw new InvalidArgumentException(
99 "Default TTM service {$this->default} cannot be write only"
100 );
101 }
102
103 $service = $this->create( $this->default );
104
105 if ( $service instanceof ReadableTtmServer ) {
106 return $service;
107 }
108
109 throw new InvalidArgumentException(
110 "Default TTM service {$this->default} must implement ReadableTtmServer."
111 );
112 }
113
118 public function getWritable(): array {
119 $writableServers = $readOnlyServers = [];
120 $ttmServerIds = $this->getNames();
121
122 foreach ( $ttmServerIds as $serverId ) {
123 $isWritable = $this->configs[ $serverId ][ 'writable' ] ?? null;
124
125 if ( $isWritable ) {
126 if ( $serverId === $this->default ) {
127 throw new InvalidArgumentException(
128 "Default TTM server {$this->default} cannot be write only"
129 );
130 }
131
132 $server = $this->create( $serverId );
133 if ( !$server instanceof WritableTtmServer ) {
134 throw new InvalidArgumentException(
135 "Server '$serverId' marked writable does not implement WritableTtmServer interface"
136 );
137 }
138 $writableServers[ $serverId ] = $server;
139 } elseif ( $isWritable === false ) {
140 $readOnlyServers[] = $serverId;
141 }
142 }
143
144 if ( $writableServers ) {
145 return $writableServers;
146 }
147
148 // If there are no writable server, check and use the default server
149 if ( $this->default ) {
150 $defaultTtmServer = $this->create( $this->default );
151
152 if ( $defaultTtmServer instanceof WritableTtmServer ) {
153 if ( !in_array( $this->default, $readOnlyServers ) ) {
154 $writableServers[ $this->default ] = $defaultTtmServer;
155 }
156 }
157
158 if ( $writableServers ) {
159 return $writableServers;
160 }
161 }
162
163 // Did not find any writable servers.
164 return [];
165 }
166
168 public function getWriteOnly(): array {
169 $ttmServerIds = $this->getNames();
170 $writableServers = [];
171 foreach ( $ttmServerIds as $serverId ) {
172 if ( $this->configs[ $serverId ][ 'writable' ] ?? false ) {
173 $server = $this->create( $serverId );
174 if ( !$server instanceof WritableTtmServer ) {
175 throw new \InvalidArgumentException(
176 "Server '$serverId' marked writable does not implement WritableTtmServer interface"
177 );
178 }
179 $writableServers[ $serverId ] = $server;
180 }
181 }
182
183 return $writableServers;
184 }
185}
getWritable()
Returns writable servers if configured, else returns the default TtmServer else returns null.
__construct(private readonly array $configs, private readonly ?string $default=null,)
Interface for TtmServer that can be updated.