Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request listener instead of controller #72

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions features/demo_app/default_config/routes.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions features/demo_app/mapping_collector_config/routes.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
<?php
namespace Yoanm\SymfonyJsonRpcHttpServer\Endpoint;
namespace Yoanm\SymfonyJsonRpcHttpServer\EventListener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint as SDKJsonRpcEndpoint;

/**
* Class JsonRpcHttpEndpoint
*/
class JsonRpcHttpEndpoint
class RequestListener
{
/** @var string */
private $uri;
/** @var SdkJsonRpcEndpoint */
private $sdkEndpoint;

/** @var string[] */
private $allowedMethodList = [];

/**
* @param SDKJsonRpcEndpoint $sdkEndpoint
*/
public function __construct(SDKJsonRpcEndpoint $sdkEndpoint)
public function __construct(SDKJsonRpcEndpoint $sdkEndpoint, $uri)
{
$this->uri = $uri;
$this->sdkEndpoint = $sdkEndpoint;
$this->allowedMethodList = [Request::METHOD_POST, Request::METHOD_OPTIONS];
}

/**
* @return Response
*/
public function httpOptions() : Response
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMasterRequest()) {
// Don't do anything if it's not the master request !
return;
}

$request = $event->getRequest();
if ($this->uri === $request->getRequestUri()) {
switch ($request->getMethod()) {
case Request::METHOD_POST:
$event->setResponse($this->httpPost($request));
break;
case Request::METHOD_OPTIONS:
$event->setResponse($this->httpOptions());
break;
}
}
}

protected function httpOptions() : Response
{
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
Expand All @@ -45,12 +59,7 @@ public function httpOptions() : Response
return $response;
}

/**
* @param Request $request
*
* @return Response
*/
public function httpPost(Request $request) : Response
protected function httpPost(Request $request) : Response
{
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
Expand Down
21 changes: 0 additions & 21 deletions src/Resources/config/routing/endpoint.xml

This file was deleted.

9 changes: 9 additions & 0 deletions src/Resources/config/services.private.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ services:
# Alias method resolver (used in sdk.services.app.yml)
json_rpc_http_server.alias.method_resolver: '@json_rpc_http_server.method_resolver'

json_rpc_http_server.request_listener:
class: Yoanm\SymfonyJsonRpcHttpServer\EventListener\RequestListener
arguments:
- '@json_rpc_server_sdk.infra.endpoint'
- '%json_rpc_http_server.http_endpoint_path%'
tags:
- { name: kernel.event_listener, event: kernel.request, method: 'onKernelRequest', priority: 9999 }


5 changes: 0 additions & 5 deletions src/Resources/config/services.public.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ services:
_defaults:
public: true

json_rpc_http_server.endpoint:
class: Yoanm\SymfonyJsonRpcHttpServer\Endpoint\JsonRpcHttpEndpoint
arguments:
- '@json_rpc_server_sdk.infra.endpoint'

json_rpc_http_server.dispatcher.server:
class: Yoanm\SymfonyJsonRpcHttpServer\Dispatcher\SymfonyJsonRpcServerDispatcher
arguments:
Expand Down
6 changes: 3 additions & 3 deletions tests/Common/DependencyInjection/AbstractTestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

abstract class AbstractTestClass extends AbstractExtensionTestCase
{
const EXPECTED_ENDPOINT_SERVICE_ID = 'json_rpc_http_server.endpoint';
const EXPECTED_DISPATCHER_SERVICE_ID = 'json_rpc_http_server.dispatcher.server';
const EXPECTED_HTTP_ENDPOINT_PATH_CONTAINER_PARAM = 'json_rpc_http_server.http_endpoint_path';
const EXPECTED_PARAMS_VALIDATOR_ALIAS = 'json_rpc_http_server.alias.params_validator';
const EXPECTED_REQUEST_HANDLER_SERVICE_ID = 'json_rpc_server_sdk.app.handler.jsonrpc_request';
Expand Down Expand Up @@ -47,11 +47,11 @@ protected function loadContainer(array $configurationValues = [], $mockResolver
}


protected function assertEndpointIsUsable()
protected function assertDispatcherInstalled()
{
// Retrieving this service will imply to load all related dependencies
// Any binding issues will be raised
$this->assertNotNull($this->container->get(self::EXPECTED_ENDPOINT_SERVICE_ID));
$this->assertNotNull($this->container->get(self::EXPECTED_DISPATCHER_SERVICE_ID));
}

/**
Expand Down
16 changes: 9 additions & 7 deletions tests/Functional/DependencyInjection/ConfigFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection\JsonRpcHttpServerExtension;
use Yoanm\SymfonyJsonRpcHttpServer\Dispatcher\SymfonyJsonRpcServerDispatcher;
use Yoanm\SymfonyJsonRpcHttpServer\Endpoint\JsonRpcHttpEndpoint;
use Yoanm\SymfonyJsonRpcHttpServer\EventListener\RequestListener;
use Yoanm\SymfonyJsonRpcHttpServer\Resolver\MethodResolver;

/**
Expand All @@ -38,6 +39,7 @@ protected function getContainerExtensions(): array
* @dataProvider provideSDKAppServiceIdAndClass
* @dataProvider provideSDKInfraServiceIdAndClass
* @dataProvider provideBundlePublicServiceIdAndClass
* @dataProvider provideBundlePrivateServiceIdAndClass
*
* @param string $serviceId
* @param string $expectedClassName
Expand Down Expand Up @@ -123,11 +125,6 @@ public function provideSDKInfraServiceIdAndClass()
public function provideBundlePublicServiceIdAndClass()
{
return [
'Bundle - Public - HTTP endpoint' => [
'serviceId' => 'json_rpc_http_server.endpoint',
'serviceClassName' => JsonRpcHttpEndpoint::class,
'public' => true,
],
'Bundle - Public - Event Dispatcher' => [
'serviceId' => 'json_rpc_http_server.dispatcher.server',
'serviceClassName' => SymfonyJsonRpcServerDispatcher::class,
Expand All @@ -142,15 +139,20 @@ public function provideBundlePublicServiceIdAndClass()
public function provideBundlePrivateServiceIdAndClass()
{
return [
'Bundle - Public - HTTP endpoint' => [
'serviceId' => 'json_rpc_http_server.request_listener',
'serviceClassName' => RequestListener::class,
'public' => false,
],
'Bundle - Private - JSON-RPC method resolver ServiceLocator' => [
'serviceId' => 'json_rpc_http_server.service_locator.method_resolver',
'serviceClassName' => ServiceLocator::class,
'public' => true,
'public' => false,
],
'Bundle - Private - MethodResolver alias' => [
'serviceId' => 'json_rpc_http_server.alias.method_resolver',
'serviceClassName' => MethodResolver::class,
'public' => true,
'public' => false,
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testShouldBeLoadable()
{
$this->loadContainer();

$this->assertEndpointIsUsable();
$this->assertDispatcherInstalled();
}

public function testShouldManageCustomEndpointPathFromConfiguration()
Expand All @@ -42,7 +42,7 @@ public function testShouldManageCustomEndpointPathFromConfiguration()
// Assert custom resolver is an alias of the stub
$this->assertContainerBuilderHasParameter(self::EXPECTED_HTTP_ENDPOINT_PATH_CONTAINER_PARAM, $myCustomEndpoint);

$this->assertEndpointIsUsable();
$this->assertDispatcherInstalled();
}

public function testShouldReturnAnXsdValidationBasePath()
Expand All @@ -67,7 +67,7 @@ public function testShouldBindServerDispatcherToDispatcherAwareService()
0
);

$this->assertEndpointIsUsable();
$this->assertDispatcherInstalled();
}

public function testShouldThrowAnExceptionIfDispatcherAwareServiceDoesNotUseRightTrait()
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testShouldInjectParamsValidatorAliasIfDefined()
[new Reference(self::EXPECTED_PARAMS_VALIDATOR_ALIAS)]
);

$this->assertEndpointIsUsable();
$this->assertDispatcherInstalled();
}

public function testShouldNotInjectParamsValidatorAliasIfNotDefined()
Expand All @@ -118,7 +118,7 @@ public function testShouldNotInjectParamsValidatorAliasIfNotDefined()
}
}

$this->assertEndpointIsUsable();
$this->assertDispatcherInstalled();
}

public function testShouldBindJsonRpcMethodsToMethodAwareServices()
Expand Down Expand Up @@ -165,7 +165,7 @@ public function testShouldBindJsonRpcMethodsToMethodAwareServices()
1
);

$this->assertEndpointIsUsable();
$this->assertDispatcherInstalled();
}

public function testShouldThowAnExceptionIfMethodAwareServiceDoesNotImplementRightInterface()
Expand Down
77 changes: 0 additions & 77 deletions tests/Functional/Endpoint/JsonRpcHttpEndpointTest.php

This file was deleted.

Loading