Skip to content

Commit

Permalink
Pass custom widget namespace to ajax calls
Browse files Browse the repository at this point in the history
  • Loading branch information
arrilot committed Sep 20, 2020
1 parent 36b1e49 commit ae0e44c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"AsyncWidget": "Arrilot\\Widgets\\AsyncFacade"
}
}
},
"scripts": {
"test": "phpunit"
}
}
5 changes: 4 additions & 1 deletion src/Controllers/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ public function showWidget(Request $request)
: $factory->decryptWidgetParams($request->input('params', ''));

$decodedParams = json_decode($widgetParams, true);

$params = $decodedParams ?: [];
array_unshift($params, $widgetName);

return call_user_func_array([$factory, $widgetName], $decodedParams ?: []);
return call_user_func_array([$factory, 'run'], $params);
}

/**
Expand Down
24 changes: 23 additions & 1 deletion src/Factories/AbstractWidgetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ abstract class AbstractWidgetFactory
*/
public $widgetName;

/**
* Custom widget namespace of the widget being called if is set.
*
* @var string|null
*/
public $customWidgetNamespace;

/**
* Array of widget parameters excluding the first one (config).
*
Expand Down Expand Up @@ -120,9 +127,12 @@ protected function instantiateWidget(array $params = [])

if (preg_match('#^(.*?)::(.*?)$#', $str, $m)) {
$rootNamespace = $this->app->get('arrilot.widget-namespaces')->getNamespace($m[1]);
$this->customWidgetNamespace = $m[1];
$str = $m[2];
} else {
$this->customWidgetNamespace = null;
}

$this->widgetName = $this->parseFullWidgetNameFromString($str);
$this->widgetFullParams = $params;
$this->widgetConfig = (array) array_shift($params);
Expand Down Expand Up @@ -206,4 +216,16 @@ public function decryptWidgetParams($params)
{
return $this->app->make('encrypter')->decrypt($params);
}

/**
* Get current widget name with optional custom widget namespace.
*
* @return string
*/
public function getWidgetNameWithCustomNamespace()
{
return $this->customWidgetNamespace
? $this->customWidgetNamespace . '::' . $this->widgetName
: $this->widgetName;
}
}
2 changes: 1 addition & 1 deletion src/Factories/JavascriptFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function constructAjaxCall($encryptParams = true)
$encodedParams = json_encode($this->widgetFactory->widgetFullParams);
$queryParams = [
'id' => WidgetId::get(),
'name' => $this->widgetFactory->widgetName,
'name' => $this->widgetFactory->getWidgetNameWithCustomNamespace(),
'params' => $encryptParams ? $this->widgetFactory->encryptWidgetParams($encodedParams) : $encodedParams,
];
if (!$encryptParams) {
Expand Down

0 comments on commit ae0e44c

Please sign in to comment.