1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facile\SentryModule\ServiceFactory; |
4
|
|
|
|
5
|
|
|
use Facile\SentryModule\Service\AbstractFactory; |
6
|
|
|
use Interop\Container\ContainerInterface; |
7
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
8
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
9
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
10
|
|
|
|
11
|
|
|
class AbstractClientServiceFactory implements AbstractFactoryInterface |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param ContainerInterface $container |
16
|
|
|
* @param string $requestedName |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
public function canCreate(ContainerInterface $container, $requestedName) |
20
|
|
|
{ |
21
|
|
|
return false !== $this->getServiceFactoryMapping($container, $requestedName); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ContainerInterface $container |
26
|
|
|
* @param string $requestedName |
27
|
|
|
* @param array|null $options |
28
|
|
|
* @return AbstractFactory |
29
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
30
|
|
|
*/ |
31
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
32
|
|
|
{ |
33
|
|
|
$mapping = $this->getServiceFactoryMapping($container, $requestedName); |
34
|
|
|
|
35
|
|
|
if (!$mapping) { |
36
|
|
|
throw new ServiceNotFoundException(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @var array $mapping */ |
40
|
|
|
$factoryClass = $mapping['factoryClass']; |
41
|
|
|
$name = $mapping['name']; |
42
|
|
|
|
43
|
|
|
$factory = new $factoryClass($name); |
44
|
|
|
|
45
|
|
|
return $factory($container, $requestedName); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
* @throws ServiceNotFoundException |
51
|
|
|
*/ |
52
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
53
|
|
|
{ |
54
|
|
|
return $this->canCreate($serviceLocator, $requestedName); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
* @throws ServiceNotFoundException |
60
|
|
|
*/ |
61
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
62
|
|
|
{ |
63
|
|
|
return $this($serviceLocator, $requestedName); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ContainerInterface $container |
68
|
|
|
* @param $name |
69
|
|
|
* @return array|bool |
70
|
|
|
* @throws \Interop\Container\Exception\NotFoundException |
71
|
|
|
* @throws \Interop\Container\Exception\ContainerException |
72
|
|
|
*/ |
73
|
|
|
protected function getServiceFactoryMapping(ContainerInterface $container, $name) |
74
|
|
|
{ |
75
|
|
|
$matches = []; |
76
|
|
|
|
77
|
|
|
$pattern = '/^facile\.sentry\.(?P<serviceType>[a-z0-9_]+)\.(?P<serviceName>[a-z0-9_]+)$/'; |
78
|
|
|
if (!preg_match($pattern, $name, $matches)) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$config = $container->get('config'); |
83
|
|
|
$serviceType = $matches['serviceType']; |
84
|
|
|
$serviceName = $matches['serviceName']; |
85
|
|
|
|
86
|
|
|
if (!isset($config['facile']['sentry_factories'][$serviceType])) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!isset($config['facile']['sentry'][$serviceType][$serviceName])) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return [ |
95
|
|
|
'name' => $serviceName, |
96
|
|
|
'factoryClass' => $config['facile']['sentry_factories'][$serviceType], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.