|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maps; |
|
4
|
|
|
|
|
5
|
|
|
use MWException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class for interaction with MappingService objects. |
|
9
|
|
|
* |
|
10
|
|
|
* @since 0.6.6 |
|
11
|
|
|
* |
|
12
|
|
|
* @licence GNU GPL v2+ |
|
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
14
|
|
|
*/ |
|
15
|
|
|
final class MappingServices { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Associative array containing service identifiers as keys and the names |
|
19
|
|
|
* of service classes as values. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $registeredServices = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Associative with service identifiers as keys containing instances of |
|
27
|
|
|
* the mapping service classes. |
|
28
|
|
|
* |
|
29
|
|
|
* Note: This list only contains the instances, so is not to be used for |
|
30
|
|
|
* looping over all available services, as not all of them are guaranteed |
|
31
|
|
|
* to have an instance already, use $registeredServices for this purpose. |
|
32
|
|
|
* |
|
33
|
|
|
* @var MappingService[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $services = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Registers a service class linked to an identifier. |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function registerService( string $serviceIdentifier, string $serviceClassName ) { |
|
41
|
|
|
self::$registeredServices[$serviceIdentifier] = $serviceClassName; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns the instance of a service class. This method takes |
|
46
|
|
|
* care of creating the instance if this is not done yet. |
|
47
|
|
|
* |
|
48
|
|
|
* @throws MWException |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function getServiceInstance( string $serviceIdentifier ): MappingService { |
|
51
|
|
|
if ( !array_key_exists( $serviceIdentifier, self::$services ) ) { |
|
52
|
|
|
if ( array_key_exists( $serviceIdentifier, self::$registeredServices ) ) { |
|
53
|
|
|
$service = new self::$registeredServices[$serviceIdentifier]( $serviceIdentifier ); |
|
54
|
|
|
|
|
55
|
|
|
if ( $service instanceof MappingService ) { |
|
56
|
|
|
self::$services[$serviceIdentifier] = $service; |
|
57
|
|
|
} else { |
|
58
|
|
|
throw new MWException( |
|
59
|
|
|
'The service object linked to service identifier ' . $serviceIdentifier . ' does not implement iMappingService.' |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
} else { |
|
63
|
|
|
throw new MWException( |
|
64
|
|
|
'There is no service object linked to service identifier ' . $serviceIdentifier . '.' |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return self::$services[$serviceIdentifier]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public static function getMainServiceName( string $serviceName ): string { |
|
73
|
|
|
if ( !array_key_exists( $serviceName, self::$services ) ) { |
|
74
|
|
|
foreach ( array_keys( self::$registeredServices ) as $serviceIdentifier ) { |
|
75
|
|
|
$service = self::getServiceInstance( $serviceIdentifier ); |
|
76
|
|
|
|
|
77
|
|
|
if ( $service->hasAlias( $serviceName ) ) { |
|
78
|
|
|
return $service->getName(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $serviceName; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|