1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Svycka\Settings\Collection\Factory; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Interop\Container\Exception\ContainerException; |
7
|
|
|
use Svycka\Settings\Collection\CollectionsManager; |
8
|
|
|
use Svycka\Settings\Collection\SettingsCollection; |
9
|
|
|
use Svycka\Settings\Options\CollectionOptions; |
10
|
|
|
use Svycka\Settings\Options\ModuleOptions; |
11
|
|
|
use Svycka\Settings\Provider\OwnerProviderInterface; |
12
|
|
|
use Svycka\Settings\Storage\StorageAdapterInterface; |
13
|
|
|
use Svycka\Settings\Type\TypesManager; |
14
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
15
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
16
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
17
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Vytautas Stankus <[email protected]> |
21
|
|
|
* @license MIT |
22
|
|
|
*/ |
23
|
|
|
class SettingsCollectionAbstractFactory implements AbstractFactoryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Can the factory create an instance for the service? |
27
|
|
|
* |
28
|
|
|
* @param ContainerInterface $container |
29
|
|
|
* @param string $requestedName |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
2 |
|
public function canCreate(ContainerInterface $container, $requestedName) |
33
|
|
|
{ |
34
|
|
|
/** @var CollectionsManager $config */ |
35
|
2 |
|
$config = $container->get(ModuleOptions::class)->getCollections(); |
36
|
2 |
|
if (empty($config)) { |
37
|
1 |
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return isset($config[$requestedName]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create an object |
45
|
|
|
* |
46
|
|
|
* @param ContainerInterface $container |
47
|
|
|
* @param string $requestedName |
48
|
|
|
* @param null|array $options |
49
|
|
|
* @return object |
50
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
51
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
52
|
|
|
* creating a service. |
53
|
|
|
* @throws ContainerException if any other error occurs |
54
|
|
|
*/ |
55
|
1 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
56
|
|
|
{ |
57
|
1 |
|
$collectionsConfig = $container->get(ModuleOptions::class)->getCollections(); |
58
|
1 |
|
$config = new CollectionOptions($collectionsConfig[$requestedName]); |
59
|
1 |
|
$config->setName($requestedName); |
60
|
|
|
|
61
|
|
|
/** @var StorageAdapterInterface $storage */ |
62
|
1 |
|
$storage = $container->get($config->getStorageAdapter()); |
63
|
|
|
/** @var OwnerProviderInterface $owner_provider */ |
64
|
1 |
|
$owner_provider = $container->get($config->getOwnerProvider()); |
65
|
|
|
/** @var TypesManager $typesManager */ |
66
|
1 |
|
$typesManager = $container->get(TypesManager::class); |
67
|
|
|
|
68
|
1 |
|
return new SettingsCollection($config, $storage, $owner_provider, $typesManager); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
* |
74
|
|
|
* @deprecated will be removed after zf2 support drop |
75
|
|
|
*/ |
76
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
77
|
|
|
{ |
78
|
|
|
return $this->canCreate($serviceLocator->getServiceLocator(), $requestedName); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
* |
84
|
|
|
* @deprecated will be removed after zf2 support drop |
85
|
|
|
*/ |
86
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
87
|
|
|
{ |
88
|
|
|
return $this($serviceLocator->getServiceLocator(), $requestedName); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: