manager.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php namespace Laravel\Cache;
  2. use Laravel\Facade;
  3. use Laravel\Container;
  4. class Manager_Facade extends Facade {
  5. public static $resolve = 'cache';
  6. }
  7. class Manager {
  8. /**
  9. * All of the active cache drivers.
  10. *
  11. * @var Cache\Driver
  12. */
  13. public $drivers = array();
  14. /**
  15. * The application IoC container.
  16. *
  17. * @var Container
  18. */
  19. private $container;
  20. /**
  21. * The default cache driver.
  22. *
  23. * @var string
  24. */
  25. private $default;
  26. /**
  27. * Create a new cache manager instance.
  28. *
  29. * @param Container $container
  30. * @return void
  31. */
  32. public function __construct(Container $container, $default)
  33. {
  34. $this->default = $default;
  35. $this->container = $container;
  36. }
  37. /**
  38. * Get a cache driver instance.
  39. *
  40. * If no driver name is specified, the default cache driver will be returned
  41. * as defined in the cache configuration file.
  42. *
  43. * @param string $driver
  44. * @return Cache\Driver
  45. */
  46. public function driver($driver = null)
  47. {
  48. if (is_null($driver)) $driver = $this->default;
  49. if ( ! array_key_exists($driver, $this->drivers))
  50. {
  51. if ( ! $this->container->registered('laravel.cache.'.$driver))
  52. {
  53. throw new \Exception("Cache driver [$driver] is not supported.");
  54. }
  55. return $this->drivers[$driver] = $this->container->resolve('laravel.cache.'.$driver);
  56. }
  57. return $this->drivers[$driver];
  58. }
  59. /**
  60. * Pass all other methods to the default cache driver.
  61. *
  62. * Passing method calls to the driver instance provides a convenient API for the developer
  63. * when always using the default cache driver.
  64. */
  65. public function __call($method, $parameters)
  66. {
  67. return call_user_func_array(array($this->driver(), $method), $parameters);
  68. }
  69. }