|
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 Laminas\ServiceManager\ServiceLocatorInterface; |
|
10
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
11
|
|
|
use Psr\Container\ContainerInterface; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractFactory implements FactoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use ServiceOptionsProviderTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array<mixed>|null |
|
19
|
|
|
*/ |
|
20
|
|
|
protected ?array $factoryOptions = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param array<mixed>|null $factoryOptions |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(array $factoryOptions = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->factoryOptions = $factoryOptions; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @throws ServiceNotCreatedException |
|
32
|
|
|
* @throws ServiceNotFoundException |
|
33
|
|
|
* @throws ContainerExceptionInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function getService(ContainerInterface $container, mixed $name, string $requestedName): mixed |
|
36
|
|
|
{ |
|
37
|
|
|
// Returning non-string arguments reduces factory logic for options that may have already been resolved |
|
38
|
|
|
if (!is_string($name)) { |
|
39
|
|
|
return $name; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!class_exists($name) && !$container->has($name)) { |
|
43
|
|
|
throw new ServiceNotFoundException( |
|
44
|
|
|
sprintf( |
|
45
|
|
|
'The required \'%s\' dependency could not be found while creating service \'%s\'', |
|
46
|
|
|
$name, |
|
47
|
|
|
$requestedName |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
try { |
|
53
|
|
|
/** @throws \Exception */ |
|
54
|
|
|
return $container->get($name); |
|
55
|
|
|
} catch (ContainerExceptionInterface $e) { |
|
56
|
|
|
throw $e; |
|
57
|
|
|
} catch (\Exception $e) { |
|
58
|
|
|
throw new ServiceNotCreatedException( |
|
59
|
|
|
sprintf( |
|
60
|
|
|
'The required \'%s\' dependency could not be created for service \'%s\'', |
|
61
|
|
|
$name, |
|
62
|
|
|
$requestedName |
|
63
|
|
|
), |
|
64
|
|
|
$e->getCode(), |
|
65
|
|
|
$e |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array<mixed>|null $options |
|
72
|
|
|
* |
|
73
|
|
|
* @throws ServiceNotCreatedException |
|
74
|
|
|
* @throws ContainerExceptionInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function buildService( |
|
77
|
|
|
ServiceLocatorInterface $serviceLocator, |
|
78
|
|
|
string $name, |
|
79
|
|
|
?array $options, |
|
80
|
|
|
string $requestedName |
|
81
|
|
|
): mixed { |
|
82
|
|
|
try { |
|
83
|
|
|
return $serviceLocator->build($name, $options); |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
throw new ServiceNotCreatedException( |
|
86
|
|
|
sprintf( |
|
87
|
|
|
'Failed to build service \'%s\' required as dependency of service \'%s\'', |
|
88
|
|
|
$name, |
|
89
|
|
|
$requestedName |
|
90
|
|
|
), |
|
91
|
|
|
$e->getCode(), |
|
92
|
|
|
$e |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|