Files
spa-api/laravel/cache/manager.php

65 lines
1.6 KiB
PHP
Raw Normal View History

2011-10-12 21:55:05 -05:00
<?php namespace Laravel\Cache; use Laravel\IoC;
class Manager {
2011-08-18 19:56:29 -05:00
/**
* All of the active cache drivers.
*
2011-09-20 23:36:13 -05:00
* @var array
2011-08-18 19:56:29 -05:00
*/
2011-09-20 23:36:13 -05:00
protected static $drivers = array();
2011-08-18 19:56:29 -05:00
/**
* Get a cache driver instance.
*
2011-10-15 14:04:11 -05:00
* If no driver name is specified, the default cache driver will be
* returned as defined in the cache configuration file.
2011-08-18 19:56:29 -05:00
*
2011-09-28 22:51:07 -05:00
* <code>
* // Get the default cache driver instance
* $driver = Cache::driver();
*
* // Get a specific cache driver instance by name
* $driver = Cache::driver('memcached');
* </code>
*
2011-08-18 19:56:29 -05:00
* @param string $driver
* @return Cache\Driver
*/
2011-09-20 23:36:13 -05:00
public static function driver($driver = null)
2011-08-18 19:56:29 -05:00
{
2011-09-20 23:36:13 -05:00
if (is_null($driver)) $driver = Config::get('cache.default');
2011-08-18 19:56:29 -05:00
2011-09-20 23:36:13 -05:00
if ( ! array_key_exists($driver, static::$drivers))
2011-08-18 19:56:29 -05:00
{
2011-09-20 23:36:13 -05:00
if ( ! IoC::container()->registered('laravel.cache.'.$driver))
2011-08-18 19:56:29 -05:00
{
throw new \Exception("Cache driver [$driver] is not supported.");
2011-08-18 19:56:29 -05:00
}
2011-09-28 22:51:07 -05:00
return static::$drivers[$driver] = IoC::container()->core('cache.'.$driver);
2011-08-18 19:56:29 -05:00
}
2011-09-20 23:36:13 -05:00
return static::$drivers[$driver];
2011-08-18 19:56:29 -05:00
}
/**
2011-08-19 20:12:39 -05:00
* Pass all other methods to the default cache driver.
2011-08-18 19:56:29 -05:00
*
2011-10-15 14:04:11 -05:00
* Passing method calls to the driver instance provides a convenient API
* for the developer when always using the default cache driver.
2011-09-28 22:51:07 -05:00
*
* <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>
2011-08-18 19:56:29 -05:00
*/
2011-09-20 23:36:13 -05:00
public static function __callStatic($method, $parameters)
2011-08-18 19:56:29 -05:00
{
2011-09-20 23:36:13 -05:00
return call_user_func_array(array(static::driver(), $method), $parameters);
2011-08-18 19:56:29 -05:00
}
}