1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DeGraciaMathieu\Manager; |
4
|
|
|
|
5
|
|
|
use DeGraciaMathieu\Manager\Exceptions\DriverResolutionException; |
6
|
|
|
|
7
|
|
|
abstract class Manager |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Driver aggregator instance. |
11
|
|
|
* |
12
|
|
|
* @var \DeGraciaMathieu\Manager\Aggregator |
13
|
|
|
*/ |
14
|
|
|
protected $aggregator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Use cached driver cache. |
18
|
|
|
* |
19
|
|
|
* @var boolean |
20
|
|
|
*/ |
21
|
|
|
protected $cached = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new Manager instance. |
25
|
|
|
*/ |
26
|
3 |
|
public function __construct() |
27
|
|
|
{ |
28
|
3 |
|
$this->aggregator = new Aggregator(); |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the default driver name. |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
abstract public function getDefaultDriver(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get a driver instance. |
40
|
|
|
* |
41
|
|
|
* @param string $name |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
3 |
|
public function driver($name = null) |
45
|
|
|
{ |
46
|
3 |
|
$name = $name ?: $this->getDefaultDriver(); |
47
|
|
|
|
48
|
3 |
|
return $this->resolve($name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Resolve the given driver instance. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @return mixed |
56
|
|
|
* |
57
|
|
|
* @throws \DeGraciaMathieu\Manager\Exceptions\DriverResolutionException |
58
|
|
|
* @throws \DeGraciaMathieu\Manager\Exceptions\DriverOverwrittenException |
59
|
|
|
*/ |
60
|
3 |
|
public function resolve(string $name) |
61
|
|
|
{ |
62
|
3 |
|
return $this->cached |
63
|
1 |
|
? $this->makeCached($name) |
64
|
3 |
|
: $this->make($name); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Make a driver instance. |
69
|
|
|
* |
70
|
|
|
* @param string $name |
71
|
|
|
* @return mixed |
72
|
|
|
* |
73
|
|
|
* @throws \DeGraciaMathieu\Manager\Exceptions\DriverResolutionException |
74
|
|
|
*/ |
75
|
3 |
|
public function make(string $name) |
76
|
|
|
{ |
77
|
3 |
|
return $this->makeDriverInstance($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Make a cached driver instance. |
82
|
|
|
* |
83
|
|
|
* @param string $name |
84
|
|
|
* @return mixed |
85
|
|
|
* |
86
|
|
|
* @throws \DeGraciaMathieu\Manager\Exceptions\DriverResolutionException |
87
|
|
|
* @throws \DeGraciaMathieu\Manager\Exceptions\DriverOverwrittenException |
88
|
|
|
*/ |
89
|
1 |
|
public function makeCached(string $name) |
90
|
|
|
{ |
91
|
1 |
|
if ($this->aggregator->has($name)) { |
92
|
1 |
|
return $this->aggregator->get($name); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$driver = $this->make($name); |
96
|
1 |
|
$this->aggregator->set($name, $driver); |
97
|
|
|
|
98
|
1 |
|
return $driver; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Make a new driver instance. |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @return mixed |
106
|
|
|
* |
107
|
|
|
* @throws \DeGraciaMathieu\Manager\Exceptions\DriverResolutionException |
108
|
|
|
*/ |
109
|
3 |
|
protected function makeDriverInstance(string $name) |
110
|
|
|
{ |
111
|
3 |
|
$method = "create".ucfirst(strtolower($name))."Driver"; |
112
|
|
|
|
113
|
3 |
|
if ( ! method_exists($this, $method)) { |
114
|
1 |
|
throw new DriverResolutionException("Driver [$name] not supported."); |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
return $this->{$method}(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Dynamically call the default driver instance. |
122
|
|
|
* |
123
|
|
|
* @param string $method |
124
|
|
|
* @param array $parameters |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
1 |
|
public function __call($method, $parameters) |
128
|
|
|
{ |
129
|
1 |
|
return $this->driver()->{$method}(...$parameters); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Dynamically access driver. |
134
|
|
|
* |
135
|
|
|
* @param string $name |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
public function __get(string $name) |
139
|
|
|
{ |
140
|
|
|
return $this->driver($name); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|