Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TtmServerFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TtmServer;
5
9use TTMServer;
11
19 private $configs;
21 private $default;
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 return $ttmServersIds;
39 }
40
41 public function has( string $name ): bool {
42 $ttmServersIds = $this->getNames();
43 return in_array( $name, $ttmServersIds );
44 }
45
46 public function create( string $name ): TTMServer {
47 if ( !$this->has( $name ) ) {
48 throw new ServiceCreationFailure( "No configuration for name '$name'" );
49 }
50
51 $config = $this->configs[$name];
52 if ( !is_array( $config ) ) {
53 throw new ServiceCreationFailure( "Invalid configuration for name '$name'" );
54 }
55
56 if ( isset( $config['class'] ) ) {
57 $class = $config['class'];
58 return new $class( $config );
59 } elseif ( isset( $config['type'] ) ) {
60 $type = $config['type'];
61 switch ( $type ) {
62 case 'ttmserver':
63 return new DatabaseTTMServer( $config );
64 case 'remote-ttmserver':
65 return new RemoteTTMServer( $config );
66 default:
67 throw new ServiceCreationFailure( "Unknown type for name '$name': $type" );
68 }
69 }
70
71 throw new ServiceCreationFailure( "Invalid configuration for name '$name': type not specified" );
72 }
73
75 public function getDefault(): WritableTTMServer {
76 $service = null;
77
78 try {
79 if ( $this->default !== null ) {
80 $service = $this->create( $this->default );
81 }
82 } catch ( ServiceCreationFailure $e ) {
83 }
84
85 if ( $service instanceof WritableTTMServer ) {
86 return $service;
87 }
88
89 return new FakeTTMServer();
90 }
91}
Mysql based backend.
NO-OP version of TTMServer when it is disabled.
getDefault()
Return the primary service or a no-op fallback if primary cannot be constructed.
__construct(array $configs, ?string $default=null)
Class for handling remote TTMServers over MediaWiki API.
Some general static methods for instantiating TTMServer and helpers.
Definition TTMServer.php:20
Interface for TTMServer that can be updated.