1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineModule\ServiceFactory; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
7
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
8
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Abstract service factory capable of instantiating services whose names match the |
12
|
|
|
* pattern <code>doctrine.$serviceType.$serviceName</doctrine> |
13
|
|
|
* |
14
|
|
|
* @license MIT |
15
|
|
|
* @link http://www.doctrine-project.org/ |
16
|
|
|
* @author Marco Pivetta <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class AbstractDoctrineServiceFactory implements AbstractFactoryInterface |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
30 |
|
public function canCreate(ContainerInterface $container, $requestedName) |
24
|
|
|
{ |
25
|
30 |
|
return false !== $this->getFactoryMapping($container, $requestedName); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritDoc} |
30
|
|
|
*/ |
31
|
1 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
32
|
|
|
{ |
33
|
1 |
|
$mappings = $this->getFactoryMapping($container, $requestedName); |
34
|
|
|
|
35
|
1 |
|
if (! $mappings) { |
36
|
|
|
throw new ServiceNotFoundException(); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$factoryClass = $mappings['factoryClass']; |
40
|
|
|
/* @var $factory \DoctrineModule\Service\AbstractFactory */ |
41
|
1 |
|
$factory = new $factoryClass($mappings['serviceName']); |
42
|
|
|
|
43
|
1 |
|
return $factory->createService($container); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
* @deprecated |
49
|
|
|
*/ |
50
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName) |
51
|
|
|
{ |
52
|
|
|
return $this->canCreate($container, $requestedName); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
* @deprecated |
58
|
|
|
*/ |
59
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
60
|
|
|
{ |
61
|
|
|
return $this($serviceLocator, $requestedName); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ContainerInterface $serviceLocator |
66
|
|
|
* @param string $name |
67
|
|
|
* |
68
|
|
|
* @return array|bool |
69
|
|
|
*/ |
70
|
30 |
|
private function getFactoryMapping(ContainerInterface $serviceLocator, $name) |
71
|
|
|
{ |
72
|
30 |
|
$matches = []; |
73
|
|
|
|
74
|
30 |
|
if (! preg_match( |
75
|
30 |
|
'/^doctrine\.((?<mappingType>orm|odm)\.|)(?<serviceType>[a-z0-9_]+)\.(?<serviceName>[a-z0-9_]+)$/', |
76
|
30 |
|
$name, |
77
|
30 |
|
$matches |
78
|
|
|
)) { |
79
|
11 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
20 |
|
$config = $serviceLocator->get('Config'); |
83
|
20 |
|
$mappingType = $matches['mappingType']; |
84
|
20 |
|
$serviceType = $matches['serviceType']; |
85
|
20 |
|
$serviceName = $matches['serviceName']; |
86
|
|
|
|
87
|
20 |
|
if ($mappingType == '') { |
88
|
20 |
|
if (! isset($config['doctrine_factories'][$serviceType]) || |
89
|
20 |
|
! isset($config['doctrine'][$serviceType][$serviceName]) |
90
|
|
|
) { |
91
|
4 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return [ |
95
|
16 |
|
'serviceType' => $serviceType, |
96
|
16 |
|
'serviceName' => $serviceName, |
97
|
16 |
|
'factoryClass' => $config['doctrine_factories'][$serviceType], |
98
|
|
|
]; |
99
|
|
|
} else { |
100
|
|
|
if (! isset($config['doctrine_factories'][$mappingType]) || |
101
|
|
|
! isset($config['doctrine_factories'][$mappingType][$serviceType]) || |
102
|
|
|
! isset($config['doctrine'][$mappingType][$serviceType][$serviceName]) |
103
|
|
|
) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
return [ |
107
|
|
|
'serviceType' => $serviceType, |
108
|
|
|
'serviceName' => $serviceName, |
109
|
|
|
'factoryClass' => $config['doctrine_factories'][$mappingType][$serviceType], |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.