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

get and set ignored classes #2091

Merged
merged 4 commits into from Apr 17, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 48 additions & 13 deletions Routing/Loader/Reader/RestActionReader.php
Expand Up @@ -67,6 +67,29 @@ class RestActionReader
private $availableConventionalActions = ['new', 'edit', 'remove'];
private $hasMethodPrefix;

/**
* ignore several type hinted arguments.
*/
private $ignoredClasses = [
ConstraintViolationListInterface::class,
MessageInterface::class,
ParamConverter::class,
ParamFetcherInterface::class,
Request::class,
SessionInterface::class,
UserInterface::class,
];

/**
* Initializes controller reader.
Tobion marked this conversation as resolved.
Show resolved Hide resolved
*
* @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;
Expand Down Expand Up @@ -157,6 +180,29 @@ public function getParents()
return $this->parents;
}

/**
* Set ignored classes.
Tobion marked this conversation as resolved.
Show resolved Hide resolved
*
* These classes will be ignored for route construction when
* type hinted as method argument.
*
* @param string[] $ignoredClasses
*/
public function setIgnoredClasses(array $ignoredClasses): void
{
$this->ignoredClasses = $ignoredClasses;
}

/**
* Get ignored classes.
*
* @return string[]
*/
public function getIgnoredClasses(): array
Tobion marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->ignoredClasses;
}

/**
* @param string[] $resource
*
Expand Down Expand Up @@ -184,7 +230,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
Expand Down Expand Up @@ -423,17 +469,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()])) {
Expand All @@ -443,7 +478,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;
}
Expand Down