1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineModule\ServiceFactory; |
21
|
|
|
|
22
|
|
|
use Interop\Container\ContainerInterface; |
23
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
24
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
25
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Abstract service factory capable of instantiating services whose names match the |
29
|
|
|
* pattern <code>doctrine.$serviceType.$serviceName</doctrine> |
30
|
|
|
* |
31
|
|
|
* @license MIT |
32
|
|
|
* @link http://www.doctrine-project.org/ |
33
|
|
|
* @author Marco Pivetta <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class AbstractDoctrineServiceFactory implements AbstractFactoryInterface |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
30 |
|
public function canCreate(ContainerInterface $container, $requestedName) |
41
|
|
|
{ |
42
|
30 |
|
return false !== $this->getFactoryMapping($container, $requestedName); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
49
|
|
|
{ |
50
|
1 |
|
$mappings = $this->getFactoryMapping($container, $requestedName); |
51
|
|
|
|
52
|
1 |
|
if (! $mappings) { |
53
|
|
|
throw new ServiceNotFoundException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$factoryClass = $mappings['factoryClass']; |
57
|
|
|
/* @var $factory \DoctrineModule\Service\AbstractFactory */ |
58
|
1 |
|
$factory = new $factoryClass($mappings['serviceName']); |
59
|
|
|
|
60
|
1 |
|
return $factory->createService($container); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
* @deprecated |
66
|
|
|
*/ |
67
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName) |
68
|
|
|
{ |
69
|
|
|
return $this->canCreate($container, $requestedName); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
* @deprecated |
75
|
|
|
*/ |
76
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
77
|
|
|
{ |
78
|
|
|
return $this($serviceLocator, $requestedName); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ContainerInterface $serviceLocator |
83
|
|
|
* @param string $name |
84
|
|
|
* |
85
|
|
|
* @return array|bool |
86
|
|
|
*/ |
87
|
30 |
|
private function getFactoryMapping(ContainerInterface $serviceLocator, $name) |
88
|
|
|
{ |
89
|
30 |
|
$matches = array(); |
90
|
|
|
|
91
|
30 |
|
if (!preg_match( |
92
|
30 |
|
'/^doctrine\.((?<mappingType>orm|odm)\.|)(?<serviceType>[a-z0-9_]+)\.(?<serviceName>[a-z0-9_]+)$/', |
93
|
30 |
|
$name, |
94
|
|
|
$matches |
95
|
30 |
|
)) { |
96
|
10 |
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
20 |
|
$config = $serviceLocator->get('Config'); |
100
|
20 |
|
$mappingType = $matches['mappingType']; |
101
|
20 |
|
$serviceType = $matches['serviceType']; |
102
|
20 |
|
$serviceName = $matches['serviceName']; |
103
|
|
|
|
104
|
20 |
|
if ($mappingType == '') { |
105
|
20 |
|
if (! isset($config['doctrine_factories'][$serviceType]) || |
106
|
18 |
|
! isset($config['doctrine'][$serviceType][$serviceName]) |
107
|
20 |
|
) { |
108
|
4 |
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return array( |
112
|
16 |
|
'serviceType' => $serviceType, |
113
|
16 |
|
'serviceName' => $serviceName, |
114
|
16 |
|
'factoryClass' => $config['doctrine_factories'][$serviceType], |
115
|
16 |
|
); |
116
|
|
|
} else { |
117
|
|
|
if (! isset($config['doctrine_factories'][$mappingType]) || |
118
|
|
|
! isset($config['doctrine_factories'][$mappingType][$serviceType]) || |
119
|
|
|
! isset($config['doctrine'][$mappingType][$serviceType][$serviceName]) |
120
|
|
|
) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
return array( |
124
|
|
|
'serviceType' => $serviceType, |
125
|
|
|
'serviceName' => $serviceName, |
126
|
|
|
'factoryClass' => $config['doctrine_factories'][$mappingType][$serviceType], |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.