1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1);
|
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasFactory;
|
6
|
|
|
|
7
|
|
|
use Arp\LaminasFactory\Exception\ServiceNotCreatedException;
|
8
|
|
|
use Arp\LaminasFactory\Exception\ServiceNotFoundException;
|
9
|
|
|
use Interop\Container\ContainerInterface;
|
10
|
|
|
use Laminas\ServiceManager\Factory\FactoryInterface;
|
11
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* @author Alex Patterson <[email protected]>
|
15
|
|
|
* @package Arp\LaminasFactory
|
16
|
|
|
*/
|
17
|
|
|
abstract class AbstractFactory implements FactoryInterface
|
18
|
|
|
{
|
19
|
|
|
/**
|
20
|
|
|
* @trait ServiceOptionsProviderTrait
|
21
|
|
|
*/
|
22
|
|
|
use ServiceOptionsProviderTrait;
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* @var array
|
26
|
|
|
*/
|
27
|
|
|
protected $factoryOptions = [];
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @param array $factoryOptions
|
31
|
|
|
*/
|
32
|
|
|
public function __construct(array $factoryOptions = [])
|
33
|
|
|
{
|
34
|
|
|
$this->factoryOptions = $factoryOptions;
|
35
|
|
|
}
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* @param ContainerInterface $container The dependency injection container.
|
39
|
|
|
* @param string $name The name of the service to retrieved.
|
40
|
|
|
* @param string $requestedName The service that is being created.
|
41
|
|
|
*
|
42
|
|
|
* @return mixed
|
43
|
|
|
*
|
44
|
|
|
* @throws ServiceNotFoundException If the requested service cannot be loaded.
|
45
|
|
|
*/
|
46
|
|
|
protected function getService(ContainerInterface $container, string $name, string $requestedName)
|
47
|
|
|
{
|
48
|
|
|
if ($name === $requestedName) {
|
49
|
|
|
throw new ServiceNotFoundException(
|
50
|
|
|
sprintf(
|
51
|
|
|
'Encountered a circular dependency reference for service \'%s\'.',
|
52
|
|
|
$requestedName
|
53
|
|
|
)
|
54
|
|
|
);
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
if (!$container->has($name)) {
|
58
|
|
|
throw new ServiceNotFoundException(
|
59
|
|
|
sprintf(
|
60
|
|
|
'The required \'%s\' dependency could not be found while creating service \'%s\'.',
|
61
|
|
|
$name,
|
62
|
|
|
$requestedName
|
63
|
|
|
)
|
64
|
|
|
);
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
return $container->get($name);
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
/**
|
71
|
|
|
* Create a new service instance using the provided $options.
|
72
|
|
|
*
|
73
|
|
|
* @param ServiceLocatorInterface $serviceLocator
|
74
|
|
|
* @param string $name
|
75
|
|
|
* @param array|null $options
|
76
|
|
|
* @param string $requestedName
|
77
|
|
|
*
|
78
|
|
|
* @return mixed
|
79
|
|
|
*
|
80
|
|
|
* @throws ServiceNotCreatedException If the service cannot be built.
|
81
|
|
|
*/
|
82
|
|
|
protected function buildService(
|
83
|
|
|
ServiceLocatorInterface $serviceLocator,
|
84
|
|
|
string $name,
|
85
|
|
|
?array $options,
|
86
|
|
|
string $requestedName
|
87
|
|
|
) {
|
88
|
|
|
try {
|
89
|
|
|
return $serviceLocator->build($name, $options);
|
90
|
|
|
} catch (\Throwable $e) {
|
91
|
|
|
throw new ServiceNotCreatedException(
|
92
|
|
|
sprintf(
|
93
|
|
|
'Failed to build service \'%s\' required as dependency of service \'%s\' : %s',
|
94
|
|
|
$name,
|
95
|
|
|
$requestedName,
|
96
|
|
|
$e->getMessage()
|
97
|
|
|
),
|
98
|
|
|
$e->getCode(),
|
99
|
|
|
$e
|
100
|
|
|
);
|
101
|
|
|
}
|
102
|
|
|
}
|
103
|
|
|
}
|
104
|
|
|
|