1: <?php
2:
3: namespace Charcoal\App\Config;
4:
5:
6: use InvalidArgumentException;
7:
8:
9: use Charcoal\Config\AbstractConfig;
10:
11: 12: 13:
14: class CacheConfig extends AbstractConfig
15: {
16: 17: 18:
19: private $types = ['memory'];
20:
21: 22: 23: 24: 25: 26:
27: private $defaultTtl = 864000;
28:
29: 30: 31:
32: private $prefix = 'charcoal';
33:
34: 35: 36:
37: public $middleware;
38:
39: 40: 41:
42: public function defaults()
43: {
44: return [
45: 'types' => ['memory'],
46: 'default_ttl' => 864000,
47: 'prefix' => 'charcoal',
48: 'middleware' => $this->middlewareDefaults()
49: ];
50: }
51:
52: 53: 54:
55: private function middlewareDefaults()
56: {
57: return [
58: 'active' => true,
59: 'included_path' => null,
60: 'excluded_path' => null,
61: 'methods' => [
62: 'GET'
63: ],
64: 'status_codes' => [
65: 200
66: ],
67: 'ttl' => 0,
68: 'included_query' => null,
69: 'excluded_query' => null,
70: 'ignored_query' => null
71: ];
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81:
82: public function setTypes(array $types)
83: {
84: $this->types = [];
85: foreach ($types as $type) {
86: $this->addType($type);
87: }
88: return $this;
89: }
90:
91: 92: 93: 94: 95:
96: public function validTypes()
97: {
98: return [
99: 'apc',
100: 'file',
101: 'db',
102: 'memcache',
103: 'memory',
104: 'noop',
105: 'redis'
106: ];
107: }
108:
109: 110: 111: 112: 113:
114: public function addType($type)
115: {
116: if (!in_array($type, $this->validTypes())) {
117: throw new InvalidArgumentException(
118: sprintf('Invalid cache type: "%s"', $type)
119: );
120: }
121: $this->types[] = $type;
122: return $this;
123: }
124:
125: 126: 127:
128: public function types()
129: {
130: return $this->types;
131: }
132:
133: 134: 135: 136: 137:
138: public function setDefaultTtl($ttl)
139: {
140: if (!is_numeric($ttl)) {
141: throw new InvalidArgumentException(
142: 'TTL must be an integer (seconds).'
143: );
144: }
145: $this->defaultTtl = (int)$ttl;
146: return $this;
147: }
148:
149: 150: 151:
152: public function defaultTtl()
153: {
154: return $this->defaultTtl;
155: }
156:
157: 158: 159: 160: 161:
162: public function setPrefix($prefix)
163: {
164: if (!is_string($prefix)) {
165: throw new InvalidArgumentException(
166: 'Prefix must be a string.'
167: );
168: }
169: $this->prefix = $prefix;
170: return $this;
171: }
172:
173: 174: 175:
176: public function prefix()
177: {
178: return $this->prefix;
179: }
180:
181: 182: 183: 184:
185: public function setMiddleware(array $middleware)
186: {
187: $middleware = array_merge($this->middlewareDefaults(), $middleware);
188: $this->middleware = $middleware;
189: return $this;
190: }
191:
192: 193: 194:
195: public function middleware()
196: {
197: return $this->middleware;
198: }
199: }
200: