|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Moka\Builder; |
|
5
|
|
|
|
|
6
|
|
|
use Moka\Exception\InvalidArgumentException; |
|
7
|
|
|
use Moka\Exception\InvalidIdentifierException; |
|
8
|
|
|
use Moka\Exception\MockNotCreatedException; |
|
9
|
|
|
use Moka\Factory\ProxyGeneratorFactory; |
|
10
|
|
|
use Moka\Proxy\ProxyContainer; |
|
11
|
|
|
use Moka\Proxy\ProxyInterface; |
|
12
|
|
|
use Moka\Strategy\MockingStrategyInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ProxyBuilder |
|
16
|
|
|
* @package Moka\Builder |
|
17
|
|
|
*/ |
|
18
|
|
|
class ProxyBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ProxyContainer |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $container; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var MockingStrategyInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $mockingStrategy; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* ProxyBuilder constructor. |
|
32
|
|
|
* @param MockingStrategyInterface $mockingStrategy |
|
33
|
|
|
*/ |
|
34
|
6 |
|
public function __construct(MockingStrategyInterface $mockingStrategy) |
|
35
|
|
|
{ |
|
36
|
6 |
|
$this->mockingStrategy = $mockingStrategy; |
|
37
|
6 |
|
$this->reset(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
7 |
|
public function reset(): void |
|
44
|
|
|
{ |
|
45
|
7 |
|
$this->container = new ProxyContainer(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $fqcnOrAlias |
|
50
|
|
|
* @param string|null $alias |
|
51
|
|
|
* @return ProxyInterface |
|
52
|
|
|
* |
|
53
|
|
|
* @throws MockNotCreatedException |
|
54
|
|
|
* @throws InvalidIdentifierException |
|
55
|
|
|
* @throws InvalidArgumentException |
|
56
|
|
|
* @throws \ReflectionException |
|
57
|
|
|
*/ |
|
58
|
8 |
|
public function getProxy(string $fqcnOrAlias, string $alias = null): ProxyInterface |
|
59
|
|
|
{ |
|
60
|
8 |
|
if ($this->container->has($fqcnOrAlias)) { |
|
61
|
1 |
|
return $this->container->get($fqcnOrAlias); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
8 |
|
if (null === $alias) { |
|
65
|
3 |
|
return $this->buildProxy($fqcnOrAlias); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
if (!$this->container->has($alias)) { |
|
69
|
5 |
|
$this->container->set($alias, $this->buildProxy($fqcnOrAlias)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
return $this->container->get($alias); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $fqcn |
|
77
|
|
|
* @return ProxyInterface |
|
78
|
|
|
* |
|
79
|
|
|
* @throws \ReflectionException |
|
80
|
|
|
* @throws InvalidArgumentException |
|
81
|
|
|
* @throws MockNotCreatedException |
|
82
|
|
|
*/ |
|
83
|
8 |
|
protected function buildProxy(string $fqcn): ProxyInterface |
|
84
|
|
|
{ |
|
85
|
8 |
|
return ProxyGeneratorFactory::get($this->mockingStrategy)->get($fqcn); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|