1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 Krishnaprasad MG <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Sunspikes\Ratelimit\Cache\Factory; |
27
|
|
|
|
28
|
|
|
use Desarrolla2\Cache\Adapter\Apc; |
29
|
|
|
use Desarrolla2\Cache\Adapter\File; |
30
|
|
|
use Desarrolla2\Cache\Adapter\MemCache; |
31
|
|
|
use Desarrolla2\Cache\Adapter\Memory; |
32
|
|
|
use Desarrolla2\Cache\Adapter\Mongo; |
33
|
|
|
use Desarrolla2\Cache\Adapter\MySQL; |
34
|
|
|
use Desarrolla2\Cache\Adapter\NotCache; |
35
|
|
|
use Desarrolla2\Cache\Adapter\Redis; |
36
|
|
|
use Desarrolla2\Cache\Cache; |
37
|
|
|
use Sunspikes\Ratelimit\Cache\Exception\DriverNotFoundException; |
38
|
|
|
use Sunspikes\Ratelimit\Cache\Exception\InvalidConfigException; |
39
|
|
|
|
40
|
|
|
class DesarrollaCacheFactory implements FactoryInterface |
41
|
|
|
{ |
42
|
|
|
/* @const DEFAULT_TTL */ |
43
|
|
|
const DEFAULT_TTL = 3600; |
44
|
|
|
/* @const DEFAULT_LIMIT */ |
45
|
|
|
const DEFAULT_LIMIT = 1000; |
46
|
|
|
|
47
|
|
|
/* @var array */ |
48
|
|
|
protected $config; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string|null $configFile |
52
|
|
|
* @param array $configArray |
53
|
|
|
*/ |
54
|
1 |
|
public function __construct($configFile = null, array $configArray = []) |
55
|
|
|
{ |
56
|
|
|
// Default config from distribution |
57
|
1 |
|
if (null === $configFile) { |
58
|
1 |
|
$configFile = __DIR__.'/../../../config/config.php'; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
$config = include $configFile; |
62
|
|
|
|
63
|
1 |
|
$this->config = array_merge($config, $configArray); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
1 |
|
public function make() |
70
|
|
|
{ |
71
|
1 |
|
return new Cache($this->getDriver()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Make the driver based on given config |
76
|
|
|
* |
77
|
|
|
* @return null|\Desarrolla2\Cache\Adapter\AdapterInterface |
78
|
|
|
* |
79
|
|
|
* @throws DriverNotFoundException |
80
|
|
|
* @throws InvalidConfigException |
81
|
|
|
*/ |
82
|
1 |
|
protected function getDriver() |
83
|
|
|
{ |
84
|
1 |
|
$driver = $this->config['driver']; |
85
|
|
|
|
86
|
1 |
|
if (is_null($driver)) { |
87
|
|
|
throw new InvalidConfigException('Cache driver is not defined in configuration.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$driverCreateMethod = 'create' . ucfirst($driver) . 'Driver'; |
91
|
|
|
|
92
|
1 |
|
if (method_exists($this, $driverCreateMethod)) { |
93
|
1 |
|
$driver = $this->{$driverCreateMethod}(); |
94
|
1 |
|
$driver->setOption('ttl', |
95
|
1 |
|
$this->config['default_ttl'] |
96
|
|
|
?: static::DEFAULT_TTL |
97
|
1 |
|
); |
98
|
|
|
|
99
|
1 |
|
return $driver; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
throw new DriverNotFoundException('Cannot find the driver ' . $driver . ' for Desarrolla'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create NotCache driver |
107
|
|
|
* |
108
|
|
|
* @return NotCache |
109
|
|
|
*/ |
110
|
1 |
|
protected function createNotcacheDriver() |
111
|
|
|
{ |
112
|
1 |
|
return new NotCache(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Create File driver |
117
|
|
|
* |
118
|
|
|
* @return File |
119
|
|
|
*/ |
120
|
|
|
protected function createFileDriver() |
121
|
|
|
{ |
122
|
|
|
return new File($this->config['cache_dir']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Create APC driver |
127
|
|
|
* |
128
|
|
|
* @return Apc |
129
|
|
|
*/ |
130
|
|
|
protected function createApcDriver() |
131
|
|
|
{ |
132
|
|
|
return new Apc(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Create Memory driver |
137
|
|
|
* |
138
|
|
|
* @return Memory |
139
|
|
|
* @throws \Desarrolla2\Cache\Adapter\MemoryCacheException |
140
|
|
|
*/ |
141
|
|
|
protected function createMemoryDriver() |
142
|
|
|
{ |
143
|
|
|
$memory = new Memory(); |
144
|
|
|
$memory->setOption('limit', |
145
|
|
|
$this->config['limit'] |
146
|
|
|
?: static::DEFAULT_LIMIT |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
return $memory; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Create Mongo driver |
154
|
|
|
* |
155
|
|
|
* @return Mongo |
156
|
|
|
*/ |
157
|
|
|
protected function createMongoDriver() |
158
|
|
|
{ |
159
|
|
|
return new Mongo($this->config['server']); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Create MySQL driver |
164
|
|
|
* |
165
|
|
|
* @return MySQL |
166
|
|
|
*/ |
167
|
|
|
protected function createMysqlDriver() |
168
|
|
|
{ |
169
|
|
|
return new MySQL( |
170
|
|
|
$this->config['host'], |
171
|
|
|
$this->config['username'], |
172
|
|
|
$this->config['password'], |
173
|
|
|
$this->config['port'] |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Create Redis driver |
179
|
|
|
* |
180
|
|
|
* @return Redis |
181
|
|
|
*/ |
182
|
|
|
protected function createRedisDriver() |
183
|
|
|
{ |
184
|
|
|
return new Redis(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Create MemCache driver |
189
|
|
|
* |
190
|
|
|
* @return MemCache |
191
|
|
|
*/ |
192
|
|
|
protected function createMemcacheDriver() |
193
|
|
|
{ |
194
|
|
|
return new MemCache(); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|