1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElasticsearchModule\Service; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Interop\Container\ContainerInterface; |
7
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
8
|
|
|
use Zend\ServiceManager\FactoryInterface; |
9
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
10
|
|
|
use Zend\Stdlib\ArrayObject as ZendArrayObject; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Pedro Alves <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractFactory implements FactoryInterface |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $name |
25
|
|
|
*/ |
26
|
43 |
|
public function __construct($name) |
27
|
2 |
|
{ |
28
|
43 |
|
$this->name = $name; |
29
|
43 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
abstract public function getServiceType(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ContainerInterface $container |
38
|
|
|
* @param array|ArrayObject|ZendArrayObject $config |
39
|
|
|
*/ |
40
|
|
|
abstract protected function create(ContainerInterface $container, $config); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
44
|
|
|
* @return mixed |
45
|
|
|
* @throws ServiceNotCreatedException |
46
|
|
|
*/ |
47
|
42 |
|
public function createService(ServiceLocatorInterface $serviceLocator) |
48
|
|
|
{ |
49
|
42 |
|
return $this($serviceLocator, sprintf('elasticsearch.%s.%s', $this->getServiceType(), $this->name)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ContainerInterface $container |
54
|
|
|
* @return mixed |
55
|
|
|
* @throws ServiceNotCreatedException |
56
|
|
|
*/ |
57
|
43 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
58
|
|
|
{ |
59
|
43 |
|
$config = $container->get('Config'); |
60
|
43 |
|
$serviceType = $this->getServiceType(); |
61
|
|
|
|
62
|
43 |
|
if (!isset($config['elasticsearch'][$serviceType][$this->name])) { |
63
|
3 |
|
throw new ServiceNotCreatedException("$requestedName could not be found"); |
64
|
|
|
} |
65
|
|
|
|
66
|
40 |
|
return $this->create($container, $config['elasticsearch'][$serviceType][$this->name]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ContainerInterface $container |
71
|
|
|
* @param string $serviceOrClass |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
34 |
|
protected function getServiceOrClassObject(ContainerInterface $container, $serviceOrClass) |
75
|
|
|
{ |
76
|
34 |
|
if ($container->has($serviceOrClass)) { |
77
|
22 |
|
return $container->get($serviceOrClass); |
78
|
31 |
|
} else if (class_exists($serviceOrClass)) { |
79
|
28 |
|
return new $serviceOrClass(); |
80
|
|
|
} |
81
|
3 |
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
23 |
|
protected function getName() |
88
|
|
|
{ |
89
|
23 |
|
return $this->name; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.