|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Trucker\Framework; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Trucker\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
abstract class FactoryDriver |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The IoC Container. |
|
12
|
|
|
* |
|
13
|
|
|
* @var Container |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $app; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Build a new FactoryDriver. |
|
19
|
|
|
* |
|
20
|
|
|
* @param Container $app |
|
21
|
|
|
*/ |
|
22
|
7 |
|
public function __construct(Container $app) |
|
23
|
|
|
{ |
|
24
|
7 |
|
$this->app = $app; |
|
25
|
7 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Function to use other defined abstract methods to |
|
29
|
|
|
* use a standard naming-convention based method of |
|
30
|
|
|
* building classes by the factory. |
|
31
|
|
|
* |
|
32
|
|
|
* @return mixed - anything a subclass factory can build |
|
33
|
|
|
* |
|
34
|
|
|
* @throws \InvalidArgumentException |
|
35
|
|
|
*/ |
|
36
|
53 |
|
public function build() |
|
37
|
|
|
{ |
|
38
|
|
|
//get the driver to build |
|
39
|
53 |
|
$driver = $this->getDriverConfigValue(); |
|
40
|
|
|
|
|
41
|
|
|
//return null if there's nothing to build. |
|
42
|
53 |
|
if (is_null($driver)) { |
|
43
|
18 |
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
//get the prefix, suffix and namespace for the driver class |
|
47
|
53 |
|
$prefix = $this->getDriverNamePrefix(); |
|
48
|
53 |
|
$suffix = $this->getDriverNameSuffix(); |
|
49
|
53 |
|
$ns = $this->getDriverNamespace(); |
|
50
|
|
|
|
|
51
|
|
|
//use naming convention to convert the driver name |
|
52
|
|
|
//into a fully quantified class name |
|
53
|
53 |
|
$klass = Str::studly($driver); |
|
54
|
53 |
|
$fqcn = "{$ns}\\{$prefix}{$klass}{$suffix}"; |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
53 |
|
$refl = new \ReflectionClass($fqcn); |
|
58
|
7 |
|
} catch (\ReflectionException $e) { |
|
59
|
7 |
|
throw new \InvalidArgumentException("Unsupported driver [{$driver}] to load [{$fqcn}]"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
//make sure the driver implements the interface properly |
|
63
|
46 |
|
$interface = $this->getDriverInterface(); |
|
64
|
46 |
|
if (!$refl->implementsInterface($interface)) { |
|
65
|
|
|
throw new \InvalidArgumentException("Unsupported interface [{$driver}] must implement [{$interface}]"); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
46 |
|
$instance = $refl->newInstanceArgs($this->getDriverArgumentsArray()); |
|
69
|
|
|
|
|
70
|
46 |
|
if (is_null($instance)) { |
|
71
|
|
|
throw new \InvalidArgumentException("Driver [{$driver}] failed to create an instance"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
46 |
|
return $instance; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Function to return a string representaion of the namespace |
|
79
|
|
|
* that all classes built by the factory should be contained within. |
|
80
|
|
|
* |
|
81
|
|
|
* @return string - namespace string |
|
82
|
|
|
*/ |
|
83
|
|
|
abstract public function getDriverNamespace(); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Function to return the interface that the driver's produced |
|
87
|
|
|
* by the factory must implement. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string Interface |
|
90
|
|
|
*/ |
|
91
|
|
|
abstract public function getDriverInterface(); |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Function to return a string that should be suffixed |
|
95
|
|
|
* to the studly-cased driver name of all the drivers |
|
96
|
|
|
* that the factory can return. |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
abstract public function getDriverNameSuffix(); |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Function to return a string that should be prefixed |
|
104
|
|
|
* to the studly-cased driver name of all the drivers |
|
105
|
|
|
* that the factory can return. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
abstract public function getDriverNamePrefix(); |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Function to return an array of arguments that should be |
|
113
|
|
|
* passed to the constructor of a new driver instance. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
abstract public function getDriverArgumentsArray(); |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Function to return the string representation of the driver |
|
121
|
|
|
* itslef based on a value fetched from the config file. This |
|
122
|
|
|
* function will itself access the config, and return the driver |
|
123
|
|
|
* setting. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
abstract public function getDriverConfigValue(); |
|
128
|
|
|
} |
|
129
|
|
|
|