1 <?php
2 3 4 5 6 7
8
9 namespace Cross\Cache;
10
11 use Cross\I\RequestCacheInterface;
12 use Cross\Exception\CoreException;
13 use Cross\Cache\Request\FileCache;
14 use Cross\Cache\Request\RedisCache;
15 use Cross\Cache\Request\Memcache;
16 use ReflectionClass;
17
18 19 20 21 22 23 24
25 class RequestCache
26 {
27 const FILE = 1;
28 const MEMCACHE = 2;
29 const REDIS = 3;
30
31 32 33
34 static $instance;
35
36 37 38 39 40 41 42 43
44 static function factory($type, array $options)
45 {
46 switch ($type) {
47 case 'file':
48 case self::FILE:
49 $instance = new FileCache($options);
50 break;
51
52 case 'memcache':
53 case self::MEMCACHE:
54 $instance = new Memcache($options);
55 break;
56
57 case 'redis':
58 case self::REDIS:
59 $instance = new RedisCache($options);
60 break;
61
62 default:
63 if (is_string($type)) {
64 try {
65 $rf = new ReflectionClass($type);
66 if ($rf->implementsInterface('Cross\I\RequestCacheInterface')) {
67 $instance = $rf->newInstance();
68 } else {
69 throw new CoreException('Must implement RequestCacheInterface');
70 }
71 } catch (\Exception $e) {
72 throw new CoreException('Reflection ' . $e->getMessage());
73 }
74 } elseif (is_object($type)) {
75 if ($type instanceof RequestCacheInterface) {
76 $instance = $type;
77 } else {
78 throw new CoreException('Must implement RequestCacheInterface');
79 }
80 } else {
81 throw new CoreException('Unsupported cache type!');
82 }
83 }
84
85 $instance->setConfig($options);
86 return $instance;
87 }
88 }
89