1: <?php namespace Laravel; use Closure;
2:
3: class Cache {
4:
5: /**
6: * All of the active cache drivers.
7: *
8: * @var array
9: */
10: public static $drivers = array();
11:
12: /**
13: * The third-party driver registrar.
14: *
15: * @var array
16: */
17: public static $registrar = array();
18:
19: /**
20: * Get a cache driver instance.
21: *
22: * If no driver name is specified, the default will be returned.
23: *
24: * <code>
25: * // Get the default cache driver instance
26: * $driver = Cache::driver();
27: *
28: * // Get a specific cache driver instance by name
29: * $driver = Cache::driver('memcached');
30: * </code>
31: *
32: * @param string $driver
33: * @return Cache\Drivers\Driver
34: */
35: public static function driver($driver = null)
36: {
37: if (is_null($driver)) $driver = Config::get('cache.driver');
38:
39: if ( ! isset(static::$drivers[$driver]))
40: {
41: static::$drivers[$driver] = static::factory($driver);
42: }
43:
44: return static::$drivers[$driver];
45: }
46:
47: /**
48: * Create a new cache driver instance.
49: *
50: * @param string $driver
51: * @return Cache\Drivers\Driver
52: */
53: protected static function factory($driver)
54: {
55: if (isset(static::$registrar[$driver]))
56: {
57: $resolver = static::$registrar[$driver];
58:
59: return $resolver();
60: }
61:
62: switch ($driver)
63: {
64: case 'apc':
65: return new Cache\Drivers\APC(Config::get('cache.key'));
66:
67: case 'file':
68: return new Cache\Drivers\File(path('storage').'cache'.DS);
69:
70: case 'memcached':
71: return new Cache\Drivers\Memcached(Memcached::connection(), Config::get('cache.key'));
72:
73: case 'memory':
74: return new Cache\Drivers\Memory;
75:
76: case 'redis':
77: return new Cache\Drivers\Redis(Redis::db());
78:
79: case 'database':
80: return new Cache\Drivers\Database(Config::get('cache.key'));
81:
82: case 'wincache':
83: return new Cache\Drivers\WinCache(Config::get('cache.key'));
84:
85: default:
86: throw new \Exception("Cache driver {$driver} is not supported.");
87: }
88: }
89:
90: /**
91: * Register a third-party cache driver.
92: *
93: * @param string $driver
94: * @param Closure $resolver
95: * @return void
96: */
97: public static function extend($driver, Closure $resolver)
98: {
99: static::$registrar[$driver] = $resolver;
100: }
101:
102: /**
103: * Magic Method for calling the methods on the default cache driver.
104: *
105: * <code>
106: * // Call the "get" method on the default cache driver
107: * $name = Cache::get('name');
108: *
109: * // Call the "put" method on the default cache driver
110: * Cache::put('name', 'Taylor', 15);
111: * </code>
112: */
113: public static function __callStatic($method, $parameters)
114: {
115: return call_user_func_array(array(static::driver(), $method), $parameters);
116: }
117:
118: }
119: