Skip to content

Commit 6f91575

Browse files
committed
Инициализация сессии.
1 parent 580c233 commit 6f91575

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

configs/notifier.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
44

55
use Proklung\Notifier\Email\CustomEmailChannel;
6+
use Proklung\Notifier\Session\SessionInitializator;
67
use Symfony\Bridge\Monolog\Handler\NotifierHandler;
78
use Symfony\Component\EventDispatcher\EventDispatcher;
9+
use Symfony\Component\HttpFoundation\Request;
810
use Symfony\Component\HttpFoundation\RequestStack;
11+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
912
use Symfony\Component\Mailer\Mailer;
1013
use Symfony\Component\Notifier\Channel\BrowserChannel;
1114
use Symfony\Component\Notifier\Channel\ChannelPolicy;
@@ -52,9 +55,20 @@
5255
}
5356

5457
$container->services()
58+
->set('session_migrator', SessionInitializator::class)
59+
60+
->set('session_instance', SessionInterface::class)
61+
->public()
62+
->factory([service('session_migrator'), 'session'])
63+
5564
->set('request_stack', RequestStack::class)
65+
->call('push', [service('module_request')])
5666
->set('event_dispatcher', EventDispatcher::class)
5767

68+
->set('module_request', Request::class)
69+
->factory([Request::class, 'createFromGlobals'])
70+
->call('setSession', [service('session_instance')])
71+
5872
->set('notifier', Notifier::class)
5973
->args([tagged_locator('notifier.channel', 'channel'), service('notifier.channel_policy')->ignoreOnInvalid()])
6074
->public()
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Proklung\Notifier\Session;
4+
5+
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
6+
use Symfony\Component\HttpFoundation\Session\Session;
7+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
8+
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
9+
10+
/**
11+
* Class SessionInitializator
12+
* @package Proklung\Notifier\Session
13+
*/
14+
class SessionInitializator
15+
{
16+
/**
17+
* @var Session|null $session Сессии Symfony.
18+
*/
19+
private $session;
20+
21+
/**
22+
* Инициализация.
23+
*
24+
* @return void
25+
*/
26+
public function init(): void
27+
{
28+
$this->session = new Session(new PhpBridgeSessionStorage());
29+
// Если сессия не запущена, то запустить и мигрировать.
30+
if (!$this->session->isStarted()) {
31+
$this->session->start();
32+
$this->migrateSession();
33+
}
34+
}
35+
36+
/**
37+
* Объект Session.
38+
*
39+
* @return SessionInterface
40+
*
41+
* @since 28.10.2020 Change to interface.
42+
*/
43+
public function session(): SessionInterface
44+
{
45+
if ($this->session === null) {
46+
$this->init();
47+
}
48+
49+
return $this->session;
50+
}
51+
52+
/**
53+
* FlashBag.
54+
*
55+
* @return FlashBagInterface
56+
*/
57+
public function getFlashBag() : FlashBagInterface
58+
{
59+
if ($this->session === null) {
60+
$this->init();
61+
}
62+
63+
return $this->session->getFlashBag();
64+
}
65+
66+
/**
67+
* Флэш сообщения из сессии.
68+
*
69+
* @param string $message ID сообщения.
70+
*
71+
* @return array
72+
*/
73+
public function getFlashMessages(string $message): array
74+
{
75+
if ($this->session === null) {
76+
$this->init();
77+
}
78+
79+
return $this->session->getFlashBag()->get($message);
80+
}
81+
82+
/**
83+
* Установить - получить значение ключа.
84+
*
85+
* @param string $key Ключ.
86+
* @param mixed $value Значение.
87+
*
88+
* @return null|mixed
89+
*/
90+
public function value(string $key, $value = null)
91+
{
92+
if ($this->session === null) {
93+
$this->init();
94+
}
95+
96+
if ($value === null) {
97+
return $this->session->get($key);
98+
}
99+
100+
$this->session->set($key, $value);
101+
102+
return null;
103+
}
104+
105+
/**
106+
* Миграция $_SESSION в сессии Symfony.
107+
*
108+
* @return void
109+
*/
110+
public function migrateSession() : void
111+
{
112+
foreach ($_SESSION as $key => $item) {
113+
$this->value($key, $item);
114+
}
115+
}
116+
117+
/**
118+
* Csrf токен приложения.
119+
*
120+
* @return string
121+
*/
122+
public function csrfTokenApp() : string
123+
{
124+
if ($this->session === null) {
125+
$this->init();
126+
}
127+
128+
return (string)$this->session->get('csrf_token');
129+
}
130+
}

0 commit comments

Comments
 (0)