8000 [MonologBridge] Added hashing with spl_object_id in RouteProcessor, f… · symfony/symfony@228511e · GitHub
[go: up one dir, main page]

Skip to content

Commit 228511e

Browse files
committed
[MonologBridge] Added hashing with spl_object_id in RouteProcessor, fixed CS
1 parent 6d6c5ce commit 228511e

File tree

4 files changed

+80
-63
lines changed

4 files changed

+80
-63
lines changed

src/Symfony/Bridge/Monolog/Processor/ConsoleCommandProcessor.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
use Symfony\Component\Console\ConsoleEvents;
1515
use Symfony\Component\Console\Event\ConsoleEvent;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
use Symfony\Contracts\Service\ResetInterface;
1718

1819
/**
1920
* Adds the current console command information to the log entry.
2021
*
2122
* @author Piotr Stankowski <git@trakos.pl>
2223
*/
23-
class ConsoleCommandProcessor implements ProcessorInterface, EventSubscriberInterface
24+
class ConsoleCommandProcessor implements ProcessorInterface, EventSubscriberInterface, ResetInterface
2425
{
2526
private $commandData;
2627
private $includeArguments;
@@ -32,13 +33,6 @@ public function __construct(bool $includeArguments = true, bool $includeOptions
3233
$this->includeOptions = $includeOptions 8000 ;
3334
}
3435

35-
public static function getSubscribedEvents()
36-
{
37-
return array(
38-
ConsoleEvents::COMMAND => array('onCommand', 1),
39-
);
40-
}
41-
4236
public function __invoke(array $records)
4337
{
4438
if (null !== $this->commandData && !isset($records['extra']['command'])) {
@@ -48,7 +42,12 @@ public function __invoke(array $records)
4842
return $records;
4943
}
5044

51-
public function onCommand(ConsoleEvent $event)
45+
public function reset()
46+
{
47+
$this->commandData = null;
48+
}
49+
50+
public function addCommandData(ConsoleEvent $event)
5251
{
5352
$this->commandData = array(
5453
'name' => $event->getCommand()->getName(),
@@ -60,4 +59,11 @@ public function onCommand(ConsoleEvent $event)
6059
$this->commandData['options'] = $event->getInput()->getOptions();
6160
}
6261
}
62+
63+
public static function getSubscribedEvents()
64+
{
65+
return array(
66+
ConsoleEvents::COMMAND => array('addCommandData', 1),
67+
);
68+
}
6369
}

src/Symfony/Bridge/Monolog/Processor/RouteProcessor.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,42 @@
1515
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1616
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1717
use Symfony\Component\HttpKernel\KernelEvents;
18+
use Symfony\Contracts\Service\ResetInterface;
1819

1920
/**
2021
* Adds the current route information to the log entry.
2122
*
2223
* @author Piotr Stankowski <git@trakos.pl>
2324
*/
24-
class RouteProcessor implements ProcessorInterface, EventSubscriberInterface
25+
class RouteProcessor implements ProcessorInterface, EventSubscriberInterface, ResetInterface
2526
{
26-
private $routeData = array();
27+
private $routeData;
2728
private $includeParams;
2829

2930
public function __construct(bool $includeParams = true)
3031
{
3132
$this->includeParams = $includeParams;
33+
$this->reset();
3234
}
3335

3436
public function __invoke(array $records)
3537
{
36-
if (!empty($this->routeData) && !isset($records['extra']['requests'])) {
37-
$records['extra']['requests'] = $this->routeData;
38+
if ($this->routeData && !isset($records['extra']['requests'])) {
39+
$records['extra']['requests'] = array_values($this->routeData);
3840
}
3941

4042
return $records;
4143
}
4244

43-
public function onKernelRequest(GetResponseEvent $event)
45+
public function reset()
46+
{
47+
$this->routeData = array();
48+
}
49+
50+
public function addRouteData(GetResponseEvent $event)
4451
{
4552
if ($event->isMasterRequest()) {
46-
$this->routeData = array();
53+
$this->reset();
4754
}
4855

4956
$request = $event->getRequest();
@@ -60,23 +67,20 @@ public function onKernelRequest(GetResponseEvent $event)
6067
$currentRequestData['route_params'] = $request->attributes->get('_route_params');
6168
}
6269

63-
$this->routeData[] = $currentRequestData;
70+
$this->routeData[spl_object_id($request)] = $currentRequestData;
6471
}
6572

66-
public function onKernelFinishRequest(FinishRequestEvent $event)
73+
public function removeRouteData(FinishRequestEvent $event)
6774
{
68-
if (!$event->getRequest()->attributes->has('_controller')) {
69-
return;
70-
}
71-
72-
array_pop($this->routeData);
75+
$requestId = spl_object_id($event->getRequest());
76+
unset($this->routeData[$requestId]);
7377
}
7478

7579
public static function getSubscribedEvents()
7680
{
7781
return array(
78-
KernelEvents::REQUEST => array('onKernelRequest', 1),
79-
KernelEvents::FINISH_REQUEST => array('onKernelFinishRequest', 1),
82+
KernelEvents::REQUEST => array('addRouteData', 1),
83+
KernelEvents::FINISH_REQUEST => array('removeRouteData', 1),
8084
);
8185
}
8286
}

src/Symfony/Bridge/Monolog/Tests/Processor/ConsoleCommandProcessorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919

2020
class ConsoleCommandProcessorTest extends TestCase
2121
{
22-
const TEST_ARGUMENTS = array('test' => 'argument');
23-
const TEST_OPTIONS = array('test' => 'option');
24-
const TEST_NAME = 'some:test';
22+
private const TEST_ARGUMENTS = array('test' => 'argument');
23< F987 /td>+
private const TEST_OPTIONS = array('test' => 'option');
24+
private const TEST_NAME = 'some:test';
2525

2626
public function testProcessor()
2727
{
2828
$processor = new ConsoleCommandProcessor();
29-
$processor->onCommand($this->getConsoleEvent());
29+
$processor->addCommandData($this->getConsoleEvent());
3030

3131
$record = $processor(array('extra' => array()));
3232

@@ -40,7 +40,7 @@ public function testProcessor()
4040
public function testProcessorWithOptions()
4141
{
4242
$processor = new ConsoleCommandProcessor(true, true);
43-
$processor->onCommand($this->getConsoleEvent());
43+
$processor->addCommandData($this->getConsoleEvent());
4444

4545
$record = $processor(array('extra' => array()));
4646

src/Symfony/Bridge/Monolog/Tests/Processor/RouteProcessorTest.php

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Monolog\Processor\RouteProcessor;
1616
use Symfony\Component\HttpFoundation\ParameterBag;
17+
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1819
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1920

2021
class RouteProcessorTest extends TestCase
2122
{
22-
const TEST_CONTROLLER = 'App\Controller\SomeController::someMethod';
23-
const TEST_ROUTE = 'someRouteName';
24-
const TEST_PARAMS = array('param1' => 'value1');
23+
private const TEST_CONTROLLER = 'App\Controller\SomeController::someMethod';
24+
private const TEST_ROUTE = 'someRouteName';
25+
private const TEST_PARAMS = array('param1' => 'value1');
2526

2627
public function testProcessor()
2728
{
29+
$request = $this->mockFilledRequest();
2830
$processor = new RouteProcessor();
29-
$processor->onKernelRequest($this->getFilledRequestEvent());
31+
$processor->addRouteData($this->mockGetResponseEvent($request));
3032

3133
$record = $processor(array('extra' => array()));
3234

@@ -40,8 +42,9 @@ public function testProcessor()
4042

4143
public function testProcessorWithoutParams()
4244
{
45+
$request = $this->mockFilledRequest();
4346
$processor = new RouteProcessor(false);
44-
$processor->onKernelRequest($this->getFilledRequestEvent());
47+
$processor->addRouteData($this->mockGetResponseEvent($request));
4548

4649
$record = $processor(array('extra' => array()));
4750

@@ -56,10 +59,12 @@ public function testProcessorWithoutParams()
5659
public function testProcessorWithSubRequests()
5760
{
5861
$controllerFromSubRequest = 'OtherController::otherMethod';
62+
$mainRequest = $this->mockFilledRequest();
63+
$subRequest = $this->mockFilledRequest($controllerFromSubRequest);
5964

6065
$processor = new RouteProcessor(false);
61-
$processor->onKernelRequest($this->getFilledRequestEvent());
62-
$processor->onKernelRequest($this->getFilledRequestEvent($controllerFromSubRequest));
66+
$processor->addRouteData($this->mockGetResponseEvent($mainRequest));
67+
$processor->addRouteData($this->mockGetResponseEvent($subRequest));
6368

6469
$ 10000 record = $processor(array('extra' => array()));
6570

@@ -75,14 +80,15 @@ public function testProcessorWithSubRequests()
7580
);
7681
}
7782

78-
public function testFinishRequestRemovesLastEntry()
83+
public function testFinishRequestRemovesRelatedEntry()
7984
{
80-
$controllerFromSubRequest = 'OtherController::otherMethod';
85+
$mainRequest = $this->mockFilledRequest();
86+
$subRequest = $this->mockFilledRequest('OtherController::otherMethod');
8187

8288
$processor = new RouteProcessor(false);
83-
$processor->onKernelRequest($this->getFilledRequestEvent());
84-
$processor->onKernelRequest($this->getFilledRequestEvent($controllerFromSubRequest));
85-
$processor->onKernelFinishRequest($this->getFinishRequestEvent());
89+
$processor->addRouteData($this->mockGetResponseEvent($mainRequest));
90+
$processor->addRouteData($this->mockGetResponseEvent($subRequest));
91+
$processor->removeRouteData($this->mockFinishRequestEvent($subRequest));
8692
$record = $processor(array('extra' => array()));
8793

8894
$this->assertArrayHasKey('requests', $record['extra']);
@@ -92,16 +98,17 @@ public function testFinishRequestRemovesLastEntry()
9298
$record['extra']['requests'][0]
9399
);
94100

95-
$processor->onKernelFinishRequest($this->getFinishRequestEvent());
101+
$processor->removeRouteData($this->mockFinishRequestEvent($mainRequest));
96102
$record = $processor(array('extra' => array()));
97103

98104
$this->assertArrayNotHasKey('requests', $record['extra']);
99105
}
100106

101107
public function testProcessorWithEmptyRequest()
102108
{
109+
$request = $this->mockEmptyRequest();
103110
$processor = new RouteProcessor();
104-
$processor->onKernelRequest($this->getEmptyRequestEvent());
111+
$processor->addRouteData($this->mockGetResponseEvent($request));
105112

106113
$record = $processor(array('extra' => array()));
107114
$this->assertEquals(array('extra' => array()), $record);
@@ -115,41 +122,41 @@ public function testProcessorDoesNothingWhenNoRequest()
115122
$this->assertEquals(array('extra' => array()), $record);
116123
}
117124

118-
private function getFinishRequestEvent(array $attributes = array('_controller' => 'test::test')): FinishRequestEvent
125+
private function mockGetResponseEvent(Request $request): GetResponseEvent
119126
{
120-
$request = new \stdClass();
121-
$request->attributes = new ParameterBag($attributes);
122-
123-
$event = $this->getMockBuilder(FinishRequestEvent::class)->disableOriginalConstructor()->getMock();
127+
$event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock();
124128
$event->method('getRequest')->willReturn($request);
125129

126130
return $event;
127131
}
128132

129-
private function getRequestEvent(array $attributes): GetResponseEvent
133+
private function mockFinishRequestEvent(Request $request): FinishRequestEvent
130134
{
131-
$request = new \stdClass();
132-
$request->attributes = new ParameterBag($attributes);
133-
134-
$event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock();
135+
$event = $this->getMockBuilder(FinishRequestEvent::class)->disableOriginalConstructor()->getMock();
135136
$event->method('getRequest')->willReturn($request);
136137

137138
return $event;
138139
}
139140

140- private function getEmptyRequestEvent(): GetResponseEvent
141+
private function mockEmptyRequest(): Request
141142
{
142-
return $this->getRequestEvent(array());
143+
return $this->mockRequest(array());
143144
}
144145

145-
private function getFilledRequestEvent(string $controller = self::TEST_CONTROLLER): GetResponseEvent
146+
private function mockFilledRequest(string $controller = self::TEST_CONTROLLER): Request
146147
{
147-
return $this->getRequestEvent(
148-
array(
149-
'_controller' => $controller,
150-
'_route' => self::TEST_ROUTE,
151-
'_route_params' => self::TEST_PARAMS,
152-
)
153-
);
148+
return $this->mockRequest(array(
149+
'_controller' => $controller,
150+
'_route' => self::TEST_ROUTE,
151+
'_route_params' => self::TEST_PARAMS,
152+
));
153+
}
154+
155+
private function mockRequest(array $attributes): Request
156+
{
157+
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock();
158+
$request->attributes = new ParameterBag($attributes);
159+
160+
return $request;
154161
}
155162
}

0 commit comments

Comments
 (0)
0