|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @file LazyCalls.php |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4+ |
|
6
|
|
|
* |
|
7
|
|
|
* @author Yancharuk Alexander <alex at itvault dot info> |
|
8
|
|
|
* @copyright © 2012-2015 Alexander Yancharuk <alex at itvault at info> |
|
9
|
|
|
* @date 2015-12-26 13:38 |
|
10
|
|
|
* @license The BSD 3-Clause License |
|
11
|
|
|
* <http://opensource.org/licenses/BSD-3-Clause> |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Traits; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use Veles\Traits\SingletonInstance; |
|
18
|
|
|
|
|
19
|
|
|
trait LazyCalls |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
protected static $calls = []; |
|
23
|
|
|
|
|
24
|
|
|
use SingletonInstance; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Lazy calls invocation |
|
28
|
|
|
*/ |
|
29
|
2 |
|
protected static function invokeLazyCalls() |
|
30
|
|
|
{ |
|
31
|
2 |
|
foreach (static::$calls as $call) { |
|
32
|
2 |
|
call_user_func_array( |
|
33
|
2 |
|
[static::$instance->getDriver(), $call['method']], |
|
34
|
2 |
|
$call['arguments'] |
|
35
|
2 |
|
); |
|
36
|
2 |
|
} |
|
37
|
2 |
|
static::$calls = []; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public static function instance() |
|
44
|
|
|
{ |
|
45
|
2 |
|
if (null === static::$instance) { |
|
46
|
2 |
|
$class = get_called_class(); |
|
47
|
|
|
|
|
48
|
2 |
|
static::$instance = new $class; |
|
49
|
2 |
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
if ([] !== static::$calls) { |
|
52
|
2 |
|
static::invokeLazyCalls(); |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
return static::$instance; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Collect calls which will be invoked during first real query |
|
60
|
|
|
* |
|
61
|
|
|
* @param $method |
|
62
|
|
|
* @param $arguments |
|
63
|
|
|
* |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
* @throws Exception |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function __call($method, $arguments) |
|
68
|
|
|
{ |
|
69
|
2 |
|
if (!method_exists($this->getDriver(), $method)) { |
|
|
|
|
|
|
70
|
1 |
|
throw new Exception('Calling non existent method!'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
static::addCall($method, $arguments); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Save calls for future invocation |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $method Method name that should be called |
|
80
|
|
|
* @param array $arguments Method arguments |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public static function addCall($method, array $arguments = []) |
|
83
|
|
|
{ |
|
84
|
2 |
|
static::$calls[] = [ |
|
85
|
2 |
|
'method' => $method, |
|
86
|
|
|
'arguments' => $arguments |
|
87
|
2 |
|
]; |
|
88
|
2 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: