1: <?php namespace Illuminate\Support;
2:
3: use Closure;
4:
5: abstract class Manager {
6:
7: /**
8: * The application instance.
9: *
10: * @var \Illuminate\Foundation\Application
11: */
12: protected $app;
13:
14: /**
15: * The registered custom driver creators.
16: *
17: * @var array
18: */
19: protected $customCreators = array();
20:
21: /**
22: * The array of created "drivers".
23: *
24: * @var array
25: */
26: protected $drivers = array();
27:
28: /**
29: * Create a new manager instance.
30: *
31: * @param \Illuminate\Foundation\Application $app
32: * @return void
33: */
34: public function __construct($app)
35: {
36: $this->app = $app;
37: }
38:
39: /**
40: * Get a driver instance.
41: *
42: * @param string $driver
43: * @return mixed
44: */
45: public function driver($driver = null)
46: {
47: $driver = $driver ?: $this->getDefaultDriver();
48:
49: // If the given driver has not been created before, we will create the instances
50: // here and cache it so we can return it next time very quickly. If their is
51: // already a driver created by this name, we'll just return that instance.
52: if ( ! isset($this->drivers[$driver]))
53: {
54: $this->drivers[$driver] = $this->createDriver($driver);
55: }
56:
57: return $this->drivers[$driver];
58: }
59:
60: /**
61: * Create a new driver instance.
62: *
63: * @param string $driver
64: * @return mixed
65: */
66: protected function createDriver($driver)
67: {
68: $method = 'create'.ucfirst($driver).'Driver';
69:
70: // We'll check to see if a creator method exists for the given driver. If not we
71: // will check for a custom driver creator, which allows developers to create
72: // drivers using their own customized driver creator Closure to create it.
73: if (isset($this->customCreators[$driver]))
74: {
75: return $this->callCustomCreator($driver);
76: }
77: elseif (method_exists($this, $method))
78: {
79: return $this->$method();
80: }
81:
82: throw new \InvalidArgumentException("Driver [$driver] not supported.");
83: }
84:
85: /**
86: * Call a custom driver creator.
87: *
88: * @param string $driver
89: * @return mixed
90: */
91: protected function callCustomCreator($driver)
92: {
93: return $this->customCreators[$driver]($this->app);
94: }
95:
96: /**
97: * Register a custom driver creator Closure.
98: *
99: * @param string $driver
100: * @param Closure $callback
101: * @return void
102: */
103: public function extend($driver, Closure $callback)
104: {
105: $this->customCreators[$driver] = $callback;
106: }
107:
108: /**
109: * Get all of the created "drivers".
110: *
111: * @return array
112: */
113: public function getDrivers()
114: {
115: return $this->drivers;
116: }
117:
118: /**
119: * Dynamically call the default driver instance.
120: *
121: * @param string $method
122: * @param array $parameters
123: * @return mixed
124: */
125: public function __call($method, $parameters)
126: {
127: return call_user_func_array(array($this->driver(), $method), $parameters);
128: }
129:
130: }