|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains the BaseProxy class. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2017 Attila Fulop |
|
6
|
|
|
* @author Attila Fulop |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @since 2017-05-24 |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
namespace Konekt\Concord\Proxies; |
|
14
|
|
|
|
|
15
|
|
|
use Konekt\Concord\Contracts\Concord; |
|
16
|
|
|
use LogicException; |
|
17
|
|
|
|
|
18
|
|
|
abstract class BaseProxy |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $contract; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
protected static $instances = []; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Concord */ |
|
27
|
|
|
public $concord; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Repository constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Concord $concord |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Concord $concord = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->concord = $concord ?: app('concord'); |
|
37
|
|
|
|
|
38
|
|
|
if (empty($this->contract)) { |
|
39
|
|
|
$this->contract = $this->guessContract(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!interface_exists($this->contract)) { |
|
43
|
|
|
throw new LogicException( |
|
44
|
|
|
sprintf('The proxy %s has a non-existent contract class: `%s`', |
|
45
|
|
|
static::class, $this->contract |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* This is a method where the dark word 'static' has 7 occurrences |
|
53
|
|
|
* |
|
54
|
|
|
* @return BaseProxy |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getInstance() |
|
57
|
|
|
{ |
|
58
|
|
|
if (!isset(static::$instances[static::class])) { |
|
59
|
|
|
static::$instances[static::class] = new static(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return static::$instances[static::class]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Resets the proxy by removing the class instance. |
|
67
|
|
|
* Shouldn't be used in application code. |
|
68
|
|
|
* |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function __reset() |
|
71
|
|
|
{ |
|
72
|
|
|
// This is only needed to ensure that in the rare case when |
|
73
|
|
|
// the app instance gets replaced along with the Concord |
|
74
|
|
|
// singleton in runtime, no stale concord survives it |
|
75
|
|
|
if (isset(static::$instances[static::class])) { |
|
76
|
|
|
unset(static::$instances[static::class]); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $method |
|
82
|
|
|
* @param $parameters |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function __callStatic($method, $parameters) |
|
87
|
|
|
{ |
|
88
|
|
|
return call_user_func(static::getInstance()->targetClass() . '::' . $method, ...$parameters); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Try guessing the associated contract class for a concrete proxy class |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
abstract protected function guessContract(); |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the resolved class |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
abstract protected function targetClass() : string; |
|
104
|
|
|
} |
|
105
|
|
|
|