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