|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\LaminasFactory; |
|
6
|
|
|
|
|
7
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
|
8
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
|
9
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
10
|
|
|
use Psr\Container\ContainerInterface; |
|
11
|
|
|
|
|
12
|
|
|
trait ServiceOptionsProviderTrait |
|
13
|
|
|
{ |
|
14
|
|
|
use ApplicationOptionsProviderTrait; |
|
15
|
|
|
|
|
16
|
|
|
private string $serviceOptionsKey = 'services'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return array<mixed> |
|
20
|
|
|
* |
|
21
|
|
|
* @throws ServiceNotCreatedException |
|
22
|
|
|
* @throws ServiceNotFoundException |
|
23
|
|
|
* @throws ContainerExceptionInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getServiceOptions(ContainerInterface $container, string $requestedName, ?string $key = null): array |
|
26
|
|
|
{ |
|
27
|
|
|
$applicationOptions = $this->getApplicationOptions($container); |
|
28
|
|
|
$serviceOptionsKey = $this->getServiceOptionsKey($key); |
|
29
|
|
|
|
|
30
|
|
|
if (!array_key_exists($serviceOptionsKey, $applicationOptions)) { |
|
31
|
|
|
throw new ServiceNotCreatedException( |
|
32
|
|
|
sprintf( |
|
33
|
|
|
'Unable to find a configuration key for service of type \'%s\' while creating service \'%s\'.', |
|
34
|
|
|
$serviceOptionsKey, |
|
35
|
|
|
$requestedName |
|
36
|
|
|
) |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @phpstan-ignore-next-line Right side of && is always true */ |
|
41
|
|
|
$serviceOptions = (isset($this->factoryOptions) && is_array($this->factoryOptions)) |
|
42
|
|
|
? $this->factoryOptions |
|
43
|
|
|
: []; |
|
44
|
|
|
|
|
45
|
|
|
if (isset($applicationOptions[$serviceOptionsKey][$requestedName])) { |
|
46
|
|
|
if (!is_array($applicationOptions[$serviceOptionsKey][$requestedName])) { |
|
47
|
|
|
throw new ServiceNotCreatedException( |
|
48
|
|
|
sprintf( |
|
49
|
|
|
'The configuration options must be of type \'array\'; \'%s\' provided for service \'%s\'.', |
|
50
|
|
|
gettype($applicationOptions[$serviceOptionsKey][$requestedName]), |
|
51
|
|
|
$requestedName |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
$serviceOptions = array_replace_recursive( |
|
56
|
|
|
$serviceOptions, |
|
57
|
|
|
$applicationOptions[$serviceOptionsKey][$requestedName] |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $serviceOptions; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function getServiceOptionsKey(?string $key): string |
|
65
|
|
|
{ |
|
66
|
|
|
return $key ?? $this->serviceOptionsKey; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|