Skip to content

Commit

Permalink
Merge pull request #21 from martinlutter/symfony6
Browse files Browse the repository at this point in the history
update to support symfony 6
  • Loading branch information
martinlutter authored Nov 15, 2022
2 parents 7fb182a + 027ce67 commit d9b05d1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
}
},
"require": {
"php": "^7.1|^8.0",
"php": "^8.0",
"everlutionsk/ajaxcom-php": "^1.1",
"symfony/framework-bundle": "^3.4.26|^4.1.12|^5.0",
"symfony/http-foundation": "^3.4.26|^4.1.12|^5.0",
"symfony/routing": "^3.4.26|^4.1.12|^5.0",
"symfony/config": "^3.4.26|^4.1.12|^5.0",
"symfony/dependency-injection": "^3.4.26|^4.1.12|^5.0",
"symfony/http-kernel": "^3.4.26|^4.1.12|^5.0",
"symfony/twig-bundle": "^3.4.26|^4.1.12|^5.0"
"symfony/framework-bundle": ">=6.0",
"symfony/http-foundation": ">=6.0",
"symfony/routing": ">=6.0",
"symfony/config": ">=6.0",
"symfony/dependency-injection": ">=6.0",
"symfony/http-kernel": ">=6.0",
"symfony/twig-bundle": ">=6.0"
},
"license": "MIT",
"authors": [
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/AjaxcomSymfony4Trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function setAjaxcomRequiredServices(

protected function render(string $view, array $parameters = array(), Response $response = null): Response
{
$request = $this->requestStack->getMasterRequest();
$request = $this->requestStack->getMainRequest();

if ($this->isAjax()) {
return $this->ajaxcom->handle($view, $parameters);
Expand Down Expand Up @@ -156,7 +156,7 @@ protected function prependAjaxBlock(string $id): self
protected function isAjax(): bool
{
/** @var Request $request */
$request = $this->requestStack->getMasterRequest();
$request = $this->requestStack->getMainRequest();
$isAjax = $request->server->getBoolean(Ajaxcom::AJAX_COM_HEADER, false);

return $isAjax;
Expand Down
17 changes: 9 additions & 8 deletions src/Mutation/Callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Everlution\Ajaxcom\Handler;
use Everlution\AjaxcomBundle\DataObject\Callback as AjaxCallback;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;

/**
Expand All @@ -17,34 +18,34 @@ class Callbacks implements MutatorInterface
{
const SESSION_KEY = 'ajaxcom/callbacks';

/** @var Session */
private $session;
/** @var RequestStack */
private $requestStack;

public function __construct(Session $session)
public function __construct(RequestStack $requestStack)
{
$this->session = $session;
$this->requestStack = $requestStack;
}

public function mutate(Handler $ajax): Handler
{
/** @var AjaxCallback[] $callbacks */
$callbacks = $this->session->get(self::SESSION_KEY, []);
$callbacks = $this->requestStack->getSession()->get(self::SESSION_KEY, []);
uasort($callbacks, [$this, 'sortByPriority']);

foreach ($callbacks as $callback) {
$ajax->callback($callback->getFunction(), $callback->getParameters());
}

$this->session->remove(self::SESSION_KEY);
$this->requestStack->getSession()->remove(self::SESSION_KEY);

return $ajax;
}

public function add(AjaxCallback $callback): self
{
$callbacks = $this->session->get(self::SESSION_KEY, []);
$callbacks = $this->requestStack->getSession()->get(self::SESSION_KEY, []);
$callbacks[] = $callback;
$this->session->set(self::SESSION_KEY, $callbacks);
$this->requestStack->getSession()->set(self::SESSION_KEY, $callbacks);

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mutation/ChangeUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ChangeUrl implements MutatorInterface
public function __construct(RequestStack $requestStack, UrlGeneratorInterface $router)
{
$this->router = $router;
$this->request = $requestStack->getMasterRequest();
$this->request = $requestStack->getMainRequest();
}

public function mutate(Handler $ajax): Handler
Expand Down
18 changes: 12 additions & 6 deletions src/Mutation/FlashMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
use Everlution\AjaxcomBundle\AjaxcomException;
use Everlution\AjaxcomBundle\DataObject\Block;
use Everlution\AjaxcomBundle\Service\RenderBlock;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;

/**
* Class FlashMessages.
Expand All @@ -19,32 +20,37 @@ class FlashMessages implements MutatorInterface
{
/** @var RenderBlock */
private $renderBlock;
/** @var FlashBagInterface */
private $flashBag;
/** @var RequestStack */
private $requestStack;
/** @var string */
private $flashesTemplate;
/** @var string */
private $flashesBlockId;

public function __construct(
RenderBlock $renderBlock,
FlashBagInterface $flashBag,
RequestStack $requestStack,
string $flashesTemplate,
string $flashesBlockId
) {
$this->renderBlock = $renderBlock;
$this->flashBag = $flashBag;
$this->requestStack = $requestStack;
$this->flashesTemplate = $flashesTemplate;
$this->flashesBlockId = $flashesBlockId;
}

public function mutate(Handler $ajax): Handler
{
$session = $this->requestStack->getSession();
if (!$session instanceof Session) {
throw new AjaxcomException('Bad session instance');
}

try {
$messages = $this->renderBlock->render(
(new Block($this->flashesBlockId))->refresh(),
$this->flashesTemplate,
['flashes' => $this->flashBag->all()]
['flashes' => $session->/** @scrutinizer ignore-call */getFlashBag()->all()]
);
$ajax->container("#$this->flashesBlockId")->html($messages);
} catch (AjaxcomException $exception) {
Expand Down
10 changes: 5 additions & 5 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
public: true
arguments:
- '@everlution.ajaxcom'
- '@ajaxcom.mutation.containe'
- '@ajaxcom.mutation.container'

everlution.ajaxcom:
class: Everlution\Ajaxcom\Handler
Expand All @@ -17,7 +17,7 @@ services:
- '@twig'

# mutations
ajaxcom.mutation.containe:
ajaxcom.mutation.container:
class: Everlution\AjaxcomBundle\Mutation\Container
# the order of following call is important
calls:
Expand Down Expand Up @@ -65,7 +65,7 @@ services:
class: Everlution\AjaxcomBundle\Mutation\Callbacks
public: true
arguments:
- '@session'
- '@request_stack'

ajaxcom.mutation.replace_class:
class: Everlution\AjaxcomBundle\Mutation\ReplaceClass
Expand All @@ -85,7 +85,7 @@ services:
public: false
arguments:
- '@ajaxcom.render_block'
- '@session.flash_bag'
- '@request_stack'
- '%everlution.ajaxcom.flash_template%'
- '%everlution.ajaxcom.flash_block_id%'

Expand Down Expand Up @@ -120,7 +120,7 @@ services:
Everlution\AjaxcomBundle\Service\Ajaxcom: '@ajaxcom.handler'
Everlution\Ajaxcom\Handler: '@everlution.ajaxcom'
Everlution\AjaxcomBundle\Service\RenderBlock: '@ajaxcom.render_block'
Everlution\AjaxcomBundle\Mutation\Container: '@ajaxcom.mutation.containe'
Everlution\AjaxcomBundle\Mutation\Container: '@ajaxcom.mutation.container'
Everlution\AjaxcomBundle\Mutation\AppendBlocks: '@ajaxcom.mutation.append_blocks'
Everlution\AjaxcomBundle\Mutation\PrependBlocks: '@ajaxcom.mutation.prepend_blocks'
Everlution\AjaxcomBundle\Mutation\AddBlocks: '@ajaxcom.mutation.add_blocks'
Expand Down

0 comments on commit d9b05d1

Please sign in to comment.