Skip to content

Commit ce6858e

Browse files
authored
Implement Context system (#4064)
2 parents fb2c669 + 94d8a99 commit ce6858e

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopsys\FrontendApiBundle\Component\Context;
6+
7+
use Override;
8+
use Shopsys\FrameworkBundle\Component\Context\AbstractContext;
9+
use Symfony\Component\HttpFoundation\RequestStack;
10+
11+
final class FrontendApiContext extends AbstractContext
12+
{
13+
/**
14+
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
15+
*/
16+
public function __construct(
17+
private readonly RequestStack $requestStack,
18+
) {
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
#[Override]
25+
public function getDescription(): string
26+
{
27+
return 'Matches requests to Frontend API endpoints (GraphQL)';
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
#[Override]
34+
public function matches(): bool
35+
{
36+
$request = $this->requestStack->getMainRequest();
37+
38+
if ($request === null) {
39+
return false;
40+
}
41+
42+
return in_array($request->attributes->get('_route'), ['overblog_graphql_endpoint', 'overblog_graphql_batch_endpoint'], true);
43+
}
44+
}

src/Component/HttpFoundation/TransactionalMasterRequestConditionProvider.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@
88
use GraphQL\Language\AST\OperationDefinitionNode;
99
use GraphQL\Language\Parser;
1010
use Override;
11+
use Shopsys\FrameworkBundle\Component\Context\ContextResolverInterface;
1112
use Shopsys\FrameworkBundle\Component\HttpFoundation\TransactionalMasterRequestConditionProviderInterface;
13+
use Shopsys\FrontendApiBundle\Component\Context\FrontendApiContext;
1214
use Shopsys\FrontendApiBundle\Model\Error\InvalidArgumentUserError;
1315
use Symfony\Component\HttpKernel\Event\RequestEvent;
1416

1517
class TransactionalMasterRequestConditionProvider implements TransactionalMasterRequestConditionProviderInterface
1618
{
17-
protected const GRAPHQL_ENDPOINT_ROUTE = 'overblog_graphql_endpoint';
1819
protected const QUERY_TYPE = 'query';
1920

21+
/**
22+
* @param \Shopsys\FrameworkBundle\Component\Context\ContextResolverInterface $contextResolver
23+
*/
24+
public function __construct(
25+
protected readonly ContextResolverInterface $contextResolver,
26+
) {
27+
}
28+
2029
/**
2130
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
2231
* @return bool
@@ -33,7 +42,7 @@ public function shouldBeginTransaction(RequestEvent $event): bool
3342
*/
3443
protected function isRequestGraphQlQuery(RequestEvent $requestEvent): bool
3544
{
36-
if ($requestEvent->getRequest()->attributes->get('_route') !== static::GRAPHQL_ENDPOINT_ROUTE) {
45+
if (!$this->contextResolver->isCurrentContext(FrontendApiContext::class)) {
3746
return false;
3847
}
3948

src/Model/Error/ErrorHandlerListener.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@
44

55
namespace Shopsys\FrontendApiBundle\Model\Error;
66

7+
use Shopsys\FrameworkBundle\Component\Context\ContextResolverInterface;
8+
use Shopsys\FrontendApiBundle\Component\Context\FrontendApiContext;
79
use Symfony\Component\HttpFoundation\JsonResponse;
810
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
911
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1012

1113
class ErrorHandlerListener
1214
{
15+
/**
16+
* @param \Shopsys\FrameworkBundle\Component\Context\ContextResolverInterface $contextResolver
17+
*/
18+
public function __construct(
19+
protected readonly ContextResolverInterface $contextResolver,
20+
) {
21+
}
22+
1323
/**
1424
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
1525
*/
1626
public function onKernelException(ExceptionEvent $event): void
1727
{
1828
$throwable = $event->getThrowable();
19-
$routeParam = $event->getRequest()->attributes->get('_route');
2029

21-
if (!($throwable instanceof BadRequestHttpException) || !$this->isGraphQlRoute($routeParam)) {
30+
if (!($throwable instanceof BadRequestHttpException) || !$this->contextResolver->isCurrentContext(FrontendApiContext::class)) {
2231
return;
2332
}
2433

@@ -30,13 +39,4 @@ public function onKernelException(ExceptionEvent $event): void
3039

3140
$event->setResponse(new JsonResponse($errors));
3241
}
33-
34-
/**
35-
* @param string $routeParam
36-
* @return bool
37-
*/
38-
protected function isGraphQlRoute(string $routeParam): bool
39-
{
40-
return in_array($routeParam, ['overblog_graphql_endpoint', 'overblog_graphql_batch_endpoint'], true);
41-
}
4242
}

src/Resources/config/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
public: false
66

77
Shopsys\FrontendApiBundle\:
8-
resource: '../../**/*{Facade,Factory,Helper,Listener,Loader,Mapper,Mutation,Provider,Query,Repository,Subscriber,Transformer,Validator}.php'
8+
resource: '../../**/*{Context,Facade,Factory,Helper,Listener,Loader,Mapper,Mutation,Provider,Query,Repository,Subscriber,Transformer,Validator}.php'
99

1010
resolverMaps:
1111
namespace: Shopsys\FrontendApiBundle\

0 commit comments

Comments
 (0)