Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the grid to work without having an active request #16

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions Grid/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Grid implements GridInterface
protected $router;

/**
* @var \Symfony\Component\HttpFoundation\Session\Session;
* @var null|\Symfony\Component\HttpFoundation\Session\Session;
*/
protected $session;

Expand Down Expand Up @@ -334,9 +334,15 @@ public function __construct($container, $id = '', GridConfigInterface $config =
$this->container = $container;
$this->config = $config;

$this->router = $container->get('router');
$this->request = $container->get('request_stack')->getCurrentRequest();
$this->session = $this->request->getSession();
$this->router = $container->get('router');
$this->request = $container->get('request_stack')->getCurrentRequest();

if (null === $this->request) {
$this->request = Request::createFromGlobals();
} else {
$this->session = $this->request->getSession();
}

$this->securityContext = $container->get('security.authorization_checker');

$this->id = $id;
Expand Down Expand Up @@ -454,7 +460,9 @@ public function handleRequest(Request $request)

$this->processPersistence();

$this->sessionData = (array)$this->session->get($this->hash);
if (null !== $this->session) {
$this->sessionData = (array)$this->session->get($this->hash);
}

$this->processLazyParameters();

Expand Down Expand Up @@ -524,7 +532,9 @@ public function isReadyForRedirect()

$this->processPersistence();

$this->sessionData = (array)$this->session->get($this->hash);
if (null !== $this->session) {
$this->sessionData = (array)$this->session->get($this->hash);
}

$this->processLazyParameters();

Expand Down Expand Up @@ -565,10 +575,12 @@ protected function processPersistence()
// Persistence or reset - kill previous session
if ((!$this->request->isXmlHttpRequest() && !$this->persistence && $referer != $this->getCurrentUri())
|| isset($this->requestData[self::REQUEST_QUERY_RESET])) {
$this->session->remove($this->hash);
if (null !== $this->session) {
$this->session->remove($this->hash);
}
}

if ($this->session->get($this->hash) === null) {
if (null !== $this->session && $this->session->get($this->hash) === null) {
$this->newSession = true;
}
}
Expand Down Expand Up @@ -656,7 +668,7 @@ protected function processMassActions($actionId)
}
}

if (is_callable($action->getCallback())) {
if (is_callable($action->getCallback()) && null !== $this->session) {
$this->massActionResponse = call_user_func($action->getCallback(), $actionKeys, $actionAllKeys, $this->session, $action->getParameters());
} elseif (strpos($action->getCallback(), ':') !== false) {
$path = array_merge(
Expand Down Expand Up @@ -735,7 +747,10 @@ protected function processTweaks($tweakId)

if (isset($tweak['reset'])) {
$this->sessionData = [];
$this->session->remove($this->hash);

if (null !== $this->session) {
$this->session->remove($this->hash);
}
}

if (isset($tweak['filters'])) {
Expand Down Expand Up @@ -1144,7 +1159,7 @@ protected function set($key, $data)

protected function saveSession()
{
if (!empty($this->sessionData) && !empty($this->hash)) {
if (!empty($this->sessionData) && !empty($this->hash) && null !== $this->session) {
$this->session->set($this->hash, $this->sessionData);
}
}
Expand Down