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

Annotation-powered method registration #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; top-most EditorConfig file
root = true

; Unix-style newlines
[*]
charset = utf-8
end_of_line = LF
insert_final_newline = true
trim_trailing_whitespace = true

[*.php]
indent_style = space
indent_size = 4

[composer.{json,lock}]
indent_size = 2

[*.md]
max_line_length = 80

[COMMIT_EDITMSG]
max_line_length = 0
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"suggest": {
"yoanm/symfony-jsonrpc-params-validator": "Symfony bundle for easy JSON-RPC params validation",
"yoanm/symfony-jsonrpc-http-server-doc": "JSON-RPC documentation Bundle"
"yoanm/symfony-jsonrpc-http-server-doc": "JSON-RPC documentation Bundle",
"doctrine/annotations": "Autowire methods using annotations."
},
"require": {
"php": ">=7.2",
Expand All @@ -48,6 +49,7 @@
"matthiasnoback/symfony-config-test": "^3.0 || ^4.0",
"symfony/framework-bundle": "^4.0 || ^5.0",
"symfony/routing": "^4.0 || ^5.0",
"yoanm/php-unit-extended": "~1.0"
"yoanm/php-unit-extended": "~1.0",
"doctrine/annotations": "^1.13"
}
}
42 changes: 42 additions & 0 deletions src/Annotation/JsonRpcMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Yoanm\SymfonyJsonRpcHttpServer\Annotation;

/**
* Annotation class for @JsonRpcMethod().
*
* @Annotation
* @Target({"CLASS"})
*/
class JsonRpcMethod
{
private $name;

/**
* @param array $data An array of key/value parameters
*
* @throws \BadMethodCallException
*/
public function __construct(array $data)
{
foreach ($data as $key => $value) {
$method = 'set'.str_replace('_', '', $key);
if (!method_exists($this, $method)) {
throw new \BadMethodCallException(
sprintf('Unknown property "%s" on annotation "%s".', $key, static::class)
);
}
$this->$method($value);
}
}

public function setName(string $name)
{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}
6 changes: 3 additions & 3 deletions src/DependencyInjection/JsonRpcHttpServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function process(ContainerBuilder $container)
{
$this->bindJsonRpcServerDispatcher($container);
$this->bindValidatorIfDefined($container);
$this->binJsonRpcMethods($container);
$this->bindJsonRpcMethods($container);
}

/**
Expand Down Expand Up @@ -149,11 +149,11 @@ private function bindValidatorIfDefined(ContainerBuilder $container) : void
/**
* @param ContainerBuilder $container
*/
private function binJsonRpcMethods(ContainerBuilder $container) : void
private function bindJsonRpcMethods(ContainerBuilder $container) : void
{
$mappingAwareServiceDefinitionList = $this->findAndValidateMappingAwareDefinitionList($container);

$jsonRpcMethodDefinitionList = (new JsonRpcMethodDefinitionHelper())
$jsonRpcMethodDefinitionList = (new JsonRpcMethodDefinitionHelper($container))
->findAndValidateJsonRpcMethodDefinition($container);

$methodMappingList = [];
Expand Down
39 changes: 37 additions & 2 deletions src/DependencyInjection/JsonRpcMethodDefinitionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
Expand All @@ -11,6 +12,22 @@
*/
class JsonRpcMethodDefinitionHelper
{

private $annotationsEnabled;

private $reader;

private const METHOD_ANNOTATION_CLASS = 'Yoanm\\SymfonyJsonRpcHttpServer\\Annotation\\JsonRpcMethod';

public function __construct(ContainerBuilder $container)
{
$this->annotationsEnabled = $container->has('routing.loader.annotation')
&& class_exists('Doctrine\Common\Annotations\AnnotationReader');
if ($this->annotationsEnabled) {
$this->reader = new \Doctrine\Common\Annotations\AnnotationReader();
}
}

/**
* @param ContainerBuilder $container
*
Expand All @@ -26,8 +43,26 @@ public function findAndValidateJsonRpcMethodDefinition(ContainerBuilder $contain
$this->validateJsonRpcMethodDefinition($serviceId, $container->getDefinition($serviceId));

foreach ($tagAttributeList as $tagAttributeKey => $tagAttributeData) {
$this->validateJsonRpcMethodTagAttributes($serviceId, $tagAttributeData);
$methodName = $tagAttributeData[JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG_METHOD_NAME_KEY];
try {
$this->validateJsonRpcMethodTagAttributes($serviceId, $tagAttributeData);
$methodName = $tagAttributeData[JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG_METHOD_NAME_KEY];
} catch (\LogicException $e) {
// Annotation routing loader is conditionally added by the framework
// bundle; if it's enabled, allow for our own annotation-based
// method discovery.
if ($this->annotationsEnabled) {
$reflection = $container->getReflectionClass($container->getDefinition($serviceId)->getClass());
$annotation = $this->reader->getClassAnnotation($reflection, self::METHOD_ANNOTATION_CLASS);
if ($annotation) {
/** @var \Yoanm\SymfonyJsonRpcHttpServer\Annotation\JsonRpcMethod $annotation */
$methodName = $annotation->getName();
} else {
throw $e;
}
} else {
throw $e;
}
}
$definitionList[$serviceId][] = $methodName;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class JsonRpcMethodDefinitionHelperTest extends AbstractTestClass

protected function setUp(): void
{
$this->helper = new JsonRpcMethodDefinitionHelper();
parent::setUp();
$this->helper = new JsonRpcMethodDefinitionHelper($this->container);
}

public function testShouldManageJsonRpcMethodMappingFromTag()
Expand Down