|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Acelaya\Website\Factory; |
|
5
|
|
|
|
|
6
|
|
|
use Interop\Container\ContainerInterface; |
|
7
|
|
|
use Interop\Container\Exception\ContainerException; |
|
8
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
9
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
|
10
|
|
|
use Zend\ServiceManager\Factory\AbstractFactoryInterface; |
|
11
|
|
|
|
|
12
|
|
|
class DotNotationConfigAbstractFactory implements AbstractFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Can the factory create an instance for the service? |
|
16
|
|
|
* |
|
17
|
|
|
* @param ContainerInterface $container |
|
18
|
|
|
* @param string $requestedName |
|
19
|
|
|
* @return bool |
|
20
|
|
|
*/ |
|
21
|
|
|
public function canCreate(ContainerInterface $container, $requestedName) |
|
22
|
|
|
{ |
|
23
|
|
|
return substr_count($requestedName, '.') > 0; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create an object |
|
28
|
|
|
* |
|
29
|
|
|
* @param ContainerInterface $container |
|
30
|
|
|
* @param string $requestedName |
|
31
|
|
|
* @param null|array $options |
|
32
|
|
|
* @return object |
|
33
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
34
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
35
|
|
|
* creating a service. |
|
36
|
|
|
* @throws ContainerException if any other error occurs |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$parts = explode('.', $requestedName); |
|
41
|
|
|
$serviceName = array_shift($parts); |
|
42
|
|
|
if (! $container->has($serviceName)) { |
|
43
|
|
|
throw new ServiceNotCreatedException(sprintf( |
|
44
|
|
|
'Defined service "%s" could not be found in container after resolving dotted expression "%s".', |
|
45
|
|
|
$serviceName, |
|
46
|
|
|
$requestedName |
|
47
|
|
|
)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$array = $container->get($serviceName); |
|
51
|
|
|
return $this->readKeysFromArray($parts, $array); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array $keys |
|
56
|
|
|
* @param array|\ArrayAccess $array |
|
57
|
|
|
* @return mixed|null |
|
58
|
|
|
* @throws ServiceNotCreatedException |
|
59
|
|
|
*/ |
|
60
|
|
|
private function readKeysFromArray(array $keys, $array) |
|
61
|
|
|
{ |
|
62
|
|
|
$key = array_shift($keys); |
|
63
|
|
|
|
|
64
|
|
|
// When one of the provided keys is not found, throw an exception |
|
65
|
|
|
if (! isset($array[$key])) { |
|
66
|
|
|
throw new ServiceNotCreatedException(sprintf( |
|
67
|
|
|
'The key "%s" provided in the dotted notation could not be found in the array service', |
|
68
|
|
|
$key |
|
69
|
|
|
)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$value = $array[$key]; |
|
73
|
|
|
if (! empty($keys) && (is_array($value) || $value instanceof \ArrayAccess)) { |
|
74
|
|
|
$value = $this->readKeysFromArray($keys, $value); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $value; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|