|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace Resta\Support;
|
|
4
|
|
|
|
|
5
|
|
|
use Resta\Contracts\MacroAbleContracts;
|
|
6
|
|
|
use Resta\Foundation\ApplicationProvider;
|
|
7
|
|
|
|
|
8
|
|
|
class Macro extends ApplicationProvider
|
|
9
|
|
|
{
|
|
10
|
|
|
/**
|
|
11
|
|
|
* @var bool $isMacro
|
|
12
|
|
|
*/
|
|
13
|
|
|
protected $isMacro = false;
|
|
14
|
|
|
|
|
15
|
|
|
/**
|
|
16
|
|
|
* @var string $macro
|
|
17
|
|
|
*/
|
|
18
|
|
|
protected $macro;
|
|
19
|
|
|
|
|
20
|
|
|
/**
|
|
21
|
|
|
* @var string $class
|
|
22
|
|
|
*/
|
|
23
|
|
|
protected $class;
|
|
24
|
|
|
|
|
25
|
|
|
/**
|
|
26
|
|
|
* @var string
|
|
27
|
|
|
*/
|
|
28
|
|
|
protected $values;
|
|
29
|
|
|
|
|
30
|
|
|
/**
|
|
31
|
|
|
* @param null|string $macro
|
|
32
|
|
|
* @param callable $callback
|
|
33
|
|
|
* @return mixed|null
|
|
34
|
|
|
*/
|
|
35
|
|
|
public function call($macro,callable $callback)
|
|
36
|
|
|
{
|
|
37
|
|
|
if(!is_null($macro)){
|
|
38
|
|
|
|
|
39
|
|
|
//get macro class from kernel macros
|
|
40
|
|
|
$macroCall = config('kernel.macros.'.$macro);
|
|
41
|
|
|
|
|
42
|
|
|
//if macroCall class is available and an object.
|
|
43
|
|
|
if(!is_null($macroCall) && Utils::isNamespaceExists($macroCall)){
|
|
44
|
|
|
return $macroCall;
|
|
45
|
|
|
}
|
|
46
|
|
|
}
|
|
47
|
|
|
|
|
48
|
|
|
return call_user_func($callback);
|
|
49
|
|
|
}
|
|
50
|
|
|
|
|
51
|
|
|
/**
|
|
52
|
|
|
* check conditions for macro
|
|
53
|
|
|
*
|
|
54
|
|
|
* @param bool $static
|
|
55
|
|
|
* @return bool
|
|
56
|
|
|
*/
|
|
57
|
|
|
protected function checkMacroConditions($static=false)
|
|
58
|
|
|
{
|
|
59
|
|
|
return is_string($this->macro) &&
|
|
60
|
|
|
Utils::isNamespaceExists($this->macro) &&
|
|
61
|
|
|
$this->checkMacroInstanceOf($static);
|
|
62
|
|
|
}
|
|
63
|
|
|
|
|
64
|
|
|
/**
|
|
65
|
|
|
* check macro instanceOf or static class
|
|
66
|
|
|
*
|
|
67
|
|
|
* @param bool $static
|
|
68
|
|
|
* @return bool
|
|
69
|
|
|
*/
|
|
70
|
|
|
protected function checkMacroInstanceOf($static=false)
|
|
71
|
|
|
{
|
|
72
|
|
|
if($static){
|
|
73
|
|
|
return true;
|
|
74
|
|
|
}
|
|
75
|
|
|
return $this->app->resolve($this->macro) instanceof MacroAbleContracts;
|
|
76
|
|
|
}
|
|
77
|
|
|
|
|
78
|
|
|
/**
|
|
79
|
|
|
* get macro object
|
|
80
|
|
|
*
|
|
81
|
|
|
* @param null|string $method
|
|
82
|
|
|
* @param callable $callback
|
|
83
|
|
|
* @return mixed
|
|
84
|
|
|
*/
|
|
85
|
|
|
public function get($method,callable $callback)
|
|
86
|
|
|
{
|
|
87
|
|
|
if($this->isMacro){
|
|
88
|
|
|
|
|
89
|
|
|
if(method_exists($resolve = $this->app->resolve($this->macro),$method)){
|
|
90
|
|
|
return $resolve->macro($this->class);
|
|
91
|
|
|
}
|
|
92
|
|
|
}
|
|
93
|
|
|
return call_user_func($callback);
|
|
94
|
|
|
}
|
|
95
|
|
|
|
|
96
|
|
|
/**
|
|
97
|
|
|
* get values for macro
|
|
98
|
|
|
*
|
|
99
|
|
|
* @return mixed
|
|
100
|
|
|
*/
|
|
101
|
|
|
public function getValues()
|
|
102
|
|
|
{
|
|
103
|
|
|
return $this->values;
|
|
104
|
|
|
}
|
|
105
|
|
|
|
|
106
|
|
|
/**
|
|
107
|
|
|
* is availability macro for class
|
|
108
|
|
|
*
|
|
109
|
|
|
* @param bool $static
|
|
110
|
|
|
* @param $class
|
|
111
|
|
|
* @return $this
|
|
112
|
|
|
*/
|
|
113
|
|
|
public function isMacro($class,$static=false)
|
|
114
|
|
|
{
|
|
115
|
|
|
// if the macro class is a valid object,
|
|
116
|
|
|
// then this macro will return a boolean value if it has the specified methode.
|
|
117
|
|
|
if($this->checkMacroConditions($static)){
|
|
118
|
|
|
|
|
119
|
|
|
$this->isMacro = true;
|
|
120
|
|
|
$this->class = $class;
|
|
121
|
|
|
}
|
|
122
|
|
|
|
|
123
|
|
|
return $this;
|
|
124
|
|
|
}
|
|
125
|
|
|
|
|
126
|
|
|
/**
|
|
127
|
|
|
* set values for macro
|
|
128
|
|
|
*
|
|
129
|
|
|
* @param $values
|
|
130
|
|
|
* @return mixed
|
|
131
|
|
|
*/
|
|
132
|
|
|
public function setValues($values)
|
|
133
|
|
|
{
|
|
134
|
|
|
return $this->values = $values;
|
|
135
|
|
|
}
|
|
136
|
|
|
|
|
137
|
|
|
/**
|
|
138
|
|
|
* check been runnable with which macro
|
|
139
|
|
|
*
|
|
140
|
|
|
* @param $macro
|
|
141
|
|
|
* @param $concrete
|
|
142
|
|
|
* @param $method
|
|
143
|
|
|
* @return mixed
|
|
144
|
|
|
*/
|
|
145
|
|
|
public function with($macro,$concrete,$method=null)
|
|
146
|
|
|
{
|
|
147
|
|
|
if($this->macro === null){
|
|
148
|
|
|
return $this($macro)->isMacro($concrete)->get($method,function() use($concrete){
|
|
149
|
|
|
return $concrete;
|
|
150
|
|
|
});
|
|
151
|
|
|
}
|
|
152
|
|
|
}
|
|
153
|
|
|
|
|
154
|
|
|
/**
|
|
155
|
|
|
* check been runnable with which static macro
|
|
156
|
|
|
*
|
|
157
|
|
|
* @param $macro
|
|
158
|
|
|
* @param $concrete
|
|
159
|
|
|
* @param $method
|
|
160
|
|
|
* @return mixed
|
|
161
|
|
|
*/
|
|
162
|
|
|
public function withStatic($macro,$concrete)
|
|
163
|
|
|
{
|
|
164
|
|
|
return $this($macro)->isMacro($concrete,true)->get(null,is_callable($concrete) ?
|
|
165
|
|
|
$concrete : function() use($concrete){
|
|
166
|
|
|
return $concrete;
|
|
167
|
|
|
});
|
|
168
|
|
|
}
|
|
169
|
|
|
|
|
170
|
|
|
/**
|
|
171
|
|
|
* invoke construct for macro
|
|
172
|
|
|
*
|
|
173
|
|
|
* @param null|string $macro
|
|
174
|
|
|
* @return $this
|
|
175
|
|
|
*/
|
|
176
|
|
|
public function __invoke($macro=null)
|
|
177
|
|
|
{
|
|
178
|
|
|
if($macro!==null){
|
|
179
|
|
|
$this->macro = $macro;
|
|
180
|
|
|
}
|
|
181
|
|
|
return $this;
|
|
182
|
|
|
}
|
|
183
|
|
|
} |