diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..82266d7 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/composer.json b/composer.json index 512a4b3..0713f0e 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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" } } diff --git a/src/Annotation/JsonRpcMethod.php b/src/Annotation/JsonRpcMethod.php new file mode 100644 index 0000000..bad8a90 --- /dev/null +++ b/src/Annotation/JsonRpcMethod.php @@ -0,0 +1,43 @@ + $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; + } +} diff --git a/src/DependencyInjection/JsonRpcHttpServerExtension.php b/src/DependencyInjection/JsonRpcHttpServerExtension.php index d71bfaf..6a8b34d 100644 --- a/src/DependencyInjection/JsonRpcHttpServerExtension.php +++ b/src/DependencyInjection/JsonRpcHttpServerExtension.php @@ -66,7 +66,7 @@ public function process(ContainerBuilder $container) { $this->bindJsonRpcServerDispatcher($container); $this->bindValidatorIfDefined($container); - $this->binJsonRpcMethods($container); + $this->bindJsonRpcMethods($container); } /** @@ -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 = []; diff --git a/src/DependencyInjection/JsonRpcMethodDefinitionHelper.php b/src/DependencyInjection/JsonRpcMethodDefinitionHelper.php index b924c2d..d215c47 100644 --- a/src/DependencyInjection/JsonRpcMethodDefinitionHelper.php +++ b/src/DependencyInjection/JsonRpcMethodDefinitionHelper.php @@ -5,12 +5,30 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Exception\LogicException; use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface; +use Yoanm\SymfonyJsonRpcHttpServer\Annotation; +use Doctrine\Common\Annotations\AnnotationReader; /** * Class JsonRpcMethodDefinitionHelper */ class JsonRpcMethodDefinitionHelper { + + /** @var bool */ + private $annotationsEnabled; + + /** @var \Doctrine\Common\Annotations\AnnotationReader|null */ + private $reader; + + public function __construct(ContainerBuilder $container) + { + $this->annotationsEnabled = $container->has('routing.loader.annotation') + && class_exists('Doctrine\Common\Annotations\AnnotationReader'); + if ($this->annotationsEnabled) { + $this->reader = new AnnotationReader(); + } + } + /** * @param ContainerBuilder $container * @@ -26,8 +44,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, Annotation\JsonRpcMethod::class); + if (null !== $annotation) { + /** @var \Yoanm\SymfonyJsonRpcHttpServer\Annotation\JsonRpcMethod $annotation */ + $methodName = $annotation->getName(); + } else { + throw $e; + } + } else { + throw $e; + } + } $definitionList[$serviceId][] = $methodName; } } diff --git a/tests/Functional/DependencyInjection/JsonRpcMethodDefinitionHelperTest.php b/tests/Functional/DependencyInjection/JsonRpcMethodDefinitionHelperTest.php index 4c05dab..7c1177a 100644 --- a/tests/Functional/DependencyInjection/JsonRpcMethodDefinitionHelperTest.php +++ b/tests/Functional/DependencyInjection/JsonRpcMethodDefinitionHelperTest.php @@ -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()