manager.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php namespace Laravel\Cache; use Laravel\IoC, Laravel\Config;
  2. class Manager {
  3. /**
  4. * All of the active cache drivers.
  5. *
  6. * @var array
  7. */
  8. protected static $drivers = array();
  9. /**
  10. * Get a cache driver instance.
  11. *
  12. * If no driver name is specified, the default cache driver will
  13. * be returned as defined in the cache configuration file.
  14. *
  15. * <code>
  16. * // Get the default cache driver instance
  17. * $driver = Cache::driver();
  18. *
  19. * // Get a specific cache driver instance by name
  20. * $driver = Cache::driver('memcached');
  21. * </code>
  22. *
  23. * @param string $driver
  24. * @return Cache\Driver
  25. */
  26. public static function driver($driver = null)
  27. {
  28. if (is_null($driver)) $driver = Config::get('cache.driver');
  29. if ( ! array_key_exists($driver, static::$drivers))
  30. {
  31. if ( ! IoC::registered("laravel.cache.{$driver}"))
  32. {
  33. throw new \Exception("Cache driver [$driver] is not supported.");
  34. }
  35. return static::$drivers[$driver] = IoC::core("cache.{$driver}");
  36. }
  37. return static::$drivers[$driver];
  38. }
  39. /**
  40. * Pass all other methods to the default cache driver.
  41. *
  42. * Passing method calls to the driver instance provides a convenient API
  43. * for the developer when always using the default cache driver.
  44. *
  45. * <code>
  46. * // Call the "get" method on the default driver
  47. * $name = Cache::get('name');
  48. *
  49. * // Call the "put" method on the default driver
  50. * Cache::put('name', 'Taylor', 15);
  51. * </code>
  52. */
  53. public static function __callStatic($method, $parameters)
  54. {
  55. return call_user_func_array(array(static::driver(), $method), $parameters);
  56. }
  57. }