Skip to content

Commit

Permalink
Merge pull request #16 from northernco/make-request-optional
Browse files Browse the repository at this point in the history
Update the grid to work without having an active request
  • Loading branch information
cpkdevries authored Oct 28, 2022
2 parents 4eff124 + f458471 commit 9881189
Showing 1 changed file with 26 additions and 11 deletions.
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

0 comments on commit 9881189

Please sign in to comment.