From 7db8e763d8315d63e2c032f17e0a0fa8139b7c8d Mon Sep 17 00:00:00 2001 From: wickedOne Date: Tue, 11 Feb 2020 14:52:19 +0100 Subject: [PATCH 1/4] get and set ignored classes allow ignored classes to be set in the rest action reader fixes #2090 --- Routing/Loader/Reader/RestActionReader.php | 60 +++++++++++++++++----- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/Routing/Loader/Reader/RestActionReader.php b/Routing/Loader/Reader/RestActionReader.php index 6fd6d72f3..6223136c9 100644 --- a/Routing/Loader/Reader/RestActionReader.php +++ b/Routing/Loader/Reader/RestActionReader.php @@ -67,6 +67,31 @@ class RestActionReader private $availableConventionalActions = ['new', 'edit', 'remove']; private $hasMethodPrefix; + /** + * ignore several type hinted arguments. + * + * @var array + */ + private $ignoredClasses = [ + ConstraintViolationListInterface::class, + MessageInterface::class, + ParamConverter::class, + ParamFetcherInterface::class, + Request::class, + SessionInterface::class, + UserInterface::class, + ]; + + /** + * Initializes controller reader. + * + * @param Reader $annotationReader + * @param ParamReaderInterface $paramReader + * @param InflectorInterface $inflector + * @param bool $includeFormat + * @param array $formats + * @param bool $hasMethodPrefix + */ public function __construct(Reader $annotationReader, ParamReaderInterface $paramReader, InflectorInterface $inflector, bool $includeFormat, array $formats = [], bool $hasMethodPrefix = true) { $this->annotationReader = $annotationReader; @@ -157,6 +182,26 @@ public function getParents() return $this->parents; } + /** + * Set ignored classes. + * + * @param array $ignoredClasses + */ + public function setIgnoredClasses(array $ignoredClasses) + { + $this->ignoredClasses = $ignoredClasses; + } + + /** + * Get ignored classes. + * + * @return array + */ + public function getIgnoredClasses() + { + return $this->ignoredClasses; + } + /** * @param string[] $resource * @@ -184,7 +229,7 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method, return; } - list($httpMethod, $resources, $isCollection, $isInflectable) = $httpMethodAndResources; + [$httpMethod, $resources, $isCollection, $isInflectable] = $httpMethodAndResources; $arguments = $this->getMethodArguments($method); // if we have only 1 resource & 1 argument passed, then it's object call, so @@ -423,17 +468,6 @@ private function getMethodArguments(\ReflectionMethod $method): array }, $this->annotationReader->getMethodAnnotations($method)); } - // ignore several type hinted arguments - $ignoreClasses = [ - ConstraintViolationListInterface::class, - MessageInterface::class, - ParamConverter::class, - ParamFetcherInterface::class, - Request::class, - SessionInterface::class, - UserInterface::class, - ]; - $arguments = []; foreach ($method->getParameters() as $argument) { if (isset($params[$argument->getName()])) { @@ -443,7 +477,7 @@ private function getMethodArguments(\ReflectionMethod $method): array $argumentClass = $argument->getClass(); if ($argumentClass) { $className = $argumentClass->getName(); - foreach ($ignoreClasses as $class) { + foreach ($this->getIgnoredClasses() as $class) { if ($className === $class || is_subclass_of($className, $class)) { continue 2; } From 77f9a0d8159a3ad87994aeb34ee4e00e76166b8a Mon Sep 17 00:00:00 2001 From: wickedOne Date: Wed, 12 Feb 2020 08:39:16 +0100 Subject: [PATCH 2/4] documented getter and setter added return types and a little documentation for ignored classes --- Routing/Loader/Reader/RestActionReader.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Routing/Loader/Reader/RestActionReader.php b/Routing/Loader/Reader/RestActionReader.php index 6223136c9..10aaccc1f 100644 --- a/Routing/Loader/Reader/RestActionReader.php +++ b/Routing/Loader/Reader/RestActionReader.php @@ -185,9 +185,12 @@ public function getParents() /** * Set ignored classes. * + * These classes will be ignored for route construction when + * type hinted as method argument. + * * @param array $ignoredClasses */ - public function setIgnoredClasses(array $ignoredClasses) + public function setIgnoredClasses(array $ignoredClasses): void { $this->ignoredClasses = $ignoredClasses; } @@ -197,7 +200,7 @@ public function setIgnoredClasses(array $ignoredClasses) * * @return array */ - public function getIgnoredClasses() + public function getIgnoredClasses(): array { return $this->ignoredClasses; } From d85dc375e21aae2bfc493b7e7037d69750d2dec0 Mon Sep 17 00:00:00 2001 From: wickedOne Date: Wed, 12 Feb 2020 18:08:48 +0100 Subject: [PATCH 3/4] fixed type hinting changed array type hints to be more explicit --- Routing/Loader/Reader/RestActionReader.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Routing/Loader/Reader/RestActionReader.php b/Routing/Loader/Reader/RestActionReader.php index 10aaccc1f..151cd7c0e 100644 --- a/Routing/Loader/Reader/RestActionReader.php +++ b/Routing/Loader/Reader/RestActionReader.php @@ -69,8 +69,6 @@ class RestActionReader /** * ignore several type hinted arguments. - * - * @var array */ private $ignoredClasses = [ ConstraintViolationListInterface::class, @@ -188,7 +186,7 @@ public function getParents() * These classes will be ignored for route construction when * type hinted as method argument. * - * @param array $ignoredClasses + * @param string[] $ignoredClasses */ public function setIgnoredClasses(array $ignoredClasses): void { @@ -198,7 +196,7 @@ public function setIgnoredClasses(array $ignoredClasses): void /** * Get ignored classes. * - * @return array + * @return string[] */ public function getIgnoredClasses(): array { From 0c68b10ce7c7ad0fe614d1685358a3e117b1bdc5 Mon Sep 17 00:00:00 2001 From: wickedOne Date: Fri, 10 Apr 2020 16:53:40 +0200 Subject: [PATCH 4/4] remove constructor docblock fixing a rebase mishap --- Routing/Loader/Reader/RestActionReader.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Routing/Loader/Reader/RestActionReader.php b/Routing/Loader/Reader/RestActionReader.php index 151cd7c0e..191dd367b 100644 --- a/Routing/Loader/Reader/RestActionReader.php +++ b/Routing/Loader/Reader/RestActionReader.php @@ -80,16 +80,6 @@ class RestActionReader UserInterface::class, ]; - /** - * Initializes controller reader. - * - * @param Reader $annotationReader - * @param ParamReaderInterface $paramReader - * @param InflectorInterface $inflector - * @param bool $includeFormat - * @param array $formats - * @param bool $hasMethodPrefix - */ public function __construct(Reader $annotationReader, ParamReaderInterface $paramReader, InflectorInterface $inflector, bool $includeFormat, array $formats = [], bool $hasMethodPrefix = true) { $this->annotationReader = $annotationReader;