Skip to content

Commit

Permalink
Merge pull request #44 from tuxes3/fix/symfony6-session
Browse files Browse the repository at this point in the history
inject request stack for symfony 6
  • Loading branch information
jorisdugue authored Aug 23, 2023
2 parents 4039308 + 122acc9 commit ed8cea1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
28 changes: 20 additions & 8 deletions Core/H5PSymfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use GuzzleHttp\Client;
use H5PPermission;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Router;
Expand Down Expand Up @@ -84,7 +86,8 @@ public function __construct(H5POptions $options,
EditorStorage $editorStorage,
TokenStorageInterface $tokenStorage,
EntityManagerInterface $manager,
Session $session,
?Session $session,
?RequestStack $requestStack,
AuthorizationCheckerInterface $authorizationChecker,
EventDispatcherInterface $eventDispatcher,
RouterInterface $router)
Expand All @@ -93,10 +96,16 @@ public function __construct(H5POptions $options,
$this->editorStorage = $editorStorage;
$this->tokenStorage = $tokenStorage;
$this->manager = $manager;
$this->session = $session;
$this->authorizationChecker = $authorizationChecker;
$this->eventDispatcher = $eventDispatcher;
$this->router = $router;
try {
$this->session = !$requestStack ? null : $requestStack->getSession();
} catch (SessionNotFoundException $e) {
}
if (!$this->session) {
$this->session = $session;
}
}

/**
Expand Down Expand Up @@ -195,7 +204,9 @@ public function setLibraryTutorialUrl($machineName, $tutorialUrl)
*/
public function setErrorMessage($message, $code = NULL)
{
$this->session->getFlashBag()->add("error", "[$code]: $message");
if ($this->session) {
$this->session->getFlashBag()->add("error", "[$code]: $message");
}
}

/**
Expand All @@ -204,7 +215,9 @@ public function setErrorMessage($message, $code = NULL)
*/
public function setInfoMessage($message)
{
$this->session->getFlashBag()->add("info", "$message");
if ($this->session) {
$this->session->getFlashBag()->add("info", "$message");
}
}

/**
Expand All @@ -214,11 +227,10 @@ public function setInfoMessage($message)
*/
public function getMessages($type)
{
if (!$this->session->getFlashBag()->has($type)) {
return NULL;
if (!$this->session || !$this->session->getFlashBag()->has($type)) {
return null;
}
$messages = $this->session->getFlashBag()->get($type);
return $messages;
return $this->session->getFlashBag()->get($type);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:

studit_h5p.interface:
class: Studit\H5PBundle\Core\H5PSymfony
arguments: ['@studit_h5p.options', '@studit_h5p.editor_storage','@security.token_storage', '@doctrine.orm.entity_manager', '@session', '@security.authorization_checker', '@event_dispatcher', '@router.default']
arguments: ['@studit_h5p.options', '@studit_h5p.editor_storage','@security.token_storage', '@doctrine.orm.entity_manager', '@?session', '@?request_stack', '@security.authorization_checker', '@event_dispatcher', '@router.default']
Studit\H5PBundle\Core\H5PSymfony: '@studit_h5p.interface'

studit_h5p.filestorage:
Expand Down Expand Up @@ -91,12 +91,15 @@ services:

Studit\H5PBundle\Controller\H5PController:
autowire: true
autoconfigure: true
tags: [ 'controller.service_arguments']
Studit\H5PBundle\Controller\H5PInteractionController:
autowire: true
autoconfigure: true
tags: [ 'controller.service_arguments']
Studit\H5PBundle\Controller\H5PAJAXController:
autowire: true
autoconfigure: true
tags: [ 'controller.service_arguments']

Studit\H5PBundle\Entity\EventRepository:
Expand Down

0 comments on commit ed8cea1

Please sign in to comment.