1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magister\Services\Support; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Manager. |
9
|
|
|
*/ |
10
|
|
|
abstract class Manager |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The application instance. |
14
|
|
|
* |
15
|
|
|
* @var \Magister\Magister |
16
|
|
|
*/ |
17
|
|
|
protected $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The array of created "drivers". |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $drivers = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new manager instance. |
28
|
|
|
* |
29
|
|
|
* @param \Magister\Magister $app |
30
|
|
|
*/ |
31
|
|
|
public function __construct($app) |
32
|
|
|
{ |
33
|
|
|
$this->app = $app; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the default driver name. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
abstract public function getDefaultDriver(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the driver instance. |
45
|
|
|
* |
46
|
|
|
* @param string $driver |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function driver($driver = null) |
51
|
|
|
{ |
52
|
|
|
$driver = $driver ?: $this->getDefaultDriver(); |
53
|
|
|
|
54
|
|
|
if (!isset($this->drivers[$driver])) { |
55
|
|
|
$this->drivers[$driver] = $this->createDriver($driver); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->drivers[$driver]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a new driver instance. |
63
|
|
|
* |
64
|
|
|
* @param string $driver |
65
|
|
|
* |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
protected function createDriver($driver) |
71
|
|
|
{ |
72
|
|
|
$method = 'create'.ucfirst($driver).'Driver'; |
73
|
|
|
|
74
|
|
|
if (method_exists($this, $method)) { |
75
|
|
|
return $this->{$method}(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw new InvalidArgumentException(sprintf('Driver "%s" is not supported.', $driver)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get all of the created "drivers". |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getDrivers() |
87
|
|
|
{ |
88
|
|
|
return $this->drivers; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Dynamically call the driver instance. |
93
|
|
|
* |
94
|
|
|
* @param string $method |
95
|
|
|
* @param array $parameters |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function __call($method, $parameters) |
100
|
|
|
{ |
101
|
|
|
return call_user_func_array([$this->driver(), $method], $parameters); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|