1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Wolnosciowiec\WebProxy\Factory; |
4
|
|
|
|
5
|
|
|
use DI\Container; |
6
|
|
|
use Doctrine\Common\Cache\Cache; |
7
|
|
|
use Wolnosciowiec\WebProxy\Providers\Proxy\CachedProvider; |
8
|
|
|
use Wolnosciowiec\WebProxy\Providers\Proxy\ChainProvider; |
9
|
|
|
use Wolnosciowiec\WebProxy\Providers\Proxy\ProxyProviderInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Constructs the provider object |
13
|
|
|
* ------------------------------ |
14
|
|
|
* |
15
|
|
|
* @package Wolnosciowiec\WebProxy\Factory |
16
|
|
|
*/ |
17
|
|
|
class ProxyProviderFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string $providersNames |
21
|
|
|
*/ |
22
|
|
|
private $providersNames = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Cache $cache |
26
|
|
|
*/ |
27
|
|
|
private $cache; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Container $container |
31
|
|
|
*/ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
public function __construct(string $providerNames, Cache $cache, Container $container) |
35
|
|
|
{ |
36
|
|
|
$this->providersNames = $providerNames; |
37
|
|
|
$this->cache = $cache; |
38
|
|
|
$this->container = $container; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function create(): ProxyProviderInterface |
42
|
|
|
{ |
43
|
|
|
$providers = new ChainProvider($this->buildProviders()); |
44
|
|
|
return new CachedProvider($this->cache, $providers); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws \Exception |
49
|
|
|
* @return ProxyProviderInterface[] |
50
|
|
|
*/ |
51
|
|
|
private function buildProviders() |
52
|
|
|
{ |
53
|
|
|
$providers = []; |
54
|
|
|
$names = str_replace(' ', '', $this->providersNames); |
55
|
|
|
$names = array_filter(explode(',', $names)); |
56
|
|
|
|
57
|
|
|
$defaultNamespace = '\\Wolnosciowiec\\WebProxy\\Providers\\Proxy\\'; |
58
|
|
|
|
59
|
|
|
foreach ($names as $name) { |
60
|
|
|
if (class_exists($defaultNamespace . $name)) { |
61
|
|
|
$fullName = $defaultNamespace . $name; |
62
|
|
|
} |
63
|
|
|
elseif (class_exists($name)) { |
64
|
|
|
$fullName = $name; |
65
|
|
|
} |
66
|
|
|
else { |
67
|
|
|
throw new \Exception('Invalid provider name "' . $name . '", please check the configuration'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$providers[$fullName] = $this->container->get($fullName); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $providers; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|