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