1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace Laminas\Cache\Pattern;
use Laminas\Cache\Exception\InvalidArgumentException;
use Laminas\Cache\Storage\AdapterPluginManager;
use Laminas\Cache\Storage\StorageInterface;
use Laminas\Cache\StorageFactory;
use Psr\Container\ContainerInterface;
use function class_exists;
use function is_string;
/**
* @deprecated This will be removed with v3.0.0 as providing generic factories for cache patterns wont be suitable for
* all possible combinations of implemented upstream patterns.
*/
final class StoragePatternCacheFactory
{
/**
* @var callable(ContainerInterface,array|string):StorageInterface
*/
private $storageFactory;
/**
* @param (callable(ContainerInterface,array|string):StorageInterface)|null $storageFactory
*/
public function __construct(callable $storageFactory = null)
{
$this->storageFactory = $storageFactory ?? static function ($container, $storageOption): StorageInterface {
if (is_string($storageOption)) {
$adapterPluginManager = $container->get(AdapterPluginManager::class);
return $adapterPluginManager->get($storageOption);
}
return StorageFactory::factory($storageOption);
};
}
/**
* @template T of StorageCapableInterface
* @psalm-param class-string<T> $requestedName
* @psalm-return T
*/
public function __invoke(
ContainerInterface $container,
string $requestedName,
array $options = null
): StorageCapableInterface {
if (! class_exists($requestedName)) {
throw new InvalidArgumentException(sprintf(
'Factory %s is used for a service %s which does not exist.'
. ' Please only use this factory for services with full qualified class names!',
self::class,
$requestedName
));
}
$storageOption = $options['storage'] ?? null;
$storageInstance = null;
if (is_array($storageOption) || is_string($storageOption)) {
$storageInstance = ($this->storageFactory)($container, $storageOption);
$options['storage'] = $storageInstance;
}
$patternOptions = new PatternOptions($options);
return new $requestedName($storageInstance, $patternOptions);
}
}