-
Notifications
You must be signed in to change notification settings - Fork 104
/
Callback.php
196 lines (169 loc) · 4.49 KB
/
Callback.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace atk4\ui;
use atk4\core\AppScopeTrait;
use atk4\core\DiContainerTrait;
use atk4\core\InitializerTrait;
use atk4\core\StaticAddToTrait;
use atk4\core\TrackableTrait;
/**
* Add this object to your render tree and it will expose a unique URL which, when
* executed directly will perform a PHP callback that you set().
*
* $button = Button::addTo($layout);
* $button->set('Click to do something')->link(
* Callback::addTo($button)
* ->set(function(){
* do_something();
* })
* ->getUrl()
* );
*
* @property View $owner
*/
class Callback
{
use TrackableTrait;
use AppScopeTrait;
use DiContainerTrait;
use InitializerTrait {
init as _init;
}
use StaticAddToTrait;
/**
* Whether urlTrigger is set via Post or Get (default).
*
* @var bool
*/
public $isPostTriggered;
/**
* Specify a custom GET or POST trigger here.
*
* @var string|null
*/
private $urlTrigger;
/**
* Initialize object and set default properties.
*
* @param array|string $defaults
*/
public function __construct($defaults = [])
{
$this->setDefaults($defaults);
}
/**
* Initialization.
*/
public function init(): void
{
$this->_init();
if (!$this->app) {
throw new Exception('Call-back must be part of a RenderTree');
}
if (!$this->urlTrigger) {
$this->urlTrigger = $this->name;
}
$this->setAppSticky();
}
public function setUrlTrigger(string $trigger)
{
$this->urlTrigger = $trigger;
$this->setAppSticky();
}
/**
* Set app sticky argument only when using GET method.
*/
public function setAppSticky()
{
if (!$this->isPostTriggered) {
$this->app->stickyGet($this->urlTrigger);
}
}
public function getUrlTrigger(): string
{
return $this->urlTrigger;
}
/**
* Executes user-specified action when call-back is triggered.
*
* @param callable $callback
* @param array $args
*
* @return mixed|null
*/
public function set($callback, $args = [])
{
if ($this->isTriggered()) {
$this->app->catch_runaway_callbacks = false;
$t = $this->app->run_called;
$this->app->run_called = true;
$ret = call_user_func_array($callback, $args);
$this->app->run_called = $t;
return $ret;
}
}
/**
* Terminate this callback
* by rendering the owner view by default.
*/
public function terminateJson(View $view = null)
{
if ($this->canTerminate()) {
$this->app->terminateJson($view ?: $this->owner);
}
}
/**
* Prevent callback from terminating during a reload.
*/
protected function canTerminate(): bool
{
$reload = $_GET['__atk_reload'] ?? null;
return !$reload || $this->owner->name === $reload;
}
public function isTriggered(): bool
{
if ($this->isPostTriggered) {
return isset($_POST[$this->urlTrigger]);
}
return isset($_GET[$this->urlTrigger]);
}
/**
* Return callback triggered value.
*/
public function getTriggeredValue(): string
{
if ($this->isPostTriggered) {
return $_POST[$this->urlTrigger] ?? '';
}
return $_GET[$this->urlTrigger] ?? '';
}
/**
* Return URL that will trigger action on this call-back. If you intend to request
* the URL direcly in your browser (as iframe, new tab, or document location), you
* should use getUrl instead.
*/
public function getJsUrl(string $value = 'ajax'): string
{
return $this->owner->jsUrl($this->getUrlArguments($value));
}
/**
* Return URL that will trigger action on this call-back. If you intend to request
* the URL loading from inside JavaScript, it's always advised to use getJsUrl instead.
*
*/
public function getUrl(string $value = 'callback'): string
{
return $this->owner->url($this->getUrlArguments($value));
}
/**
* Return proper url argument for this callback.
*/
private function getUrlArguments(string $value): array
{
$args = ['__atk_callback' => 1];
if (!$this->isPostTriggered) {
$args[$this->urlTrigger] = $value;
}
return $args;
}
}