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