manager.php 2.01 KB
Newer Older
1 2 3 4 5
<?php namespace Laravel\Cache;

use Laravel\Redis;
use Laravel\Config;
use Laravel\Memcached;
6 7

class Manager {
Taylor Otwell committed
8 9 10 11

	/**
	 * All of the active cache drivers.
	 *
12
	 * @var array
Taylor Otwell committed
13
	 */
14
	protected static $drivers = array();
Taylor Otwell committed
15 16 17 18

	/**
	 * Get a cache driver instance.
	 *
19 20
	 * If no driver name is specified, the default cache driver will
	 * be returned as defined in the cache configuration file.
Taylor Otwell committed
21
	 *
22 23 24 25 26 27 28 29
	 * <code>
	 *		// Get the default cache driver instance
	 *		$driver = Cache::driver();
	 *
	 *		// Get a specific cache driver instance by name
	 *		$driver = Cache::driver('memcached');
	 * </code>
	 *
Taylor Otwell committed
30 31 32
	 * @param  string        $driver
	 * @return Cache\Driver
	 */
33
	public static function driver($driver = null)
Taylor Otwell committed
34
	{
35
		if (is_null($driver)) $driver = Config::get('cache.driver');
Taylor Otwell committed
36

37
		if ( ! array_key_exists($driver, static::$drivers))
Taylor Otwell committed
38
		{
Taylor Otwell committed
39
			return static::$drivers[$driver] = static::factory($driver);
Taylor Otwell committed
40 41
		}

42
		return static::$drivers[$driver];
Taylor Otwell committed
43 44 45
	}

	/**
Taylor Otwell committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	 * Create a new cache driver instance.
	 *
	 * @param  string  $driver
	 * @return Driver
	 */
	protected static function factory($driver)
	{
		switch ($driver)
		{
			case 'apc':
				return new Drivers\APC(Config::get('cache.key'));

			case 'file':
				return new Drivers\File(CACHE_PATH);

			case 'memcached':
				return new Drivers\Memcached(Memcached::instance(), Config::get('cache.key'));

			case 'redis':
				return new Drivers\Redis(Redis::db());

			default:
Phill Sparks committed
68
				throw new \DomainException("Cache driver {$driver} is not supported.");
Taylor Otwell committed
69 70 71 72
		}
	}

	/**
Taylor Otwell committed
73
	 * Pass all other methods to the default cache driver.
Taylor Otwell committed
74
	 *
75 76
	 * Passing method calls to the driver instance provides a convenient API
	 * for the developer when always using the default cache driver.
77 78 79 80 81 82 83 84
	 *
	 * <code>
	 *		// Call the "get" method on the default driver
	 *		$name = Cache::get('name');
	 *
	 *		// Call the "put" method on the default driver
	 *		Cache::put('name', 'Taylor', 15);
	 * </code>
Taylor Otwell committed
85
	 */
86
	public static function __callStatic($method, $parameters)
Taylor Otwell committed
87
	{
88
		return call_user_func_array(array(static::driver(), $method), $parameters);
Taylor Otwell committed
89 90
	}

91
}