1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Provider; |
7
|
|
|
use seregazhuk\PinterestBot\Contracts\RequestInterface; |
8
|
|
|
use seregazhuk\PinterestBot\Contracts\ResponseInterface; |
9
|
|
|
use seregazhuk\PinterestBot\Exceptions\WrongProviderException; |
10
|
|
|
use seregazhuk\PinterestBot\Contracts\ProvidersContainerInterface; |
11
|
|
|
|
12
|
|
|
class ProvidersContainer implements ProvidersContainerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* References to the request and response classes that travels |
16
|
|
|
* through the application |
17
|
|
|
* |
18
|
|
|
* @var RequestInterface |
19
|
|
|
*/ |
20
|
|
|
protected $request; |
21
|
|
|
/** |
22
|
|
|
* @var ResponseInterface |
23
|
|
|
*/ |
24
|
|
|
protected $response; |
25
|
|
|
|
26
|
|
|
const PROVIDERS_NAMESPACE = "seregazhuk\\PinterestBot\\Api\\Providers\\"; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A array containing the cached providers |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $providers = []; |
34
|
|
|
|
35
|
|
|
public function __construct(RequestInterface $request, ResponseInterface $response) |
36
|
|
|
{ |
37
|
|
|
$this->request = $request; |
38
|
|
|
$this->response = $response; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets provider object by name. If there is no such provider |
43
|
|
|
* in providers array, it will try to create it, then save |
44
|
|
|
* it, and then return. |
45
|
|
|
* |
46
|
|
|
* @param string $provider |
47
|
|
|
* @return Provider |
48
|
|
|
* @throws WrongProviderException |
49
|
|
|
*/ |
50
|
|
|
public function getProvider($provider) |
51
|
|
|
{ |
52
|
|
|
$provider = strtolower($provider); |
53
|
|
|
|
54
|
|
|
// Check if an instance has already been initiated |
55
|
|
|
if ( ! isset($this->providers[$provider])) { |
56
|
|
|
$this->addProvider($provider); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->providers[$provider]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates provider by class name, and if success saves |
64
|
|
|
* it to providers array. Provider class must be in PROVIDERS_NAMESPACE. |
65
|
|
|
* |
66
|
|
|
* @param string $provider |
67
|
|
|
* @throws WrongProviderException |
68
|
|
|
*/ |
69
|
|
|
private function addProvider($provider) |
70
|
|
|
{ |
71
|
|
|
$className = self::PROVIDERS_NAMESPACE.ucfirst($provider); |
72
|
|
|
|
73
|
|
|
if ( ! class_exists($className)) { |
74
|
|
|
throw new WrongProviderException("Provider $className not found."); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->providers[$provider] = $this->buildProvider($className); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Build Provider object with reflection API. |
82
|
|
|
* |
83
|
|
|
* @param string $className |
84
|
|
|
* @return object |
85
|
|
|
* @throws WrongProviderException |
86
|
|
|
*/ |
87
|
|
|
private function buildProvider($className) |
88
|
|
|
{ |
89
|
|
|
$ref = new ReflectionClass($className); |
90
|
|
|
if ( ! $ref->isInstantiable()) { |
91
|
|
|
throw new WrongProviderException("Provider $className is not instantiable."); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $ref->newInstanceArgs([$this->request, $this->response]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return RequestInterface |
99
|
|
|
*/ |
100
|
|
|
public function getRequest() |
101
|
|
|
{ |
102
|
|
|
return $this->request; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return ResponseInterface |
107
|
|
|
*/ |
108
|
|
|
public function getResponse() |
109
|
|
|
{ |
110
|
|
|
return $this->response; |
111
|
|
|
} |
112
|
|
|
} |