1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BrightNucleus ChainMail Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus/ChainMail |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\ChainMail\Support; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\ChainMail\Exception\FailedToInstantiateClass; |
15
|
|
|
use BrightNucleus\ChainMail\Exception\FailedToInstantiateFactory; |
16
|
|
|
use BrightNucleus\Config\ConfigInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Factory. |
20
|
|
|
* |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
* |
23
|
|
|
* @package BrightNucleus\ChainMail |
24
|
|
|
* @author Alain Schlesser <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class Factory |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Configuration Settings. |
31
|
|
|
* |
32
|
|
|
* @since 1.0.0 |
33
|
|
|
* |
34
|
|
|
* @var ConfigInterface |
35
|
|
|
*/ |
36
|
|
|
protected $config; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Type of element the factory wants to create. |
40
|
|
|
* |
41
|
|
|
* @since 1.0.0 |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $element; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Instantiate a Factory object. |
49
|
|
|
* |
50
|
|
|
* @since 1.0.0 |
51
|
|
|
* |
52
|
|
|
* @param ConfigInterface $config Configuration settings. |
53
|
|
|
* @param string $element The type of element to instantiate a factory for. |
54
|
|
|
* |
55
|
|
|
* @throws FailedToInstantiateFactory When an unknown element type is requested. |
56
|
|
|
*/ |
57
|
16 |
|
public function __construct(ConfigInterface $config, $element) |
58
|
|
|
{ |
59
|
|
|
|
60
|
16 |
|
$this->config = $config; |
61
|
|
|
|
62
|
16 |
|
if ( ! $this->config->hasKey($element)) { |
63
|
|
|
throw new FailedToInstantiateFactory(sprintf( |
64
|
|
|
'Could not instantiate Factory for unknown Element Type "%1$s".', |
65
|
|
|
$element |
66
|
|
|
)); |
67
|
|
|
} |
68
|
|
|
|
69
|
16 |
|
$this->element = $element; |
70
|
16 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create and return a new instance of an element. |
74
|
|
|
* |
75
|
|
|
* @since 1.0.0 |
76
|
|
|
* |
77
|
|
|
* @param string $type Type of element to create. |
78
|
|
|
* @param mixed $arguments Optional. Arguments to pass to the object. |
79
|
|
|
* |
80
|
|
|
* @return object New instance of the requested class. |
81
|
|
|
* @throws FailedToInstantiateClass If an unknown element type is requested. |
82
|
|
|
*/ |
83
|
16 |
|
public function create($type, $arguments = null) |
84
|
|
|
{ |
85
|
|
|
|
86
|
16 |
|
$classMap = $this->config[$this->element]; |
87
|
|
|
|
88
|
16 |
|
if ( ! array_key_exists($type, $classMap)) { |
89
|
|
|
throw new FailedToInstantiateClass(sprintf( |
90
|
|
|
'Could not create object, unknown Type "%1$s" for "%2$s" elements.', |
91
|
|
|
$type, |
92
|
|
|
$this->element |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
16 |
|
$className = $classMap[$type]['class_name']; |
97
|
|
|
|
98
|
16 |
|
return new $className($this->config, $arguments); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|