Skip to content

Commit 21d3b25

Browse files
committed
Кастомное событие AfterHandleRequestEvent
1 parent 37a6058 commit 21d3b25

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

Event/AfterHandleRequestEvent.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Prokl\WpSymfonyRouterBundle\Event;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Contracts\EventDispatcher\Event;
8+
9+
/**
10+
* Class AfterHandleRequestEvent
11+
* @package Prokl\WpSymfonyRouterBundle\Event
12+
*
13+
* @since 16.08.2021
14+
*/
15+
class AfterHandleRequestEvent extends Event
16+
{
17+
/**
18+
* @var Request $request
19+
*/
20+
private $request;
21+
22+
/**
23+
* @var Response $response
24+
*/
25+
private $response;
26+
27+
/**
28+
* @param Request $request Request.
29+
* @param Response $response Response.
30+
*/
31+
public function __construct(Request $request, Response $response)
32+
{
33+
$this->request = $request;
34+
$this->response = $response;
35+
}
36+
37+
/**
38+
* @return Request
39+
*/
40+
public function getRequest(): Request
41+
{
42+
return $this->request;
43+
}
44+
45+
/**
46+
* @return Response
47+
*/
48+
public function getResponse(): Response
49+
{
50+
return $this->response;
51+
}
52+
}

Event/KernelCustomEvents.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Prokl\WpSymfonyRouterBundle\Event;
4+
5+
/**
6+
* Interface KernelCustomEvents
7+
*
8+
* @package Prokl\WpSymfonyRouterBundle\Event
9+
* @since 16.08.2021
10+
*/
11+
interface KernelCustomEvents
12+
{
13+
/**
14+
* Кастомное событие, запускаемое после обработки запроса роутером.
15+
*
16+
* Происходит после обработки запроса роутером, но до события kernel.terminate.
17+
* Необходимо, чтобы можно было как-то сопрягать запросы Symfony и нативный контекст.
18+
*
19+
* @Event("Prokl\BitrixSymfonyRouterBundle\Event\AfterHandleRequestEvent")
20+
*/
21+
public const AFTER_HANDLE_REQUEST = 'kernel.after_handle_request';
22+
}

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020-2021 Fedor Gavrilov
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Services/Router/InitRouter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Prokl\WpSymfonyRouterBundle\Services\Router;
44

55
use Exception;
6+
use Prokl\WpSymfonyRouterBundle\Event\AfterHandleRequestEvent;
7+
use Prokl\WpSymfonyRouterBundle\Event\KernelCustomEvents;
68
use Prokl\WpSymfonyRouterBundle\Services\Interfaces\ErrorControllerInterface;
79
use Prokl\WpSymfonyRouterBundle\Services\Listeners\StringResponseListener;
810
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -182,6 +184,13 @@ public function router(): void
182184

183185
// Handle response
184186
$response = $framework->handle($this->request);
187+
188+
// Кастомное событие kernel.after_handle_request
189+
$this->dispatcher->dispatch(
190+
new AfterHandleRequestEvent($this->request, $response),
191+
KernelCustomEvents::AFTER_HANDLE_REQUEST
192+
);
193+
185194
// Инициирует событие kernel.terminate.
186195
try {
187196
$framework->terminate($this->request, $response);

0 commit comments

Comments
 (0)