manager.php 1.5 KB

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