Skip to content

Commit

Permalink
revert running callbacks using static variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 24, 2021
1 parent 4164017 commit baa3bdc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
2 changes: 1 addition & 1 deletion demos/_unit-test/callback_url.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

$expectedWord = <<<'EOF'
v3 url: callback_url.php /
callback url: callback_url.php?test=callback&__atk_cbtarget=test /
callback url: callback_url.php?__atk_cb_test=callback&__atk_cbtarget=test /
v2 url: callback_url.php
EOF;

Expand Down
21 changes: 8 additions & 13 deletions src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
class Callback extends AbstractView
{
/** @const string */
public const URL_QUERY_TARGET = '__atk_cbtarget';
public const URL_QUERY_TRIGGER_PREFIX = '__atk_cb_';

/** @var array Store currently running callback arguments. */
protected static $runningCallbackArgs = [];
/** @const string */
public const URL_QUERY_TARGET = '__atk_cbtarget';

/** @var string Specify a custom GET trigger. */
protected $urlTrigger;
Expand All @@ -49,18 +49,15 @@ protected function init(): void
public function setUrlTrigger(string $trigger = null)
{
$this->urlTrigger = $trigger ?: $this->name;

$this->getOwner()->stickyGet(self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger);
}

public function getUrlTrigger(): string
{
return $this->urlTrigger;
}

public static function getRunningCallbackArgs(): array
{
return self::$runningCallbackArgs;
}

/**
* Executes user-specified action when call-back is triggered.
*
Expand All @@ -72,8 +69,6 @@ public static function getRunningCallbackArgs(): array
public function set($fx = null, $args = null)
{
if ($this->isTriggered() && $this->canTrigger()) {
self::$runningCallbackArgs[$this->urlTrigger] = $this->getTriggeredValue();

return $fx(...($args ?? []));
}
}
Expand All @@ -93,15 +88,15 @@ public function terminateJson(AbstractView $view): void
*/
public function isTriggered()
{
return isset($_GET[$this->urlTrigger]);
return isset($_GET[self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger]);
}

/**
* Return callback triggered value.
*/
public function getTriggeredValue(): string
{
return $_GET[$this->urlTrigger] ?? '';
return $_GET[self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger] ?? '';
}

/**
Expand Down Expand Up @@ -144,6 +139,6 @@ public function getUrl(string $value = 'callback'): string
*/
private function getUrlArguments(string $value = null): array
{
return array_merge(self::getRunningCallbackArgs(), [self::URL_QUERY_TARGET => $this->urlTrigger, $this->urlTrigger => $value ?? $this->getTriggeredValue()]);
return [self::URL_QUERY_TARGET => $this->urlTrigger, self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger => $value ?? $this->getTriggeredValue()];
}
}
2 changes: 1 addition & 1 deletion src/JsReload.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function jsRender(): string
$final = (new Jquery($this->view))
->atkReloadView(
[
'uri' => $this->view->jsUrl(array_merge(['__atk_reload' => $this->view->name], Callback::getRunningCallbackArgs())),
'uri' => $this->view->jsUrl(['__atk_reload' => $this->view->name]),
'uri_options' => !empty($this->args) ? $this->args : null,
'afterSuccess' => $this->afterSuccess ? $this->afterSuccess->jsRender() : null,
'apiConfig' => !empty($this->apiConfig) ? $this->apiConfig : null,
Expand Down
16 changes: 8 additions & 8 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testCallback(): void
$cb = \Atk4\Ui\Callback::addTo($this->app);

// simulate triggering
$_GET[$cb->name] = '1';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1';

$cb->set(function ($x) use (&$var) {
$var = $x;
Expand All @@ -79,7 +79,7 @@ public function testViewUrlCallback(): void
$cb = \Atk4\Ui\Callback::addTo($v1);

// simulate triggering
$_GET[$cb->name] = '1';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1';

// Test for non sticky
$cb->setUrlTrigger($cb->name);
Expand Down Expand Up @@ -118,7 +118,7 @@ public function testCallbackLater(): void
$cb = \Atk4\Ui\CallbackLater::addTo($this->app);

// simulate triggering
$_GET[$cb->name] = '1';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1';

$cb->set(function ($x) use (&$var) {
$var = $x;
Expand All @@ -139,8 +139,8 @@ public function testCallbackLaterNested(): void
$cb = \Atk4\Ui\CallbackLater::addTo($this->app);

// simulate triggering
$_GET[$cb->name] = '1';
$_GET[$cb->name . '_2'] = '1';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name] = '1';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $cb->name . '_2'] = '1';

$app = $this->app;
$cb->set(function ($x) use (&$var, $app, &$cbname) {
Expand Down Expand Up @@ -185,7 +185,7 @@ public function testVirtualPage(): void
$vp = \Atk4\Ui\VirtualPage::addTo($this->app);

// simulate triggering
$_GET[$vp->name] = '1';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $vp->name] = '1';

$vp->set(function ($p) use (&$var) {
$var = 25;
Expand All @@ -203,7 +203,7 @@ public function testVirtualPageCustomTrigger(): void
$vp = \Atk4\Ui\VirtualPage::addTo($this->app, ['urlTrigger' => 'bah']);

// simulate triggering
$_GET['bah'] = '1';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . 'bah'] = '1';

$vp->set(function ($p) use (&$var) {
$var = 25;
Expand All @@ -228,7 +228,7 @@ public function testPull230(): void
$vp = \Atk4\Ui\VirtualPage::addTo($this->app);

// simulate triggering
$_GET[$vp->name] = '1';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . $vp->name] = '1';

$vp->set(\Closure::fromCallable([$this, 'callPull230']));

Expand Down
20 changes: 10 additions & 10 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function testWizard(): void
}

$response = $this->getResponseFromRequest(
'interactive/wizard.php?demo_wizard=1&w_form_submit=ajax&' . Callback::URL_QUERY_TARGET . '=w_form_submit',
'interactive/wizard.php?demo_wizard=1&' . Callback::URL_QUERY_TRIGGER_PREFIX . 'w_form_submit=ajax&' . Callback::URL_QUERY_TARGET . '=w_form_submit',
['form_params' => [
'dsn' => 'mysql://root:[email protected]/atk4',
]]
Expand All @@ -351,10 +351,10 @@ public function jsonResponseProvider(): array
// simple reload
$files[] = ['_unit-test/reload.php?__atk_reload=reload'];
// loader callback reload
$files[] = ['_unit-test/reload.php?c_reload=ajax&' . Callback::URL_QUERY_TARGET . '=c_reload'];
$files[] = ['_unit-test/reload.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'c_reload=ajax&' . Callback::URL_QUERY_TARGET . '=c_reload'];
// test catch exceptions
$files[] = ['_unit-test/exception.php?m_cb=ajax&' . Callback::URL_QUERY_TARGET . '=m_cb&__atk_json=1'];
$files[] = ['_unit-test/exception.php?m2_cb=ajax&' . Callback::URL_QUERY_TARGET . '=m2_cb&__atk_json=1'];
$files[] = ['_unit-test/exception.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'm_cb=ajax&' . Callback::URL_QUERY_TARGET . '=m_cb&__atk_json=1'];
$files[] = ['_unit-test/exception.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'm2_cb=ajax&' . Callback::URL_QUERY_TARGET . '=m2_cb&__atk_json=1'];

return $files;
}
Expand Down Expand Up @@ -384,11 +384,11 @@ public function testDemoAssertJsonResponse(string $uri): void
public function sseResponseProvider(): array
{
$files = [];
$files[] = ['_unit-test/sse.php?see_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
$files[] = ['_unit-test/console.php?console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
$files[] = ['_unit-test/sse.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'see_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
$files[] = ['_unit-test/console.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
if (!($this instanceof DemosHttpNoExitTest)) { // ignore content type mismatch when App->call_exit equals to true
$files[] = ['_unit-test/console_run.php?console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
$files[] = ['_unit-test/console_exec.php?console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
$files[] = ['_unit-test/console_run.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
$files[] = ['_unit-test/console_exec.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
}

return $files;
Expand Down Expand Up @@ -434,15 +434,15 @@ public function jsonResponsePostProvider(): array
{
$files = [];
$files[] = [
'_unit-test/post.php?test_submit=ajax&' . Callback::URL_QUERY_TARGET . '=test_submit',
'_unit-test/post.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'test_submit=ajax&' . Callback::URL_QUERY_TARGET . '=test_submit',
[
'f1' => 'v1',
],
];

// for JsNotify coverage
$files[] = [
'obsolete/notify2.php?test_notify=ajax&' . Callback::URL_QUERY_TARGET . '=test_notify',
'obsolete/notify2.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'test_notify=ajax&' . Callback::URL_QUERY_TARGET . '=test_notify',
[
'text' => 'This text will appear in notification',
'icon' => 'warning sign',
Expand Down
2 changes: 1 addition & 1 deletion tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function assertSubmit(array $post_data, \Closure $submit = null, \Closure
$submit_called = false;
$_POST = $post_data;
// trigger callback
$_GET['atk_submit'] = 'ajax';
$_GET[Callback::URL_QUERY_TRIGGER_PREFIX . 'atk_submit'] = 'ajax';
$_GET[Callback::URL_QUERY_TARGET] = 'atk_submit';

$this->f->onSubmit(function (Form $form) use (&$submit_called, $submit) {
Expand Down

0 comments on commit baa3bdc

Please sign in to comment.