-
Notifications
You must be signed in to change notification settings - Fork 1
/
StashServiceProvider.php
137 lines (116 loc) · 4.74 KB
/
StashServiceProvider.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace TheCodingMachine;
use Psr\Container\ContainerInterface;
use Interop\Container\Factories\Alias;
use Interop\Container\Factories\Parameter;
use Interop\Container\ServiceProviderInterface;
use Psr\Cache\CacheItemPoolInterface;
use Stash\Driver\Apc;
use Stash\Driver\BlackHole;
use Stash\Driver\Composite;
use Stash\Driver\Ephemeral;
use Stash\Driver\FileSystem;
use Stash\Driver\Memcache;
use Stash\Driver\Redis;
use Stash\Driver\Sqlite;
use Stash\DriverList;
use Stash\Interfaces\DriverInterface;
use Stash\Interfaces\PoolInterface;
use Stash\Pool;
use Twig_Environment;
use Twig_LoaderInterface;
use Twig_Loader_Chain;
use Twig_Loader_Filesystem;
class StashServiceProvider implements ServiceProviderInterface
{
const PACKAGE = 'thecodingmachine.stash-universal-module';
private $suffix;
/**
* @param string $suffix The Suffix added to all created instances (if you need more than one cache).
*/
public function __construct(string $suffix = null)
{
$this->suffix = $suffix ? '.'.$suffix : '';
}
public function getFactories()
{
return [
'stash'.$this->suffix.'.composite.options' => function(ContainerInterface $container) : array
{
// Let's put the Ephemeral driver first.
$drivers = [
self::get($container, Ephemeral::class.$this->suffix)
];
// Now, let's put APC if available.
if (Apc::isAvailable()) {
$drivers[] = self::get($container, Apc::class.$this->suffix);
} else {
// Else, let's enable teh filesystem by default.
// TODO: look in options and enable the configured options!
$drivers[] = self::get($container, FileSystem::class.$this->suffix);
}
return [
'drivers' => $drivers
];
},
Composite::class.$this->suffix => function(ContainerInterface $container) : Composite
{
return new Composite(self::get($container, 'stash'.$this->suffix.'.composite.options'));
},
Ephemeral::class.$this->suffix => [self::class,'createEphemeralDriver'],
BlackHole::class.$this->suffix => [self::class,'createBlackHoleDriver'],
Apc::class.$this->suffix => function(ContainerInterface $container) : Apc
{
return new Apc(self::get($container, 'stash'.$this->suffix.'.apc.options', []));
},
FileSystem::class.$this->suffix => function(ContainerInterface $container) : FileSystem
{
return new FileSystem(self::get($container, 'stash'.$this->suffix.'.filesystem.options', []));
},
Memcache::class.$this->suffix => function(ContainerInterface $container) : Memcache
{
return new Memcache(self::get($container, 'stash'.$this->suffix.'.memcache.options', []));
},
Redis::class.$this->suffix => function(ContainerInterface $container) : Redis
{
return new Redis(self::get($container, 'stash'.$this->suffix.'.redis.options', []));
},
Sqlite::class.$this->suffix => function(ContainerInterface $container) : Sqlite
{
return new Sqlite(self::get($container, 'stash'.$this->suffix.'.sqlite.options', []));
},
DriverInterface::class.$this->suffix => new Alias(Composite::class.$this->suffix),
Pool::class.$this->suffix => function(ContainerInterface $container) : Pool
{
return new Pool(self::get($container, DriverInterface::class.$this->suffix, []));
},
PoolInterface::class.$this->suffix => new Alias(Pool::class.$this->suffix),
CacheItemPoolInterface::class.$this->suffix => new Alias(PoolInterface::class.$this->suffix),
];
}
public function getExtensions()
{
return [];
}
/**
* Returns the entry named PACKAGE.$name, of simply $name if PACKAGE.$name is not found.
*
* @param ContainerInterface $container
* @param string $name
*
* @return mixed
*/
private static function get(ContainerInterface $container, string $name, $default = null)
{
$namespacedName = self::PACKAGE.'.'.$name;
return $container->has($namespacedName) ? $container->get($namespacedName) : ($container->has($name) ? $container->get($name) : $default);
}
public static function createEphemeralDriver() : Ephemeral
{
return new Ephemeral();
}
public static function createBlackHoleDriver() : BlackHole
{
return new BlackHole();
}
}